rendering: fixed view culling
authorLukas Krickl <lukas@krickl.dev>
Sat, 7 Mar 2026 12:02:53 +0000 (13:02 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 7 Mar 2026 12:02:53 +0000 (13:02 +0100)
Previously it would incorectly render way too many tiles.

src/lrts.c
src/r_render.c
src/t_map.c

index 25f205018903197299e2540847d8e8ce8a2539d0..8f34d7c3313fd7fdeff4a17d9a20d6fccaaa0fa6 100644 (file)
@@ -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;
index bbf5e8797fa1129f0362fcbd7ae7963dfd3ba248..b493929128d7c10291e96d6deeeb5c00f1df447f 100644 (file)
@@ -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);
 }
index 018ac291d1ea82761388d9b757139015d65029cd..f1f796aa8e7472130b1dd915ba5125086fe9d806 100644 (file)
@@ -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++;
                }
        }
 }