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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
pub mod events;
pub mod prelude {
pub use super::events::{Event, WindowEvent};
pub use super::system::{EventListener, EventListenerHandle};
pub use super::WindowParams;
}
mod backends;
mod system;
use self::ins::{ctx, CTX};
use self::system::{EventListener, EventListenerHandle, WindowSystem};
use crate::errors::*;
use crate::math::prelude::Vector2;
#[derive(Debug, Clone)]
pub struct WindowParams {
pub title: String,
pub size: Vector2<u32>,
pub multisample: u16,
pub vsync: bool,
}
impl Default for WindowParams {
fn default() -> Self {
WindowParams {
title: "Window".to_owned(),
size: Vector2::new(640, 320),
multisample: 2,
vsync: false,
}
}
}
pub(crate) unsafe fn setup(params: WindowParams) -> Result<()> {
debug_assert!(CTX.is_null(), "duplicated setup of window system.");
let ctx = WindowSystem::from(params)?;
CTX = Box::into_raw(Box::new(ctx));
Ok(())
}
pub(crate) unsafe fn headless() {
debug_assert!(CTX.is_null(), "duplicated setup of window system.");
let ctx = WindowSystem::headless();
CTX = Box::into_raw(Box::new(ctx));
}
#[inline]
pub(crate) fn resize(dimensions: Vector2<u32>) {
ctx().resize(dimensions);
}
pub(crate) unsafe fn discard() {
if CTX.is_null() {
return;
}
drop(Box::from_raw(CTX as *mut WindowSystem));
CTX = std::ptr::null();
}
pub fn attach<T: EventListener + 'static>(lis: T) -> EventListenerHandle {
ctx().add_event_listener(lis)
}
pub fn detach(handle: EventListenerHandle) {
ctx().remove_event_listener(handle)
}
#[inline]
pub fn show() {
ctx().show();
}
#[inline]
pub fn hide() {
ctx().hide();
}
#[inline]
pub fn make_current() -> Result<()> {
ctx().make_current()
}
#[inline]
pub fn is_current() -> bool {
ctx().is_current()
}
#[inline]
pub fn position() -> Vector2<i32> {
ctx().position()
}
#[inline]
pub fn dimensions() -> Vector2<u32> {
ctx().dimensions()
}
#[inline]
pub fn device_pixel_ratio() -> f32 {
ctx().device_pixel_ratio()
}
mod ins {
use super::system::WindowSystem;
pub static mut CTX: *const WindowSystem = std::ptr::null();
#[inline]
pub fn ctx() -> &'static WindowSystem {
unsafe {
debug_assert!(
!CTX.is_null(),
"window system has not been initialized properly."
);
&*CTX
}
}
}