Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save beatgammit/0fb04d4ebed54a658aba607ab0916f47 to your computer and use it in GitHub Desktop.
Save beatgammit/0fb04d4ebed54a658aba607ab0916f47 to your computer and use it in GitHub Desktop.
Slow rect render with ggez
extern crate ggez;
use std::io;
use std::time::Instant;
use ggez::{event, graphics, Context, GameResult};
struct GameState;
impl event::EventHandler for GameState {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
let start = Instant::now();
graphics::clear(ctx);
for x in 0..50 {
for y in 0..50 {
let rect = graphics::Rect::new_i32(x*10, y*10, x*10+10, y*10+10);
graphics::set_color(ctx, [(x as f32) / 500.0, 0.0, (y as f32) / 500.0, 1.0].into())?;
graphics::rectangle(ctx, graphics::DrawMode::Fill, rect)?;
}
}
graphics::present(ctx);
println!("Drawing took: {}ms", start.elapsed().subsec_millis());
Ok(())
}
}
fn main() -> io::Result<()> {
let ctx = &mut ggez::ContextBuilder::new("hello", "Hello!!")
.window_setup(ggez::conf::WindowSetup::default().title("Hello!"))
.window_mode(ggez::conf::WindowMode::default().dimensions(500, 500))
.build()
.expect("Failed to build ggez context");
graphics::set_background_color(ctx, [0.0, 0.0, 0.0, 1.0].into());
let state = &mut GameState{};
match event::run(ctx, state) {
Err(e) => println!("Error encountered running game: {}", e),
Ok(_) => println!("Game exited cleanly!"),
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment