pixel_offscreen/
pixel_offscreen.rs

1use ggez::{
2    conf::WindowMode,
3    event::{self, EventHandler},
4    glam::*,
5    graphics::{Canvas, Color},
6    Context, ContextBuilder, GameResult,
7};
8
9use pixel_handler::{
10    pixel_handler::PixelHandler,
11    structs::{grid_position::GridPosition, pixel::Pixel},
12};
13
14const CELL_SIZE: (f32, f32) = (15.0, 15.0);
15
16struct MainState {
17    pixel_handler: PixelHandler,
18}
19
20impl MainState {
21    pub fn new(_ctx: &mut Context) -> MainState {
22        let mut pixel_handler = PixelHandler::new(CELL_SIZE);
23
24        let pixel = Pixel::new(GridPosition::new(10, 10, CELL_SIZE), Color::BLUE);
25        pixel_handler.register_pixel(pixel);
26
27        MainState { pixel_handler }
28    }
29}
30
31impl EventHandler for MainState {
32    fn update(&mut self, _ctx: &mut Context) -> GameResult {
33        Ok(())
34    }
35
36    fn draw(&mut self, ctx: &mut Context) -> GameResult {
37        let mut canvas = Canvas::from_frame(ctx, Color::WHITE);
38        let pixel_handler = &mut self.pixel_handler;
39
40        pixel_handler.draw_grid(ctx, Color::BLACK);
41        pixel_handler.display_fps(ctx);
42
43        for (_position, pixel) in pixel_handler.pixels.iter_mut() {
44            let next_position = pixel.position + GridPosition::new(0, 1, CELL_SIZE);
45
46            if next_position.is_offscreen(ctx) {
47                continue;
48            } else {
49                pixel.position = next_position;
50            }
51        }
52
53        pixel_handler.update(&mut canvas, ctx);
54        canvas.finish(ctx)
55    }
56}
57
58fn main() {
59    let (mut ctx, event_loop) = ContextBuilder::new("Simple Render", "")
60        .window_setup(ggez::conf::WindowSetup::default().title("Pixel Handler"))
61        .build()
62        .expect("Could not create ggez context");
63
64    let state = MainState::new(&mut ctx);
65
66    ctx.gfx
67        .set_mode(WindowMode {
68            resizable: true,
69            min_height: 350.0,
70            min_width: 350.0,
71
72            ..Default::default()
73        })
74        .unwrap();
75
76    event::run(ctx, event_loop, state);
77}