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
43
44
45
use Window;
use glutin::{Event, EventsLoop, WindowEvent};

/// A game, which is hopefully what you're making.
pub trait Game {
    /// Initialize.
    fn new(&mut Window) -> Self;

    /// Handle an event.
    ///
    /// All input handling goes here.
    fn event(&mut self, &mut Window, Event) {}

    /// Draw a frame.
    ///
    /// Call `Window::draw` inside this function.
    fn draw(&mut self, &mut Window);

    /// Run the game!
    fn run() where Self: Sized {
        let mut events = EventsLoop::new();
        let mut window = Window::new(&events);
        let mut game = Self::new(&mut window);
        let mut running = true;

        while running {
            game.draw(&mut window);
            window.flush();

            events.poll_events(|e| {
                match e {
                    Event::WindowEvent { event: WindowEvent::Resized(..), .. } => {
                        window.resize();
                    }
                    Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
                        running = false;
                    }
                    _ => {}
                }

                game.event(&mut window, e);
            });
        }
    }
}