refactor sdl into lib observer. fix warns

This commit is contained in:
Dan Ballard 2021-02-22 22:12:35 -08:00
parent b8b17349b3
commit 351b8e9507
3 changed files with 74 additions and 78 deletions

View File

@ -3,8 +3,8 @@ use sdl2::pixels::Color;
use sdl2::video::Window; use sdl2::video::Window;
use sdl2::rect::Rect; use sdl2::rect::Rect;
use std::{thread, time};
use std::cmp; use std::time::{SystemTime};
pub mod life; pub mod life;
@ -38,43 +38,88 @@ impl<T> Grid<T>
#[derive(Clone)] #[derive(Clone)]
pub struct Cell { pub struct Patch {
pub color: Color, pub color: Color,
} }
pub trait Engine { pub trait Engine {
fn evolve(&mut self) -> Grid<Cell>; fn evolve(&mut self) -> Grid<Patch>;
} }
pub struct Observer { pub struct Observer {
pub px_size: i32, px_size: i32,
pub canvas: Canvas<Window>, canvas: Canvas<Window>,
sdl_context: sdl2::Sdl,
render_grid: Grid<Patch>,
} }
impl Observer { impl Observer {
pub fn new(px_size: i32, canvas: Canvas<Window>) -> Observer { pub fn new(px_size: i32, num_patches: u32) -> Observer {
Observer { px_size: px_size, canvas: canvas } let size: usize = (num_patches/px_size as u32) as usize;
println!("Window: {}x{} at {} px per cell", num_patches, num_patches, px_size);
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("Microworlds", num_patches, num_patches).build().unwrap();
// setup initial black render grid
let render_grid = Grid::new(Patch {color: BLACK}, size);
// Let's create a Canvas which we will use to draw in our Window
let canvas : Canvas<Window> = window.into_canvas()
.present_vsync() //< this means the screen cannot
// render faster than your display rate (usually 60Hz or 144Hz)
.build().unwrap();
Observer { px_size: px_size, canvas: canvas, sdl_context: sdl_context, render_grid: render_grid }
} }
pub fn render(&mut self, grid: Grid<Cell>) { pub fn sim(&mut self, mut engine: Box<dyn Engine>, steps_per_sec: u64) {
let mut frame = 0;
let mut event_pump = self.sdl_context.event_pump().unwrap();
let period = time::Duration::from_millis(1000 / steps_per_sec);
'main: loop {
for event in event_pump.poll_iter() {
match event {
sdl2::event::Event::Quit {..} => break 'main,
_ => {},
}
}
let start_rend = SystemTime::now();
self.render();
let render_time = start_rend.elapsed().unwrap();
let start_evo = SystemTime::now();
self.render_grid = engine.evolve();
let evo_time = start_evo.elapsed().unwrap();
let total_time = start_rend.elapsed().unwrap();
frame += 1;
println!("frame: {} - render time: {:?} | evolve time: {:?} | total time: {:?}", frame, render_time, evo_time, total_time);
if total_time < period {
thread::sleep(period - total_time);
}
}
}
fn render(&mut self) {
self.canvas.set_draw_color(BLACK); self.canvas.set_draw_color(BLACK);
self.canvas.clear(); self.canvas.clear();
for y in 0..grid.size() { for y in 0..self.render_grid.size() {
for x in 0..grid.size() { for x in 0..self.render_grid.size() {
self.canvas.set_draw_color(grid.get(y, x).color); self.canvas.set_draw_color(self.render_grid.get(y, x).color);
self.canvas.fill_rect(Rect::new(x as i32 * self.px_size, y as i32 * self.px_size, self.px_size as u32, self.px_size as u32)); match self.canvas.fill_rect(Rect::new(x as i32 * self.px_size, y as i32 * self.px_size, self.px_size as u32, self.px_size as u32)) {
Ok(()) => (),
Err(err) => {
println!("Error on canvas.fill_rect: {}", err);
return;
}
};
} }
} }
/*
for (y, row) in grid.iter().enumerate() {
for (x, cell) in row.iter().enumerate() {
rectangle( cell.color, rectangle::square((x as u32 * px_size) as f64, (y as u32 * px_size) as f64, px_size as f64), c.transform, gl)
}
}*/
self.canvas.present(); self.canvas.present();
} }
} }

View File

