summaryrefslogtreecommitdiff
path: root/src/render.cpp
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-12-12 15:44:19 +0100
committerPaweł Redman <pawel.redman@gmail.com>2017-12-12 15:44:19 +0100
commitc33d0fa9c43fb316a9c6bbb2be81446dce7f49da (patch)
treea4ded215d9dd7c5098b395925f0c19956c46670c /src/render.cpp
parent5667984053536d855ba0917deac326d429f2c5b4 (diff)
Move tile data to src/game.
Diffstat (limited to 'src/render.cpp')
-rw-r--r--src/render.cpp95
1 files changed, 49 insertions, 46 deletions
diff --git a/src/render.cpp b/src/render.cpp
index ba50d00..48beaa9 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -5,51 +5,6 @@
static sf::RectangleShape wot_rect;
static sf::Font font;
-static void draw_tile(sf::RenderWindow *window, v2f_t x, world::tile_t *tile,
- world::tile_index_t local)
-{
- sf::Texture *texture;
-
- switch (tile->type) {
- default:
- return;
-
- /*case world::TILE_DIRT:
- texture = &assets::tile_dirt;
- break;
-
- case world::TILE_WALL:
- texture = &assets::tile_wall;
- break;*/
- }
-
- wot_rect.setTexture(texture, true);
- wot_rect.setSize(sf::Vector2f(1.0f, 1.0f));
- wot_rect.setPosition(x);
- wot_rect.setFillColor(sf::Color::White);
- wot_rect.setOutlineColor(sf::Color::Transparent);
- window->draw(wot_rect);
- wot_rect.setTexture(NULL);
-
- if (debug_draw_tile_coords) {
- std::stringstream ss;
- ss << "L=" << local;
- sf::Text text(ss.str(), font, 20);
- text.setPosition(x);
- text.setScale(0.005, 0.005);
- window->draw(text);
- }
-}
-
-static void draw_sector(sf::RenderWindow *window, world::sector_t *sector)
-{
- for (ssize_t y = 0; y < SECTOR_SIZE; y++)
- for (ssize_t x = 0; x < SECTOR_SIZE; x++)
- draw_tile(window, sector->bounds.v[0] + v2f_t(x, y),
- sector->tiles + y * SECTOR_SIZE + x,
- world::tile_index_t(x, y));
-}
-
void interface::state_t::render_to(render::state_t *render)
{
if (select.selecting)
@@ -65,6 +20,16 @@ state_t::state_t(sf::RenderWindow *window_)
font.loadFromFile("assets/LiberationMono-Regular.ttf");
}
+// FIXME: rename
+static sf::Texture *tiles[256] = {0};
+
+void register_tile(uint8_t type, const char *path)
+{
+ printf("load %s\n", path);
+ tiles[type] = new sf::Texture;
+ tiles[type]->loadFromFile(path);
+}
+
void state_t::begin_frame(double now_, double dt_)
{
now = now_;
@@ -109,6 +74,44 @@ void state_t::drender_entity(world::entity_t *ent)
drender_text(ent->render_bounds, ss.str());
}
+void state_t::render_sector(world::sector_t *sector)
+{
+ for (ssize_t y = 0; y < SECTOR_SIZE; y++)
+ for (ssize_t x = 0; x < SECTOR_SIZE; x++) {
+ sf::Texture *texture;
+ v2f_t tx;
+ world::tile_t *tile;
+
+ tx = sector->bounds.v[0] + v2f_t(x, y);
+ tile = sector->tiles + y * SECTOR_SIZE + x;
+
+ texture = tiles[tile->type];
+ if (!texture) {
+ printf("draw_tile: tile %i not registered\n", tile->type);
+ abort();
+ }
+
+ wot_rect.setTexture(texture, true);
+ wot_rect.setSize(sf::Vector2f(1.0f, 1.0f));
+ wot_rect.setPosition(tx);
+ wot_rect.setFillColor(sf::Color::White);
+ wot_rect.setOutlineColor(sf::Color::Transparent);
+ window->draw(wot_rect);
+ wot_rect.setTexture(NULL);
+
+ if (debug_draw_tile_coords) {
+ world::tile_index_t local(x, y);
+ std::stringstream ss;
+
+ ss << "L=" << local;
+ sf::Text text(ss.str(), font, 20);
+ text.setPosition(tx);
+ text.setScale(0.005, 0.005);
+ window->draw(text);
+ }
+ }
+}
+
void state_t::render(game::state_t *game)
{
sf::Vector2u size = window->getSize();
@@ -127,7 +130,7 @@ void state_t::render(game::state_t *game)
bbox[1][1] = std::max({A[1], B[1], C[1], D[1]});
for (world::sector_t *sector : game->world.get_sectors(bbox))
- draw_sector(window, sector);
+ render_sector(sector);
ents = game->world.get_render_entities(bbox);
ents.sort(