82 lines
1.9 KiB
Rust
82 lines
1.9 KiB
Rust
use sdl2::render::Canvas;
|
|
use sdl2::pixels::Color;
|
|
use sdl2::video::Window;
|
|
use sdl2::rect::Rect;
|
|
|
|
|
|
use std::cmp;
|
|
|
|
pub mod life;
|
|
|
|
pub const BLACK: Color = Color::RGB(0, 0, 0);
|
|
pub const WHITE: Color = Color::RGB(255, 255, 255);
|
|
|
|
pub struct Grid<T: Clone> {
|
|
size: usize,
|
|
flat_grid: Vec<T>,
|
|
}
|
|
|
|
impl<T> Grid<T>
|
|
where T: Clone
|
|
{
|
|
pub fn new(default: T, size: usize) -> Grid<T> {
|
|
Grid::<T> {size: size, flat_grid: vec![default; size*size]}
|
|
}
|
|
|
|
pub fn get(&self, y: usize, x: usize) -> &T {
|
|
&self.flat_grid[ y*self.size + x ]
|
|
}
|
|
|
|
pub fn set(&mut self, y: usize, x: usize, val: T) {
|
|
self.flat_grid[ y*self.size + x ] = val;
|
|
}
|
|
|
|
pub fn size(&self) -> usize {
|
|
self.size
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Clone)]
|
|
pub struct Cell {
|
|
pub color: Color,
|
|
}
|
|
|
|
pub trait Engine {
|
|
fn evolve(&mut self) -> Grid<Cell>;
|
|
}
|
|
|
|
pub struct Renderer {
|
|
pub px_size: i32,
|
|
pub canvas: Canvas<Window>,
|
|
}
|
|
|
|
impl Renderer {
|
|
pub fn new(px_size: i32, canvas: Canvas<Window>) -> Renderer {
|
|
Renderer { px_size: px_size, canvas: canvas }
|
|
}
|
|
|
|
pub fn render(&mut self, grid: Grid<Cell>) {
|
|
self.canvas.set_draw_color(BLACK);
|
|
self.canvas.clear();
|
|
|
|
for y in 0..grid.size() {
|
|
for x in 0..grid.size() {
|
|
//if grid[y][x].alive {
|
|
self.canvas.set_draw_color(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));
|
|
//}
|
|
}
|
|
}
|
|
|
|
/*
|
|
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();
|
|
}
|
|
} |