@ -1,6 +1,4 @@
use std::cmp; use super::{Patch, Engine, Grid, WHITE, BLACK};
use super::{Cell, Engine, Grid, WHITE, BLACK};
pub struct LifeEngine { pub struct LifeEngine {
grid: Grid<bool>, grid: Grid<bool>,
@ -75,8 +73,8 @@ impl LifeEngine {
} }
impl Engine for LifeEngine { impl Engine for LifeEngine {
fn evolve(&mut self) -> super::Grid<Cell> { fn evolve(&mut self) -> super::Grid<Patch> {
let mut new_render_grid = super::Grid::new(Cell{color: BLACK}, self.grid.size()); let mut new_render_grid = super::Grid::new(Patch {color: BLACK}, self.grid.size());
let mut new_life_grid = Grid::new(false, self.grid.size()); let mut new_life_grid = Grid::new(false, self.grid.size());
for y in 0..self.grid.size() { for y in 0..self.grid.size() {
@ -85,11 +83,11 @@ impl Engine for LifeEngine {
if *self.grid.get(y, x) { if *self.grid.get(y, x) {
if nalive == 2 || nalive == 3 { if nalive == 2 || nalive == 3 {
new_render_grid.set(y, x, Cell{color: WHITE}); new_render_grid.set(y, x, Patch {color: WHITE});
new_life_grid.set(y, x, true); new_life_grid.set(y, x, true);
} }
} else if nalive == 3 { } else if nalive == 3 {
new_render_grid.set(y, x, Cell{color: WHITE}); new_render_grid.set(y, x, Patch {color: WHITE});
new_life_grid.set(y, x, true); new_life_grid.set(y, x, true);
} }
} }

View File

@ -1,13 +1,4 @@
use clap::{App, Arg}; use clap::{App, Arg};
use sdl2::render::Canvas;
use sdl2::video::Window;
use std::{thread, time};
use std::time::{SystemTime};
use microworlds::Engine;
use microworlds::Grid;
pub const SCREEN_SIZE_DEFAULT: u32 = 800; pub const SCREEN_SIZE_DEFAULT: u32 = 800;
pub const PX_SIZE_DEFAULT: u32 = 2; pub const PX_SIZE_DEFAULT: u32 = 2;
@ -58,7 +49,7 @@ fn main() {
}; };
let size: usize = (num_patches/px_size) as usize; let size: usize = (num_patches/px_size) as usize;
let mut engine; let engine;
match matches.value_of("engine") { match matches.value_of("engine") {
Some(arg) => match arg { Some(arg) => match arg {
@ -74,45 +65,7 @@ fn main() {
} }
} }
let sdl_context = sdl2::init().unwrap(); let mut observer= microworlds::Observer::new(px_size as i32, num_patches);
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("Microworlds", num_patches, num_patches).build().unwrap();
println!("Window: {}x{} at {} px per cell", num_patches, num_patches, px_size);
let mut render_grid = Grid::new(microworlds::Cell{color: microworlds::BLACK}, size);
println!("cell grid: {}x{} ({} cells) - {} steps per sec", size, size, (size*size), steps); println!("cell grid: {}x{} ({} cells) - {} steps per sec", size, size, (size*size), steps);
observer.sim(engine, steps);
// Let's create a Canvas which we will use to draw in our Window
let canvas : Canvas<Window> = window.into_canvas()
.present_vsync() //< this means the screen cannot
// render faster than your display rate (usually 60Hz or 144Hz)
.build().unwrap();
let mut renderer= microworlds::Observer::new(px_size as i32, canvas);
let mut frame = 0;
let mut event_pump = sdl_context.event_pump().unwrap();
let period = time::Duration::from_millis(1000 / steps);
'main: loop {
for event in event_pump.poll_iter() {
match event {
sdl2::event::Event::Quit {..} => break 'main,
_ => {},
}
}
let start_rend = SystemTime::now();
renderer.render(render_grid);
let render_time = start_rend.elapsed().unwrap();
let start_evo = SystemTime::now();
render_grid = engine.evolve();
let evo_time = start_evo.elapsed().unwrap();
let total_time = start_rend.elapsed().unwrap();
frame += 1;
println!("frame: {} - render time: {:?} | evolve time: {:?} | total time: {:?}", frame, render_time, evo_time, total_time);
if total_time < period {
thread::sleep(period - total_time);
}
}
} }