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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#![deny(missing_docs)]
#![deny(missing_copy_implementations)]
extern crate libc;
extern crate input;
use input::Input;
pub type ProcAddress = *const libc::c_void;
#[derive(Copy, Clone)]
pub struct Size {
pub width: u32,
pub height: u32,
}
impl From<[u32; 2]> for Size {
#[inline(always)]
fn from(value: [u32; 2]) -> Size {
Size { width: value[0], height: value[1] }
}
}
impl From<(u32, u32)> for Size {
#[inline(always)]
fn from(value: (u32, u32)) -> Size {
Size { width: value.0, height: value.1 }
}
}
pub trait Window {
type Event;
fn should_close(&self) -> bool;
fn size(&self) -> Size;
fn swap_buffers(&mut self);
fn poll_event(&mut self) -> Option<Self::Event>;
fn draw_size(&self) -> Size;
}
pub trait AdvancedWindow: Window + Sized {
fn get_title(&self) -> String;
fn set_title(&mut self, value: String);
fn title(mut self, value: String) -> Self {
self.set_title(value);
self
}
fn get_exit_on_esc(&self) -> bool;
fn set_exit_on_esc(&mut self, value: bool);
fn exit_on_esc(mut self, value: bool) -> Self {
self.set_exit_on_esc(value);
self
}
fn set_capture_cursor(&mut self, value: bool);
fn capture_cursor(mut self, value: bool) -> Self {
self.set_capture_cursor(value);
self
}
}
pub trait OpenGLWindow: Window {
fn get_proc_address(&mut self, proc_name: &str) -> ProcAddress;
fn is_current(&self) -> bool;
fn make_current(&mut self);
}
pub struct WindowSettings {
title: String,
size: Size,
samples: u8,
fullscreen: bool,
exit_on_esc: bool,
vsync: bool,
}
impl WindowSettings {
pub fn new<T: Into<String>, S: Into<Size>>(
title: T, size: S) -> WindowSettings
{
WindowSettings {
title: title.into(),
size: size.into(),
samples: 0,
fullscreen: false,
exit_on_esc: false,
vsync: false,
}
}
pub fn get_title(&self) -> String { self.title.clone() }
pub fn title(mut self, value: String) -> Self {
self.title = value;
self
}
pub fn get_size(&self) -> Size { self.size }
pub fn size(mut self, value: Size) -> Self {
self.size = value;
self
}
pub fn get_fullscreen(&self) -> bool { self.fullscreen }
pub fn fullscreen(mut self, value: bool) -> Self {
self.fullscreen = value;
self
}
pub fn get_exit_on_esc(&self) -> bool { self.exit_on_esc }
pub fn exit_on_esc(mut self, value: bool) -> Self {
self.exit_on_esc = value;
self
}
pub fn get_samples(&self) -> u8 { self.samples }
pub fn samples(mut self, value: u8) -> Self {
self.samples = value;
self
}
pub fn get_vsync(&self) -> bool { self.vsync }
pub fn vsync(mut self, value: bool) -> Self {
self.vsync = value;
self
}
}
pub struct NoWindow {
should_close: bool,
title: String,
}
impl NoWindow {
pub fn new(settings: WindowSettings) -> NoWindow {
let title = settings.title.clone();
NoWindow {
should_close: false,
title: title,
}
}
}
impl Window for NoWindow {
type Event = Input;
fn should_close(&self) -> bool { self.should_close }
fn size(&self) -> Size { Size { width: 0, height: 0 } }
fn swap_buffers(&mut self) {}
fn poll_event(&mut self) -> Option<Input> { None }
fn draw_size(&self) -> Size { self.size() }
}
impl AdvancedWindow for NoWindow {
fn get_title(&self) -> String { self.title.clone() }
fn set_title(&mut self, value: String) { self.title = value; }
fn get_exit_on_esc(&self) -> bool { false }
fn set_exit_on_esc(&mut self, _value: bool) {}
fn set_capture_cursor(&mut self, _value: bool) {}
}