qqx/window/
builder.rs

1use crate::Vec2;
2use super::Window;
3use glium::{
4    Display,
5    glutin::{
6        self,
7        dpi::PhysicalSize,
8        window::WindowId
9    }
10};
11
12static mut WINDOWS: Vec <Display> = Vec::new();
13
14pub struct WindowBuilder {
15    size: Option <Vec2 <u32>>,
16    title: String
17}
18
19impl Default for WindowBuilder {
20    fn default() -> Self {
21        Self {
22            size: None,
23            title: String::from("qqx")
24        }
25    }
26}
27
28impl WindowBuilder {
29    #[inline]
30    pub fn size(mut self, size: Vec2 <u32>) -> Self {
31        self.size = Some(size);
32        self
33    }
34
35    #[inline]
36    pub fn title <S> (mut self, title: S) -> Self where S: ToString {
37        self.title = title.to_string();
38        self
39    }
40
41    pub fn build(self) -> Window {
42        let mut builder = glutin::window::WindowBuilder::new().with_title(self.title);
43        if let Some(size) = self.size { builder = builder.with_inner_size(PhysicalSize::from(size)) }
44
45        let dpy = Display::new(builder, glutin::ContextBuilder::new(), crate::Stt::eventloop()).unwrap();
46
47        unsafe {
48            WINDOWS.push(dpy);
49            Window(WINDOWS.len() - 1)
50        }
51    }
52}
53
54impl Window {
55    #[inline]
56    pub(crate) fn dpy(self) -> &'static mut Display {
57        unsafe { &mut WINDOWS[self.0] }
58    }
59
60    pub(crate) fn by_id(id: WindowId) -> Self {
61        let mut i = 0;
62        unsafe {
63            while i < WINDOWS.len() {
64                if WINDOWS[i].gl_window().window().id() == id { return Window(i) }
65                i += 1
66            }
67        }
68        unreachable!()
69    }
70}