aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: faa5b7d774b213f8bf27acf02fabdaa34fef12d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use piston_window::grid::Grid;
use piston_window::*;
use sdl2_window::Sdl2Window;

pub const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 0.8];

fn main() {
    let mut window: PistonWindow<Sdl2Window> = WindowSettings::new("Hello there!", [1000, 1000])
        .build()
        .expect("Could not initialise window");

    let mut pixels_per_m = 64.;

    let grid_line = Line::new(BLACK, 1.5);
    let mut events = Events::new(EventSettings::new().lazy(true));
    while let Some(e) = events.next(&mut window) {
        e.mouse_scroll(|[_, y]| {
            if y < 0. {
                pixels_per_m *= 1.2;
            } else if y > 0. {
                pixels_per_m /= 1.2;
            }
        });

        if let Some(Button::Keyboard(Key::Escape)) = e.press_args() {
            window.set_should_close(true);
        }

        let win_size = window.draw_size();
        let grid = Grid {
            cols: (win_size.width / pixels_per_m) as u32 + 1,
            rows: (win_size.height / pixels_per_m) as u32 + 1,
            units: pixels_per_m,
        };

        window.draw_2d(&e, |c, g, _device| {
            clear([1.0; 4], g);

            grid.draw(&grid_line, &c.draw_state, c.transform, g);
        });
    }
}