From: Lukas Krickl Date: Sat, 7 Mar 2026 12:02:53 +0000 (+0100) Subject: rendering: fixed view culling X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=7e47c7d67634194fc99271b47eb77ef3cd2f56f8;p=lrts%2F.git rendering: fixed view culling Previously it would incorectly render way too many tiles. --- diff --git a/src/lrts.c b/src/lrts.c index 25f2050..8f34d7c 100644 --- a/src/lrts.c +++ b/src/lrts.c @@ -89,6 +89,7 @@ void lrts_init(void) { t_main_camera = t_camera_init(res.x, res.y); t_map = t_map_init(); + r_render_update_visible_tiles(); state->frame_count = 0; state->tick_count = 0; diff --git a/src/r_render.c b/src/r_render.c index bbf5e87..b493929 100644 --- a/src/r_render.c +++ b/src/r_render.c @@ -56,27 +56,29 @@ void r_render_frame(void) { void r_render_update_visible_tiles(void) { i32 x, y; - struct u_vec2 t, tt, tl, tb; + struct u_vec2 t0, t1, t2, t3; struct t_camera *c = &t_main_camera; r_render_state.start_tile.x = t_map.width; r_render_state.start_tile.y = t_map.height; + r_render_state.end_tile.x = 0; + r_render_state.end_tile.y = 0; for (x = 0; x < t_map.width; x++) { for (y = 0; y < t_map.height; y++) { - t = t_camera_zoom_vec2(c, u_tile_to_screen(x, y)); - tt = t_camera_zoom_vec2(c, u_tile_to_screen(x+1, y+1)); - tl = t_camera_zoom_vec2(c, u_tile_to_screen(x+2, y)); - tb = t_camera_zoom_vec2(c, u_tile_to_screen(x+2, y-2)); + t0 = t_camera_zoom_vec2(c, u_tile_to_screen(x, y)); + t1 = t_camera_zoom_vec2(c, u_tile_to_screen(x+1, y+1)); + t2 = t_camera_zoom_vec2(c, u_tile_to_screen(x+2, y)); + t3 = t_camera_zoom_vec2(c, u_tile_to_screen(x+2, y-2)); /* do not draw if tile is not visible at all * also include a few points outside the camera to avoid tiles * popping in */ - if (t_camera_is_visible_screen(&t_main_camera, t.x, t.y) - || t_camera_is_visible_screen(&t_main_camera, tt.x, tt.y) - || t_camera_is_visible_screen(&t_main_camera, tb.x, tb.y) - || t_camera_is_visible_screen(&t_main_camera, tl.x, tl.y)) { + if (t_camera_is_visible_screen(&t_main_camera, t0.x, t0.y) + || t_camera_is_visible_screen(&t_main_camera, t1.x, t1.y) + || t_camera_is_visible_screen(&t_main_camera, t2.x, t2.y) + || t_camera_is_visible_screen(&t_main_camera, t3.x, t3.y)) { if (r_render_state.end_tile.x < x) { r_render_state.end_tile.x = x; @@ -95,4 +97,7 @@ void r_render_update_visible_tiles(void) { } } } + + r_render_state.end_tile.x += 1; + U_CLAMP(r_render_state.end_tile.x, 0, t_map.width); } diff --git a/src/t_map.c b/src/t_map.c index 018ac29..f1f796a 100644 --- a/src/t_map.c +++ b/src/t_map.c @@ -17,7 +17,6 @@ struct t_map t_map_init(void) { m.tiles[j * m.width + i].type = T_TILE_TYPE_GRASS; } } - r_render_update_visible_tiles(); return m; } @@ -25,10 +24,12 @@ void t_map_draw(struct t_map *m) { i32 i, j; struct u_vec2 start = r_render_state.start_tile; struct u_vec2 end = r_render_state.end_tile; + u32 drawn_tiles = 0; for (i = start.x; i < end.x; i++) { for (j = start.y; j < end.y; j++) { r_draw_tile(&m->tiles[j * m->width + i], i, j); + drawn_tiles++; } } }