toss piston, switch to sdl2. good render performance
This commit is contained in:
parent
3746bb1e6d
commit
4da6c7caa6
File diff suppressed because it is too large
Load Diff
|
@ -7,7 +7,5 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
piston = "0.52.0"
|
sdl2 = { versoion = "0.34.3", features = ["bundled", "static-link"]}
|
||||||
piston2d-graphics = "0.39.0"
|
gl = "0.14.0"
|
||||||
pistoncore-glutin_window = "0.67.0"
|
|
||||||
piston2d-opengl_graphics = "0.76.0"
|
|
||||||
|
|
125
src/main.rs
125
src/main.rs
|
@ -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 sdl2::render::Canvas;
|
||||||
use opengl_graphics::{GlGraphics, OpenGL};
|
use sdl2::pixels::Color;
|
||||||
use piston::event_loop::{EventSettings, Events};
|
use sdl2::video::Window;
|
||||||
use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent};
|
use sdl2::rect::Rect;
|
||||||
use piston::window::WindowSettings;
|
|
||||||
use graphics::Rectangle;
|
|
||||||
|
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
use std::{thread, time};
|
||||||
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
pub const SCREEN_SIZE: u32 = 800;
|
pub const SCREEN_SIZE: u32 = 800;
|
||||||
pub const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
|
pub const PX_SIZE: u32 = 2;
|
||||||
pub const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
pub const BLACK: Color = Color::RGB(0, 0, 0);
|
||||||
|
pub const WHITE: Color = Color::RGB(255, 255, 255);
|
||||||
|
|
||||||
pub trait Cell {
|
pub trait Cell {
|
||||||
fn color(&self) -> [f32; 4];
|
fn color(&self) -> [f32; 4];
|
||||||
|
@ -22,7 +20,7 @@ pub trait Cell {
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ConwayCell {
|
pub struct ConwayCell {
|
||||||
color: [f32; 4],
|
color: Color,
|
||||||
alive: bool,
|
alive: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,28 +32,23 @@ impl Cell {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
gl: GlGraphics,
|
steps_per_sec: u64,
|
||||||
steps_per_sec: f64,
|
px_size: i32,
|
||||||
px_size: u32,
|
|
||||||
dt_last: f64,
|
|
||||||
grid: Vec<Vec<ConwayCell>>,
|
grid: Vec<Vec<ConwayCell>>,
|
||||||
|
canvas: Canvas<Window>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
fn render (&mut self, args: &RenderArgs) {
|
fn render (&mut self) {
|
||||||
use graphics::*;
|
|
||||||
|
|
||||||
let grid = &self.grid;
|
self.canvas.set_draw_color(Color::RGB(0, 0, 0));
|
||||||
let px_size = self.px_size;
|
self.canvas.clear();
|
||||||
|
|
||||||
self.gl.draw(args.viewport(), |c, gl| {
|
for y in 0..self.grid.len() {
|
||||||
// Clear the screen.
|
for x in 0..self.grid[y].len() {
|
||||||
clear(BLACK, gl);
|
if self.grid[y][x].alive {
|
||||||
|
self.canvas.set_draw_color(self.grid[y][x].color);
|
||||||
for y in 0..grid.len() {
|
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 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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,20 +59,11 @@ impl App {
|
||||||
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)
|
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 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn evolve(&mut self) {
|
fn evolve(&mut self) {
|
||||||
let mut new_grid = vec![vec![ConwayCell{color: BLACK, alive: false}; self.grid.len()]; self.grid.len()];
|
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() {
|
fn main() {
|
||||||
// Change this to OpenGL::V2_1 if not working.
|
let sdl_context = sdl2::init().unwrap();
|
||||||
let opengl = OpenGL::V3_2;
|
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];
|
let mut grid = vec![vec![ConwayCell{color: BLACK, alive: false}; size]; size];
|
||||||
|
println!("size:{}", size);
|
||||||
|
|
||||||
// r pemento
|
// r pemento
|
||||||
println!("size:{} size/2:{}", size, size/2);
|
|
||||||
let centery = size/2;
|
let centery = size/2;
|
||||||
let centerx = size/2;
|
let centerx = size/2;
|
||||||
println!("centery:{} centerx:{}", centery, centerx);
|
println!("centery:{} centerx:{}", centery, centerx);
|
||||||
|
@ -150,25 +130,42 @@ fn main() {
|
||||||
grid[centery-1][centerx-1] = ConwayCell{color: WHITE, alive: true};
|
grid[centery-1][centerx-1] = ConwayCell{color: WHITE, alive: true};
|
||||||
grid[centery][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 {
|
let mut app = App {
|
||||||
gl: GlGraphics::new(opengl),
|
steps_per_sec: 32,
|
||||||
steps_per_sec: 30.0,
|
px_size: PX_SIZE as i32,
|
||||||
px_size: px_size,
|
|
||||||
dt_last: 0.0,
|
|
||||||
grid: grid,
|
grid: grid,
|
||||||
|
canvas: canvas,
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{:?}", EventSettings::new());
|
|
||||||
|
|
||||||
let mut events = Events::new(EventSettings::new());
|
let mut frame = 0;
|
||||||
while let Some(e) = events.next(&mut window) {
|
|
||||||
if let Some(args) = e.render_args() {
|
|
||||||
app.render(&args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(args) = e.update_args() {
|
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||||
app.update(&args);
|
'main: loop {
|
||||||
|
for event in event_pump.poll_iter() {
|
||||||
|
match event {
|
||||||
|
sdl2::event::Event::Quit {..} => break 'main,
|
||||||
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue