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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
use std::borrow::Cow;
use std::ffi::{CStr, CString};
use ecs::actions::Actions;
use ecs::extension::Collector as ExtensionCollector;
use ecs::sole::Single;
use ecs::Sole;
use glfw::WindowSize;
use crate::data_types::dimens::Dimens;
use crate::event::{PostPresent as PostPresentEvent, Start as StartEvent};
use crate::vector::Vec2;
mod reexports
{
pub use glfw::window::{
CursorMode,
Hint as CreationHint,
InputMode,
Key,
KeyModifiers,
KeyState,
};
}
pub use reexports::*;
#[derive(Debug, Sole)]
/// Has to be dropped last since it holds the OpenGL context.
#[sole(drop_last)]
pub struct Window
{
inner: glfw::Window,
}
impl Window
{
/// Returns a new Window builder.
#[must_use]
pub fn builder() -> Builder
{
Builder::default()
}
/// Sets the value of a input mode.
///
/// # Errors
/// Returns `Err` if the input mode is unsupported on the current system.
pub fn set_input_mode(
&self,
input_mode: InputMode,
enabled: bool,
) -> Result<(), Error>
{
Ok(self.inner.set_input_mode(input_mode, enabled)?)
}
/// Sets the cursor mode.
///
/// # Errors
/// If a platform error occurs.
pub fn set_cursor_mode(&self, cursor_mode: CursorMode) -> Result<(), Error>
{
Ok(self.inner.set_cursor_mode(cursor_mode)?)
}
/// Returns whether or not the window should close. Will return true when the user has
/// attempted to close the window.
#[must_use]
pub fn should_close(&self) -> bool
{
self.inner.should_close()
}
/// Processes all pending events.
///
/// # Errors
/// If a platform error occurs.
pub fn poll_events(&self) -> Result<(), Error>
{
Ok(self.inner.poll_events()?)
}
/// Swaps the front and back buffers of the window.
///
/// # Errors
/// Will return `Err` if a platform error occurs or if no OpenGL window context
/// is present.
pub fn swap_buffers(&self) -> Result<(), Error>
{
Ok(self.inner.swap_buffers()?)
}
/// Returns the size of the window.
///
/// # Errors
/// Will return `Err` if a platform error occurs.
pub fn size(&self) -> Result<Dimens<u32>, Error>
{
let size = self.inner.size()?;
Ok(Dimens {
width: size.width,
height: size.height,
})
}
/// Returns the address of the specified OpenGL function, if it is supported by the
/// current OpenGL context.
///
/// # Errors
/// Will return `Err` if a platform error occurs or if no current context has
/// been set.
///
/// # Panics
/// Will panic if the `proc_name` argument contains a nul byte.
pub fn get_proc_address(
&self,
proc_name: &str,
) -> Result<unsafe extern "C" fn(), Error>
{
let proc_name_c: Cow<CStr> = CStr::from_bytes_with_nul(proc_name.as_bytes())
.map(Cow::Borrowed)
.or_else(|_| CString::new(proc_name).map(Cow::Owned))
.expect("OpenGL function name contains a nul byte");
Ok(self.inner.get_proc_address(&proc_name_c)?)
}
/// Makes the OpenGL context of the window current for the calling thread.
///
/// # Errors
/// Will return `Err` if a platform error occurs or if no OpenGL context is
/// present.
pub fn make_context_current(&self) -> Result<(), Error>
{
Ok(self.inner.make_context_current()?)
}
/// Sets the window's framebuffer size callback.
pub fn set_framebuffer_size_callback(&self, callback: impl Fn(Dimens<u32>) + 'static)
{
self.inner.set_framebuffer_size_callback(move |size| {
callback(Dimens {
width: size.width,
height: size.height,
});
});
}
/// Sets the window's key size callback.
pub fn set_key_callback(
&self,
callback: impl Fn(Key, i32, KeyState, KeyModifiers) + 'static,
)
{
self.inner.set_key_callback(callback);
}
/// Sets the window's cursor position callback.
pub fn set_cursor_pos_callback(&self, callback: impl Fn(Vec2<f64>) + 'static)
{
self.inner
.set_cursor_pos_callback(move |pos| callback(Vec2 { x: pos.x, y: pos.y }));
}
/// Sets the window's close callback.
pub fn set_close_callback(&self, callback: impl Fn() + 'static)
{
self.inner.set_close_callback(callback);
}
/// Sets the window's focus callback. The callback is called when the window loses or
/// gains input focus.
pub fn set_focus_callback(&self, callback: impl Fn(bool) + 'static)
{
self.inner.set_focus_callback(callback);
}
}
/// [`Window`] builder.
#[derive(Debug, Clone, Default)]
pub struct Builder
{
inner: glfw::WindowBuilder,
}
impl Builder
{
#[must_use]
pub fn creation_hint(mut self, hint: CreationHint, value: i32) -> Self
{
self.inner = self.inner.hint(hint, value);
self
}
/// Creates a new window.
///
/// # Errors
/// Will return `Err` if the title contains a internal nul byte or if a platform error
/// occurs.
pub fn create(&self, size: Dimens<u32>, title: &str) -> Result<Window, Error>
{
let window = self.inner.create(
&WindowSize {
width: size.width,
height: size.height,
},
title,
)?;
Ok(Window { inner: window })
}
}
#[derive(Debug)]
pub struct Extension
{
window_builder: Builder,
window_size: Dimens<u32>,
window_title: String,
}
impl Extension
{
#[must_use]
pub fn new(window_builder: Builder) -> Self
{
Self { window_builder, ..Default::default() }
}
#[must_use]
pub fn window_size(mut self, window_size: Dimens<u32>) -> Self
{
self.window_size = window_size;
self
}
#[must_use]
pub fn window_title(mut self, window_title: impl Into<String>) -> Self
{
self.window_title = window_title.into();
self
}
}
impl ecs::extension::Extension for Extension
{
fn collect(self, mut collector: ExtensionCollector<'_>)
{
collector.add_system(StartEvent, initialize);
collector.add_system(PostPresentEvent, update);
let window = self
.window_builder
.create(self.window_size, &self.window_title)
.unwrap();
window.set_cursor_mode(CursorMode::Normal).unwrap();
collector.add_sole(window).ok();
}
}
impl Default for Extension
{
fn default() -> Self
{
Self {
window_builder: Builder::default(),
window_size: Dimens { width: 1920, height: 1080 },
window_title: String::new(),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct Error(glfw::Error);
impl From<glfw::Error> for Error
{
fn from(err: glfw::Error) -> Self
{
Self(err)
}
}
fn initialize(window: Single<Window>, actions: Actions)
{
let actions_weak_ref = actions.to_weak_ref();
window.set_close_callback(move || {
let actions_weak_ref = actions_weak_ref.clone();
let actions_ref = actions_weak_ref.access().expect("No world");
actions_ref.to_actions().stop();
});
}
fn update(window: Single<Window>)
{
window
.swap_buffers()
.expect("Failed to swap window buffers");
window.poll_events().expect("Failed to poll window events");
}
|