From b13d7aaf6ddf63ce9b7179219542af885740d304 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 7 Oct 2024 00:01:02 +0200 Subject: [PATCH] culled board rendering, much better performance with enormous boards --- src/marble_engine/board.rs | 41 ++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index 90c0518..f764751 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -214,21 +214,32 @@ impl Board { pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) { let tile_size = 16 << zoom; - for x in 0..self.width { - for y in 0..self.height { - let tile = self.rows[y][x]; - let px = - (x as i32 - self.offset_x as i32) * tile_size + offset.x as i32 + tile_size / 2; - let py = - (y as i32 - self.offset_y as i32) * tile_size + offset.y as i32 + tile_size / 2; - let texture = textures.get(&tile.texture()); - d.draw_texture_ex( - texture, - Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32), - 0.0, - (1 << zoom) as f32, - Color::WHITE, - ); + + let start_x = (-offset.x as i32) / tile_size + self.offset_x as i32 - 1; + let tile_width = d.get_screen_width() / tile_size + 2; + let start_y = (-offset.y as i32) / tile_size + self.offset_y as i32 - 1; + let tile_height = d.get_screen_height() / tile_size + 2; + + for x in start_x..(start_x + tile_width) { + for y in start_y..(start_y + tile_height) { + if self.in_bounds(Pos { + x: x as isize, + y: y as isize, + }) { + let tx = x as usize; + let ty = y as usize; + let tile = self.rows[ty][tx]; + let px = (x - self.offset_x as i32) * tile_size + offset.x as i32; + let py = (y - self.offset_y as i32) * tile_size + offset.y as i32; + let texture = textures.get(&tile.texture()); + d.draw_texture_ex( + texture, + Vector2::new(px as f32, py as f32), + 0.0, + (1 << zoom) as f32, + Color::WHITE, + ); + } } } }