use ggez::event::{self, EventHandler}; use ggez::graphics::{self, Color, DrawMode, Mesh, MeshBuilder}; use ggez::{Context, ContextBuilder, GameResult}; struct Apr { row: i32, col: i32, board: Vec>, grid: Mesh, should_update_grid: bool, // Your state here... __TEST_state: usize } fn make_grid(ctx: &mut Context, r: i32, c: i32, board: &[Vec]) -> GameResult { let start_x: f32 = 20.0; let start_y: f32 = 20.0; let size: f32 = 4.0; //compile time constants for now let mut builder = MeshBuilder::new(); for row in 0..r { for col in 0..c { builder.rectangle( DrawMode::fill(), graphics::Rect { x: start_x + (size * col as f32), y: start_y + (size * row as f32), w: size, h: size, }, match board[row as usize][col as usize] { 0 => Color::WHITE, 1 => Color::BLACK, 2 => Color::RED, 3 => Color::CYAN, 4 => Color::YELLOW, 5 => Color::GREEN, 6 => Color::BLUE, 7 => Color::MAGENTA, _ => Color::BLACK, } )?; } } builder.build(ctx) } fn make_board(r: i32, c: i32) -> Vec> { let mut init : Vec> = vec![vec![0u8; r as usize]; c as usize]; for row in 0..r as usize { for col in 0..c as usize { init[row][col] = (row + col) as u8 % 8; } } init } impl Apr { pub fn new(_ctx: &mut Context, r: i32, c: i32) -> Apr { // Load/create resources such as images here. let board = make_board(r, c); Apr { row: r, col: c, grid: make_grid(_ctx, r, c, board.as_slice()).unwrap(), board: board, should_update_grid: false, __TEST_state: 0, } } fn set_colour(&mut self, r : usize, c : usize, col : u8) -> GameResult<()> { self.board[r][c] = col; self.should_update_grid = true; Ok(()) } } impl EventHandler for Apr { fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { // Err(ggez::GameError::CustomError("You done messed up trying to play this game!".to_string())) // Update code here... for _x in 0..self.row*self.col { let c = self.__TEST_state % self.col as usize; let r = (self.__TEST_state / self.row as usize) % self.row as usize; self.set_colour(r, c, (self.board[r][c] + 1) % 8)?; self.__TEST_state += 1; } Ok(()) } fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { graphics::clear(ctx, Color::new(0.5, 0.5, 0.5, 1.0)); let drawparams = graphics::DrawParam::new(); if self.should_update_grid { self.grid = make_grid(ctx, self.row, self.col, self.board.as_slice())?; self.should_update_grid = false; } graphics::draw(ctx, &self.grid, drawparams)?; // Draw code here... graphics::present(ctx) } } fn main() { // Make a Context. let (mut ctx, event_loop) = ContextBuilder::new("apruebo", "s1m7u and e-dt") .build() .expect("aieee, could not create ggez context!"); // Create an instance of your event handler. // Usually, you should provide it with the Context object to // use when setting your game up. let apr = Apr::new(&mut ctx, 128, 128); // Run! event::run(ctx, event_loop, apr); }