From e3248519ac0387ba7a88ef039411d9878fc5e64e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 2 Feb 2022 10:08:11 -0500 Subject: [PATCH] Refactor camera to batch render --- src/main.rs | 1 - src/map.rs | 1 + src/{ => map}/camera.rs | 70 ++++++++++++++++++----------------------- src/state.rs | 1 + 4 files changed, 33 insertions(+), 40 deletions(-) rename src/{ => map}/camera.rs (76%) diff --git a/src/main.rs b/src/main.rs index d161732..0ce24d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ extern crate lazy_static; mod ai; -pub mod camera; mod colors; mod components; mod damage_system; diff --git a/src/map.rs b/src/map.rs index 9fd236d..7a09a69 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,5 @@ //! An object to hold the state of the current level's map +pub mod camera; mod dungeon; mod themes; mod tiletype; diff --git a/src/camera.rs b/src/map/camera.rs similarity index 76% rename from src/camera.rs rename to src/map/camera.rs index 8dfafc6..06b0720 100644 --- a/src/camera.rs +++ b/src/map/camera.rs @@ -1,6 +1,7 @@ //! Handle rendering of the viewport -use rltk::{Point, Rltk}; -use specs::prelude::*; +use ::rltk::prelude::*; +use ::rltk::{Point, Rltk}; +use ::specs::prelude::*; use crate::components::{Hidden, Position, Renderable, Target, TileSize}; use crate::map::tile_glyph; @@ -28,6 +29,7 @@ pub fn get_screen_bounds(ecs: &World, _ctx: &mut Rltk) -> (i32, i32, i32, i32) { /// Render the current viewport pub fn render_camera(ecs: &World, ctx: &mut Rltk) { + let mut draw_batch = DrawBatch::new(); let map = ecs.fetch::(); let (min_x, max_x, min_y, max_y) = get_screen_bounds(ecs, ctx); @@ -35,23 +37,22 @@ pub fn render_camera(ecs: &World, ctx: &mut Rltk) { let map_width = map.width - 1; let map_height = map.height - 1; - let mut y = 0; - #[allow(clippy::explicit_counter_loop)] - for ty in min_y..max_y { - let mut x = 0; - for tx in min_x..max_x { + for (y, ty) in (min_y..max_y).enumerate() { + for (x, tx) in (min_x..max_x).enumerate() { if tx > 0 && tx < map_width && ty > 0 && ty < map_height { let idx = map.xy_idx(tx, ty); if map.revealed_tiles[idx] { let (glyph, fg, bg) = tile_glyph(idx, &*map); - ctx.set(x, y, fg, bg, glyph); + draw_batch.set(Point::new(x, y), ColorPair::new(fg, bg), glyph); } } else if SHOW_BOUNDARIES { - ctx.set(x, y, colors::GRAY, colors::BLACK, rltk::to_cp437('·')); + draw_batch.set( + Point::new(x, y), + ColorPair::new(colors::GRAY, colors::BLACK), + rltk::to_cp437('·'), + ); } - x += 1; } - y += 1; } // Render entities @@ -82,11 +83,9 @@ pub fn render_camera(ecs: &World, ctx: &mut Rltk) { && entity_screen_y > 0 && entity_screen_y < map_height { - ctx.set( - entity_screen_x + 1, - entity_screen_y + 1, - render.fg, - render.bg, + draw_batch.set( + Point::new(entity_screen_x + 1, entity_screen_y + 1), + ColorPair::new(render.fg, render.bg), render.glyph, ); } @@ -103,11 +102,9 @@ pub fn render_camera(ecs: &World, ctx: &mut Rltk) { && entity_screen_y > 0 && entity_screen_y < map_height { - ctx.set( - entity_screen_x, - entity_screen_y, - render.fg, - render.bg, + draw_batch.set( + Point::new(entity_screen_x, entity_screen_y), + ColorPair::new(render.fg, render.bg), render.glyph, ); } @@ -117,22 +114,22 @@ pub fn render_camera(ecs: &World, ctx: &mut Rltk) { if targets.get(*entity).is_some() { let entity_screen_x = pos.x - min_x; let entity_screen_y = pos.y - min_y; - ctx.set( - entity_screen_x - 1, - entity_screen_y, - colors::RED, - colors::YELLOW, + draw_batch.set( + Point::new(entity_screen_x - 1, entity_screen_y), + ColorPair::new(colors::RED, colors::YELLOW), ::rltk::to_cp437('['), ); - ctx.set( - entity_screen_x + 1, - entity_screen_y, - colors::RED, - colors::YELLOW, + draw_batch.set( + Point::new(entity_screen_x + 1, entity_screen_y), + ColorPair::new(colors::RED, colors::YELLOW), ::rltk::to_cp437(']'), - ) + ); } } + + draw_batch + .submit(0) + .expect("Failed to batch draw the camera"); } pub fn render_debug_map(map: &Map, ctx: &mut Rltk) { @@ -150,11 +147,8 @@ pub fn render_debug_map(map: &Map, ctx: &mut Rltk) { let map_width = map.width - 1; let map_height = map.height - 1; - let mut y = 0; - #[allow(clippy::explicit_counter_loop)] - for ty in min_y..max_y { - let mut x = 0; - for tx in min_x..max_x { + for (y, ty) in (min_y..max_y).enumerate() { + for (x, tx) in (min_x..max_x).enumerate() { if tx > 0 && tx < map_width && ty > 0 && ty < map_height { let idx = map.xy_idx(tx, ty); if map.revealed_tiles[idx] { @@ -164,8 +158,6 @@ pub fn render_debug_map(map: &Map, ctx: &mut Rltk) { } else if SHOW_BOUNDARIES { ctx.set(x, y, colors::GRAY, colors::BLACK, rltk::to_cp437('·')); } - x += 1; } - y += 1; } } diff --git a/src/state.rs b/src/state.rs index 19948e0..f216d67 100644 --- a/src/state.rs +++ b/src/state.rs @@ -582,5 +582,6 @@ impl GameState for State { } damage_system::delete_the_dead(&mut self.ecs); + ::rltk::render_draw_buffer(ctx).expect("Failed to render the Rltk buffer"); } }