Browse Source

simple test grid

master
e-dt 4 years ago
parent
commit
90301d28bf
  1. 63
      src/main.rs

63
src/main.rs

@ -1,41 +1,50 @@
use ggez::{Context, ContextBuilder, GameResult}; use ggez::{Context, ContextBuilder, GameResult};
use ggez::graphics::{self, Color}; use ggez::graphics::{self, Color, Mesh, MeshBuilder, DrawMode};
use ggez::event::{self, EventHandler}; use ggez::event::{self, EventHandler};
struct Apr { struct Apr {
grid : Mesh,
// Your state here... // Your state here...
} }
fn make_grid(ctx : &mut Context, r: i32, c: i32) -> GameResult<Mesh> {
let start_x : f32 = 20.0;
let start_y : f32 = 20.0;
let size : f32 = 20.0; //compile time constants for now
let mut start_white : bool = true;
let mut builder = MeshBuilder::new();
for row in 0..r {
let mut colour = start_white;
for col in 0..c {
builder.rectangle(
if colour { DrawMode::stroke(1.0) } else { DrawMode::fill() },
graphics::Rect {
x : start_x + (size * col as f32),
y : start_y + (size * row as f32),
w : size,
h : size
},
Color::BLACK)?;
colour = !colour;
}
start_white = !start_white;
}
builder.build(ctx)
}
impl Apr { impl Apr {
pub fn new(_ctx: &mut Context) -> Apr { pub fn new(_ctx: &mut Context) -> Apr {
// Load/create resources such as images here. // Load/create resources such as images here.
Apr { Apr {
// ... grid : make_grid(_ctx, 8, 8).unwrap()
} }
} }
fn make_grid(&self, ctx : &mut Context, r: i32, c: i32) -> GameResult<()> {
let start_x : f32 = 20.0;
let start_y : f32 = 20.0;
let size : f32 = 20.0; //compile time constants for now
let mut start_white : bool = true;
for row in 0..r {
let mut colour = start_white;
for col in 0..c {
graphics::rectangle(ctx,
if (colour) { graphics::DrawMode::Line } else { graphics::DrawMode::Fill },
graphics::Rect {
x : start_x + (size * col),
y : start_y + (size * row),
w : size,
h : size
})?;
colour = !colour;
}
start_white = !start_white;
}
}
} }
@ -43,14 +52,16 @@ impl Apr {
impl EventHandler for Apr { impl EventHandler for Apr {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
// Err(ggez::GameError::CustomError("You done messed up trying to play this game!".to_string()))
Err(ggez::GameError::CustomError("You done messed up trying to play this game!".to_string()))
// Update code here... // Update code here...
// Ok(()) Ok(())
} }
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
graphics::clear(ctx, Color::WHITE); graphics::clear(ctx, Color::WHITE);
let drawparams = graphics::DrawParam::new();
graphics::draw(ctx, &self.grid, drawparams)?;
// Draw code here... // Draw code here...
graphics::present(ctx) graphics::present(ctx)
} }

Loading…
Cancel
Save