toss piston, switch to sdl2. good render performance

This commit is contained in:
Dan Ballard 2021-01-02 18:12:59 -08:00
parent 3746bb1e6d
commit 4da6c7caa6
3 changed files with 157 additions and 1206 deletions

1216
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
piston = "0.52.0"
piston2d-graphics = "0.39.0"
pistoncore-glutin_window = "0.67.0"
piston2d-opengl_graphics = "0.76.0"
sdl2 = { versoion = "0.34.3", features = ["bundled", "static-link"]}
gl = "0.14.0"

View File

@ -1,20 +1,18 @@
extern crate glutin_window;
extern crate graphics;
extern crate opengl_graphics;
extern crate piston;
use glutin_window::GlutinWindow as Window;
use opengl_graphics::{GlGraphics, OpenGL};
use piston::event_loop::{EventSettings, Events};
use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent};
use piston::window::WindowSettings;
use graphics::Rectangle;
use sdl2::render::Canvas;
use sdl2::pixels::Color;
use sdl2::video::Window;
use sdl2::rect::Rect;
use std::cmp;
use std::{thread, time};
use std::time::{Duration, SystemTime};
pub const SCREEN_SIZE: u32 = 800;
pub const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
pub const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
pub const PX_SIZE: u32 = 2;
pub const BLACK: Color = Color::RGB(0, 0, 0);
pub const WHITE: Color = Color::RGB(255, 255, 255);
pub trait Cell {
fn color(&self) -> [f32; 4];
@ -22,7 +20,7 @@ pub trait Cell {
#[derive(Clone)]
pub struct ConwayCell {
color: [f32; 4],
color: Color,
alive: bool,
}
@ -34,53 +32,39 @@ impl Cell {
}*/
pub struct App {
gl: GlGraphics,
steps_per_sec: f64,
px_size: u32,
dt_last: f64,
steps_per_sec: u64,
px_size: i32,
grid: Vec<Vec<ConwayCell>>,
canvas: Canvas<Window>,
}
impl App {
fn render (&mut self, args: &RenderArgs) {
use graphics::*;
fn render (&mut self) {
let grid = &self.grid;
let px_size = self.px_size;
self.canvas.set_draw_color(Color::RGB(0, 0, 0));
self.canvas.clear();
self.gl.draw(args.viewport(), |c, gl| {
// Clear the screen.
clear(BLACK, gl);
for y in 0..grid.len() {
for x in 0..grid[y].len() {
if grid[y][x].alive {
rectangle(grid[y][x].color, rectangle::square((x as u32 * px_size) as f64, (y as u32 * px_size) as f64, px_size as f64), c.transform, gl)
}
for y in 0..self.grid.len() {
for x in 0..self.grid[y].len() {
if self.grid[y][x].alive {
self.canvas.set_draw_color(self.grid[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));
}
}
/*
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)
}
}*/
});
}
fn update(&mut self, args: &UpdateArgs) {
//println!("update and update.dt: {} with self.dt_last: {}", args.dt, self.dt_last);
if args.dt + self.dt_last > (1.0 / self.steps_per_sec) {
self.dt_last = 0.0;
print!(">");
self.evolve();
} else {
print!(".");
self.dt_last += args.dt;
}
/*
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();
}
fn evolve(&mut self) {
let mut new_grid = vec![vec![ConwayCell{color: BLACK, alive: false}; self.grid.len()]; self.grid.len()];
@ -125,21 +109,17 @@ impl App {
}
fn main() {
// Change this to OpenGL::V2_1 if not working.
let opengl = OpenGL::V3_2;
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("Microworlds", SCREEN_SIZE, SCREEN_SIZE).build().unwrap();
// Create an Glutin window.
let mut window: Window = WindowSettings::new("spinning-square", [SCREEN_SIZE, SCREEN_SIZE])
.graphics_api(opengl)
.exit_on_esc(true)
.build()
.unwrap();
let px_size = 2;
let size: usize = (SCREEN_SIZE/px_size) as usize;
let size: usize = (SCREEN_SIZE/PX_SIZE) as usize;
let mut grid = vec![vec![ConwayCell{color: BLACK, alive: false}; size]; size];
println!("size:{}", size);
// r pemento
println!("size:{} size/2:{}", size, size/2);
let centery = size/2;
let centerx = size/2;
println!("centery:{} centerx:{}", centery, centerx);
@ -150,25 +130,42 @@ fn main() {
grid[centery-1][centerx-1] = ConwayCell{color: WHITE, alive: true};
grid[centery][centerx+1] = ConwayCell{color: WHITE, alive: true};
// 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 app = App {
gl: GlGraphics::new(opengl),
steps_per_sec: 30.0,
px_size: px_size,
dt_last: 0.0,
steps_per_sec: 32,
px_size: PX_SIZE as i32,
grid: grid,
canvas: canvas,
};
println!("{:?}", EventSettings::new());
let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
if let Some(args) = e.render_args() {
app.render(&args);
let mut frame = 0;
let mut event_pump = sdl_context.event_pump().unwrap();
'main: loop {
for event in event_pump.poll_iter() {
match event {
sdl2::event::Event::Quit {..} => break 'main,
_ => {},
}
}
if let Some(args) = e.update_args() {
app.update(&args);
}
let start_rend = SystemTime::now();
app.render();
let render_time = start_rend.elapsed().unwrap();
let start_evo = SystemTime::now();
app.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);
thread::sleep(time::Duration::from_millis(1000 / app.steps_per_sec));
}
}