From 819c7899b3452a405bac6300fe44460ca9e5dcd6 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 7 Sep 2025 10:55:19 -0700 Subject: Add support for multiple layers. --- src/gfx2d.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/gfx2d.c b/src/gfx2d.c index 1c8b06f..eaed2b8 100644 --- a/src/gfx2d.c +++ b/src/gfx2d.c @@ -26,6 +26,9 @@ /// Take the minimum of two values. #define min(a, b) ((a) < (b) ? (a) : (b)) +/// The 0-tile denotes "no tile". The renderer skips drawing 0-tiles. +static const Tile NoTile = 0; + typedef struct ivec2 { int x, y; } ivec2; @@ -606,6 +609,10 @@ static void draw_tile_ortho(Gfx2d* gfx, Tile tile, int x, int y) { assert(x < gfx->map->world_width); assert(y < gfx->map->world_height); + if (tile == NoTile) { + return; + } + const Ts_Tile* pTile = ts_tileset_get_tile(gfx->tileset, tile); const Pixel* pixels = ts_tileset_get_tile_pixels(gfx->tileset, tile); @@ -626,6 +633,10 @@ static void draw_tile_iso(Gfx2d* gfx, Tile tile, int iso_x, int iso_y) { assert(iso_x < gfx->map->world_width); assert(iso_y < gfx->map->world_height); + if (tile == NoTile) { + return; + } + const Ts_Tile* pTile = ts_tileset_get_tile(gfx->tileset, tile); const Pixel* pixels = ts_tileset_get_tile_pixels(gfx->tileset, tile); @@ -677,18 +688,19 @@ static void draw_map_iso(Gfx2d* gfx) { assert(gfx); assert(gfx->map); - // TODO: Support for multiple layers. - const Tm_Layer* layer = tm_map_get_layer(gfx->map, 0); - - // TODO: Culling. - // Ex: map the screen corners to tile space to cull. - // Ex: walk in screen space and fetch the tile. - // The tile-centric approach might be more cache-friendly since the - // screen-centric approach would juggle multiple tiles throughout the scan. - for (int wy = 0; wy < gfx->map->world_height; ++wy) { - for (int wx = 0; wx < gfx->map->world_width; ++wx) { - const Tile tile = tm_layer_get_tile(gfx->map, layer, wx, wy); - draw_tile_iso(gfx, tile, wx, wy); + for (uint16_t l = 0; l < gfx->map->num_layers; ++l) { + const Tm_Layer* layer = tm_map_get_layer(gfx->map, l); + + // TODO: Culling. + // Ex: map the screen corners to tile space to cull. + // Ex: walk in screen space and fetch the tile. + // The tile-centric approach might be more cache-friendly since the + // screen-centric approach would juggle multiple tiles throughout the scan. + for (int wy = 0; wy < gfx->map->world_height; ++wy) { + for (int wx = 0; wx < gfx->map->world_width; ++wx) { + const Tile tile = tm_layer_get_tile(gfx->map, layer, wx, wy); + draw_tile_iso(gfx, tile, wx, wy); + } } } } -- cgit v1.2.3