summaryrefslogtreecommitdiff
path: root/client/src/window.rs
blob: 20300d5c1d6866a30665ca3fad348b895fdb625b (plain)
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 crate::renderer::Renderer;
use winit::{
    application::ApplicationHandler,
    event::WindowEvent,
    event_loop::ActiveEventLoop,
    window::{Window, WindowAttributes, WindowId},
};

pub struct WindowState {
    window: Option<(Window, Renderer<'static>)>,
}
impl WindowState {
    pub fn new() -> Self {
        Self { window: None }
    }
}
impl ApplicationHandler for WindowState {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let win = event_loop
            .create_window(WindowAttributes::default().with_maximized(true))
            .unwrap();
        let ren = Renderer::new(unsafe { std::mem::transmute::<&Window, &'static Window>(&win) })
            .unwrap();
        self.window = Some((win, ren))
    }
    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _window_id: WindowId,
        event: WindowEvent,
    ) {
        if let Some((_win, ren)) = &mut self.window {
            match event {
                WindowEvent::Resized(size) => {
                    ren.resize(size.width, size.height);
                }
                WindowEvent::RedrawRequested => ren.draw().unwrap(),
                WindowEvent::CloseRequested => {
                    event_loop.exit();
                }
                _ => (),
            }
        }
    }
}