Skip to content

Instantly share code, notes, and snippets.

@Datoh
Last active March 5, 2020 16:53
Show Gist options
  • Save Datoh/170c3f29a67a24ecc3381180dbad3590 to your computer and use it in GitHub Desktop.
Save Datoh/170c3f29a67a24ecc3381180dbad3590 to your computer and use it in GitHub Desktop.
// Draw in an offline image and a create a spritebatch
use ggez::{Context, event, graphics, nalgebra, GameResult};
struct MainState {
rect_size: f32,
sprite_count: usize,
spritebatch: graphics::spritebatch::SpriteBatch,
}
const RECT_SIZE: f32 = 64.0;
impl MainState {
fn new(ctx: &mut Context, rect_size: f32) -> GameResult<MainState> {
// Draw four colored squares in a row
let red = graphics::Color::new(1.0, 0.0, 0.0, 1.0);
let green = graphics::Color::new(0.0, 1.0, 0.0, 1.0);
let blue = graphics::Color::new(0.0, 0.0, 1.0, 1.0);
let yellow = graphics::Color::new(1.0, 1.0, 0.0, 1.0);
let colors = vec![red, green, blue, yellow];
let sprite_count = colors.len();
let width: f32 = rect_size * colors.len() as f32;
let height: f32 = rect_size;
// update temporarely the screen coordinate of the context to fit to image size
let saved_screen_coordinates = graphics::screen_coordinates(ctx);
graphics::set_screen_coordinates(ctx, graphics::Rect { x: 0.0, y: 0.0, w: width, h: height})?;
// Create a new canvas
let canvas = graphics::Canvas::new(ctx, width as u16, height as u16, ggez::conf::NumSamples::One)?;
graphics::set_canvas(ctx, Some(&canvas));
// Draw rectangles
let mut index: usize = 0;
for color in colors {
let rect = graphics::Mesh::new_rectangle(ctx, graphics::DrawMode::fill(), graphics::Rect::new(0.0, 0.0, rect_size, rect_size), color)?;
graphics::draw(ctx, &rect, (nalgebra::Point2::new(index as f32 * rect_size, 0.0),))?;
index = index + 1;
}
graphics::set_canvas(ctx, None);
// Drop the canvas image in a spritebatch
let spritebatch = graphics::spritebatch::SpriteBatch::new(canvas.into_inner());
// Restore the screen coordinates of the context
graphics::set_screen_coordinates(ctx, saved_screen_coordinates)?;
Ok(MainState { rect_size, sprite_count, spritebatch })
}
}
impl event::EventHandler for MainState {
fn update(&mut self, _ctx: &mut Context) -> GameResult {
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, graphics::BLACK);
// Draw square vertically
self.spritebatch.clear();
for i in 0..self.sprite_count {
self.spritebatch.add(graphics::DrawParam::new()
.src(graphics::Rect::new(i as f32 * self.rect_size, 0.0, self.rect_size, self.rect_size))
.dest(nalgebra::Point2::new(0.0, i as f32 * self.rect_size)));
}
graphics::draw(
ctx,
&self.spritebatch,
graphics::DrawParam::new().dest(nalgebra::Point2::new(0.0, 0.0)),
)?;
graphics::present(ctx)?;
Ok(())
}
}
pub fn main() -> GameResult {
let cb = ggez::ContextBuilder::new("draw in image", "ggez");
let (ctx, event_loop) = &mut cb.build()?;
let state = &mut MainState::new(ctx, RECT_SIZE)?;
event::run(ctx, event_loop, state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment