From e667ae699b432930932b446834a0c2ead085b996 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 6 Sep 2025 18:28:17 -0700 Subject: Address TODO --- src/gfx2d.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/gfx2d.c') diff --git a/src/gfx2d.c b/src/gfx2d.c index 14e0539..1c8b06f 100644 --- a/src/gfx2d.c +++ b/src/gfx2d.c @@ -651,16 +651,21 @@ static void draw_map_ortho(Gfx2d* gfx) { assert(gfx); assert(gfx->map); + // Render the tiles that the camera view rectangle intersects. + // +1 when computing x1,y1 because the screen dimensions need not be a + // multiple of the base tile dimensions. + const int x_tiles = gfx->screen.width / gfx->map->base_tile_width; + const int y_tiles = gfx->screen.height / gfx->map->base_tile_height; + const int x0 = gfx->camera.x / gfx->map->base_tile_width; + const int y0 = gfx->camera.y / gfx->map->base_tile_height; + const int x1 = min(gfx->map->world_width, x0 + x_tiles + 1); + const int y1 = min(gfx->map->world_height, y0 + y_tiles + 1); + for (uint16_t l = 0; l < gfx->map->num_layers; ++l) { const Tm_Layer* layer = tm_map_get_layer(gfx->map, l); - // TODO: This currently renders with tile granularity. Do so instead in - // terms of pixels for more accurate camera panning. The camera coordinates - // are already given in pixels. - for (int wy = gfx->camera.y / gfx->map->base_tile_height; - wy < gfx->map->world_height; ++wy) { - for (int wx = gfx->camera.x / gfx->map->base_tile_width; - wx < gfx->map->world_width; ++wx) { + for (int wy = y0; wy < y1; ++wy) { + for (int wx = x0; wx < x1; ++wx) { const Tile tile = tm_layer_get_tile(gfx->map, layer, wx, wy); draw_tile_ortho(gfx, tile, wx, wy); } -- cgit v1.2.3