Example 7 fixed a size bug, and use deferred rendering.
//////////////////////////////////////////////////////////////
rltk::add_wasm_support!();
-use rltk::{
- Algorithm2D, BaseMap, Console, DistanceAlg, GameState, Point, Rltk, VirtualKeyCode, RGB,
-};
+use rltk::prelude::*;
extern crate rand;
use crate::rand::Rng;
@@ -81,6 +79,7 @@ impl State {
impl GameState for State {
#[allow(non_snake_case)]
fn tick(&mut self, ctx: &mut Rltk) {
+ let mut draw_batch = DrawBatch::new();
match ctx.key {
None => {} // Nothing happened
Some(key) => {
@@ -124,8 +123,8 @@ impl GameState for State {
}
// Clear the screen
- ctx.set_active_console(0);
- ctx.cls();
+ draw_batch.target(0);
+ draw_batch.cls();
// Iterate the map array, incrementing coordinates as we go.
let mut y = 0;
@@ -152,7 +151,7 @@ impl GameState for State {
/ 10.0);
fg = RGB::from_f32(distance, distance, distance);
}
- ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
+ draw_batch.set(Point::new(x,y), ColorPair::new(fg, RGB::from_f32(0., 0., 0.)), glyph);
// Move the coordinates
x += 1;
@@ -164,15 +163,13 @@ impl GameState for State {
// Render the player @ symbol
let ppos = idx_xy(self.player_position);
- ctx.set_active_console(1);
- ctx.cls();
- ctx.set(
- ppos.0,
- ppos.1,
- RGB::from_f32(1.0, 1.0, 1.0),
- RGB::from_f32(0., 0., 0.),
- 2,
- );
+ draw_batch.target(1);
+ draw_batch.cls();
+ draw_batch.set(Point::from_tuple(ppos), ColorPair::new(RGB::from_f32(1.0, 1.0, 1.0), RGB::from_f32(0., 0., 0.)), 2);
+
+ draw_batch.submit();
+
+ render_draw_buffer(ctx);
}
}
@@ -184,17 +181,11 @@ impl BaseMap for State {
fn is_opaque(&self, idx: usize) -> bool {
self.map[idx as usize] == TileType::Wall
}
- fn get_available_exits(&self, _idx: usize) -> Vec<(usize, f32)> {
- Vec::new()
- }
- fn get_pathing_distance(&self, _idx1: usize, _idx2: usize) -> f32 {
- 0.0
- }
}
impl Algorithm2D for State {
fn dimensions(&self) -> Point {
- Point::new(80, 50)
+ Point::new(WIDTH, HEIGHT)
}
}
