Refactor camera to batch render

This commit is contained in:
Timothy Warren 2022-02-02 10:08:11 -05:00
parent 14d6f6f7f9
commit e3248519ac
4 changed files with 33 additions and 40 deletions

View File

@ -5,7 +5,6 @@
extern crate lazy_static;
mod ai;
pub mod camera;
mod colors;
mod components;
mod damage_system;

View File

@ -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;

View File

@ -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::<Map>();
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;
}
}

View File

@ -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");
}
}