use minifb for rendering

This commit is contained in:
Fabien Freling 2021-11-15 00:32:27 +01:00
parent 3844c0fe80
commit acd5ad1399
2 changed files with 29 additions and 35 deletions

View file

@ -1,6 +1,3 @@
// extern crate piston_window;
// use piston_window::*;
use std::f64::consts::*; use std::f64::consts::*;
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
@ -177,29 +174,26 @@ impl Engine {
} }
pub fn render(&mut self, buffer: &mut Vec<u32>) { pub fn render(&mut self, buffer: &mut Vec<u32>) {
// clear([1.0; 4], graphics); let half = buffer.len() / 2;
// // Ceiling // Ceiling
// let ceiling_color = [0.3, 0.3, 0.3, 1.0]; let ceiling_color = 0x888888;
// rectangle( let ceiling_it = &mut buffer[..half];
// ceiling_color, for i in ceiling_it {
// [0.0, 0.0, self.w, self.h / 2.0], *i = ceiling_color;
// context.transform, }
// graphics,
// );
// // Floor // Floor
// let floor_color = [0.5, 0.5, 0.5, 1.0]; let floor_color = 0xaaaaaa;
// rectangle( let floor_it = &mut buffer[half..];
// floor_color, for i in floor_it {
// [0.0, self.h / 2.0, self.w, self.h / 2.0], *i = floor_color;
// context.transform, }
// graphics,
// );
let left = self.player.angle + (self.horiz_fov / 2.0); let left = self.player.angle + (self.horiz_fov / 2.0);
let step = self.horiz_fov / self.w; let step = self.horiz_fov / self.w;
let width = self.w as i32; let width = self.w as i32;
let height = self.h as i32;
for n in 0..width { for n in 0..width {
let ray_angle = ((left - (n as f64) * step) + 360.0) % 360.0; let ray_angle = ((left - (n as f64) * step) + 360.0) % 360.0;
let ray_radian = ray_angle.to_radians(); let ray_radian = ray_angle.to_radians();
@ -208,22 +202,24 @@ impl Engine {
if tile == Tile::Wall { if tile == Tile::Wall {
let wall_height = (self.h / (distance * 3.0)).min(self.h); let wall_height = (self.h / (distance * 3.0)).min(self.h);
let wall_color = match pos { 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 == 0.0 => 0x3333ee, //[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 == 1.0 => 0x6666ee, //[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 == 2.0 => 0x9999ee, //[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], p if (p.x.trunc() + p.y.trunc()) % 4.0 == 3.0 => 0xaa55ee, //[0.7, 0.3, 0.9, 1.0],
_ => [1.0, 0.0, 0.0, 1.0], _ => 0xff0000,
}; };
println!( println!(
"ray: {}, angle: {}, wall at {:?}, distance: {}", "ray: {}, angle: {}, wall at {:?}, distance: {}",
n, ray_angle, pos, distance n, ray_angle, pos, distance
); );
// rectangle( let gap = (self.h - wall_height) as i32;
// wall_color, let wall_range = (gap / 2)..(height - (gap / 2));
// [n as f64, (self.h - wall_height) / 2.0, 1.0, wall_height], let start_index = n + (gap / 2) * width;
// context.transform, let end_index = n + (height - (gap / 2));
// graphics, for i in wall_range {
// ); let buf_index = (n + i * width) as usize;
buffer[buf_index] = wall_color;
}
}; };
} }

View file

@ -1,6 +1,3 @@
// extern crate piston_window;
// use piston_window::*;
extern crate minifb; extern crate minifb;
use minifb::{Key, Window, WindowOptions}; use minifb::{Key, Window, WindowOptions};
@ -40,8 +37,9 @@ fn main() {
engine.load_level(level); engine.load_level(level);
while window.is_open() && !window.is_key_down(Key::Escape) { while window.is_open() && !window.is_key_down(Key::Escape) {
// Reset framebuffer
for i in buffer.iter_mut() { for i in buffer.iter_mut() {
*i = 0; // write something more funny here! *i = 0;
} }
// if let Some(Button::Keyboard(key)) = event.press_args() { // if let Some(Button::Keyboard(key)) = event.press_args() {