From acd5ad13994f673f4881ac03326e00ceb6ef1dc6 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 15 Nov 2021 00:32:27 +0100 Subject: [PATCH] use minifb for rendering --- src/engine.rs | 58 ++++++++++++++++++++++++--------------------------- src/main.rs | 6 ++---- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 811db3f..39d662f 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,6 +1,3 @@ -// extern crate piston_window; -// use piston_window::*; - use std::f64::consts::*; #[derive(Copy, Clone, PartialEq, Debug)] @@ -177,29 +174,26 @@ impl Engine { } pub fn render(&mut self, buffer: &mut Vec) { - // clear([1.0; 4], graphics); + let half = buffer.len() / 2; - // // Ceiling - // let ceiling_color = [0.3, 0.3, 0.3, 1.0]; - // rectangle( - // ceiling_color, - // [0.0, 0.0, self.w, self.h / 2.0], - // context.transform, - // graphics, - // ); + // Ceiling + let ceiling_color = 0x888888; + let ceiling_it = &mut buffer[..half]; + for i in ceiling_it { + *i = ceiling_color; + } - // // Floor - // let floor_color = [0.5, 0.5, 0.5, 1.0]; - // rectangle( - // floor_color, - // [0.0, self.h / 2.0, self.w, self.h / 2.0], - // context.transform, - // graphics, - // ); + // Floor + let floor_color = 0xaaaaaa; + let floor_it = &mut buffer[half..]; + for i in floor_it { + *i = floor_color; + } let left = self.player.angle + (self.horiz_fov / 2.0); let step = self.horiz_fov / self.w; let width = self.w as i32; + let height = self.h as i32; for n in 0..width { let ray_angle = ((left - (n as f64) * step) + 360.0) % 360.0; let ray_radian = ray_angle.to_radians(); @@ -208,22 +202,24 @@ impl Engine { if tile == Tile::Wall { let wall_height = (self.h / (distance * 3.0)).min(self.h); let wall_color = match pos { - p if (p.x.trunc() + p.y.trunc()) % 4.0 == 0.0 => [0.2, 0.2, 0.9, 1.0], - p if (p.x.trunc() + p.y.trunc()) % 4.0 == 1.0 => [0.4, 0.4, 0.9, 1.0], - p if (p.x.trunc() + p.y.trunc()) % 4.0 == 2.0 => [0.6, 0.6, 0.9, 1.0], - p if (p.x.trunc() + p.y.trunc()) % 4.0 == 3.0 => [0.7, 0.3, 0.9, 1.0], - _ => [1.0, 0.0, 0.0, 1.0], + p if (p.x.trunc() + p.y.trunc()) % 4.0 == 0.0 => 0x3333ee, //[0.2, 0.2, 0.9, 1.0], + p if (p.x.trunc() + p.y.trunc()) % 4.0 == 1.0 => 0x6666ee, //[0.4, 0.4, 0.9, 1.0], + p if (p.x.trunc() + p.y.trunc()) % 4.0 == 2.0 => 0x9999ee, //[0.6, 0.6, 0.9, 1.0], + p if (p.x.trunc() + p.y.trunc()) % 4.0 == 3.0 => 0xaa55ee, //[0.7, 0.3, 0.9, 1.0], + _ => 0xff0000, }; println!( "ray: {}, angle: {}, wall at {:?}, distance: {}", n, ray_angle, pos, distance ); - // rectangle( - // wall_color, - // [n as f64, (self.h - wall_height) / 2.0, 1.0, wall_height], - // context.transform, - // graphics, - // ); + let gap = (self.h - wall_height) as i32; + let wall_range = (gap / 2)..(height - (gap / 2)); + let start_index = n + (gap / 2) * width; + let end_index = n + (height - (gap / 2)); + for i in wall_range { + let buf_index = (n + i * width) as usize; + buffer[buf_index] = wall_color; + } }; } diff --git a/src/main.rs b/src/main.rs index e8f9c62..5f9d2c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,3 @@ -// extern crate piston_window; -// use piston_window::*; - extern crate minifb; use minifb::{Key, Window, WindowOptions}; @@ -40,8 +37,9 @@ fn main() { engine.load_level(level); while window.is_open() && !window.is_key_down(Key::Escape) { + // Reset framebuffer for i in buffer.iter_mut() { - *i = 0; // write something more funny here! + *i = 0; } // if let Some(Button::Keyboard(key)) = event.press_args() {