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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#[cfg(target_os = "android")]
extern crate sapp_android as sapp;
#[cfg(target_os = "android")]
pub use sapp_android;
#[cfg(target_os = "macos")]
extern crate sapp_darwin as sapp;
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_arch = "wasm32",
windows
)))]
extern crate sapp_dummy as sapp;
#[cfg(target_os = "ios")]
extern crate sapp_ios as sapp;
#[cfg(all(target_os = "linux", feature = "kms"))]
extern crate sapp_kms as sapp;
#[cfg(all(target_os = "linux", not(feature = "kms")))]
extern crate sapp_linux as sapp;
#[cfg(target_arch = "wasm32")]
extern crate sapp_wasm as sapp;
#[cfg(windows)]
extern crate sapp_windows as sapp;
pub mod clipboard;
pub mod conf;
mod event;
pub mod fs;
pub mod graphics;
#[cfg(feature = "log-impl")]
pub mod log;
pub use event::*;
pub use graphics::*;
pub use sapp::gl;
use std::ffi::CString;
mod default_icon;
#[deprecated(
since = "0.3",
note = "libc rand is slow and incosistent across platforms. Please use quad-rnd crate instead."
)]
pub unsafe fn rand() -> i32 {
sapp::rand()
}
#[deprecated(
since = "0.3",
note = "libc rand is slow and incosistent across platforms. Please use quad-rnd crate instead."
)]
pub const RAND_MAX: u32 = sapp::RAND_MAX;
pub mod date {
#[cfg(not(target_arch = "wasm32"))]
pub fn now() -> f64 {
use std::time::SystemTime;
let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|e| panic!("{}", e));
time.as_secs_f64()
}
#[cfg(target_arch = "wasm32")]
pub fn now() -> f64 {
unsafe { sapp::now() }
}
}
impl Context {
pub fn quit(&self) {
#[cfg(not(target_arch = "wasm32"))]
unsafe {
sapp::sapp_quit();
}
}
pub fn request_quit(&self) {
#[cfg(not(target_arch = "wasm32"))]
unsafe {
sapp::sapp_request_quit();
}
}
pub fn cancel_quit(&self) {
#[cfg(not(target_arch = "wasm32"))]
unsafe {
sapp::sapp_cancel_quit();
}
}
pub fn set_cursor_grab(&self, grab: bool) {
#[cfg(not(target_os = "ios"))]
unsafe {
sapp::sapp_set_cursor_grab(grab);
}
}
pub fn show_mouse(&self, shown: bool) {
unsafe {
sapp::sapp_show_mouse(shown);
}
}
pub fn set_mouse_cursor(&self, _cursor_icon: CursorIcon) {
#[cfg(any(
target_arch = "wasm32",
all(target_os = "linux", not(feature = "kms")),
windows,
))]
unsafe {
sapp::sapp_set_mouse_cursor(match _cursor_icon {
CursorIcon::Default => sapp::SAPP_CURSOR_DEFAULT,
CursorIcon::Help => sapp::SAPP_CURSOR_HELP,
CursorIcon::Pointer => sapp::SAPP_CURSOR_POINTER,
CursorIcon::Wait => sapp::SAPP_CURSOR_WAIT,
CursorIcon::Crosshair => sapp::SAPP_CURSOR_CROSSHAIR,
CursorIcon::Text => sapp::SAPP_CURSOR_TEXT,
CursorIcon::Move => sapp::SAPP_CURSOR_MOVE,
CursorIcon::NotAllowed => sapp::SAPP_CURSOR_NOTALLOWED,
CursorIcon::EWResize => sapp::SAPP_CURSOR_EWRESIZE,
CursorIcon::NSResize => sapp::SAPP_CURSOR_NSRESIZE,
CursorIcon::NESWResize => sapp::SAPP_CURSOR_NESWRESIZE,
CursorIcon::NWSEResize => sapp::SAPP_CURSOR_NWSERESIZE,
});
}
}
#[allow(unused_variables)]
pub fn set_window_size(&self, new_width: u32, new_height: u32) {
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "ios",
target_os = "android",
)))]
unsafe {
if sapp::sapp_is_fullscreen() {
#[cfg(feature = "log-impl")]
warn!("Unable to set windowsize while fullscreen: https://github.com/not-fl3/miniquad/issues/179");
return;
}
sapp::sapp_set_window_size(new_width, new_height);
}
}
#[allow(unused_variables)]
pub fn set_fullscreen(&self, fullscreen: bool) {
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "ios",
target_os = "android",
)))]
unsafe {
sapp::sapp_set_fullscreen(fullscreen);
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub enum CursorIcon {
Default,
Help,
Pointer,
Wait,
Crosshair,
Text,
Move,
NotAllowed,
EWResize,
NSResize,
NESWResize,
NWSEResize,
}
pub enum UserData {
Owning((Box<dyn EventHandler>, Context)),
Free(Box<dyn EventHandlerFree>),
}
impl UserData {
pub fn owning(event_handler: impl EventHandler + 'static, ctx: Context) -> UserData {
UserData::Owning((Box::new(event_handler), ctx))
}
pub fn free(event_handler: impl EventHandlerFree + 'static) -> UserData {
UserData::Free(Box::new(event_handler))
}
}
macro_rules! event_call {
( $event_handler:expr, $fn:ident $(, $args:expr)*) => {{
match $event_handler {
UserData::Owning((ref mut event_handler, ref mut context)) => {
event_handler.$fn(context, $($args,)*);
}
UserData::Free(ref mut event_handler) => {
event_handler.$fn($($args,)*);
}
}
}};
}
enum UserDataState {
Uninitialized(Box<dyn 'static + FnOnce(Context) -> UserData>),
Intialized(UserData),
Empty,
}
extern "C" fn init(user_data: *mut ::std::os::raw::c_void) {
let data: &mut UserDataState = unsafe { &mut *(user_data as *mut UserDataState) };
let empty = UserDataState::Empty;
let f = std::mem::replace(data, empty);
let f = if let UserDataState::Uninitialized(f) = f {
f
} else {
panic!();
};
let context = graphics::Context::new();
let user_data = f(context);
*data = UserDataState::Intialized(user_data);
}
extern "C" fn frame(user_data: *mut ::std::os::raw::c_void) {
let data: &mut UserDataState = unsafe { &mut *(user_data as *mut UserDataState) };
let data = if let UserDataState::Intialized(ref mut data) = data {
data
} else {
panic!()
};
event_call!(data, update);
event_call!(data, draw);
}
extern "C" fn event(event: *const sapp::sapp_event, user_data: *mut ::std::os::raw::c_void) {
let data: &mut UserDataState = unsafe { &mut *(user_data as *mut UserDataState) };
let event = unsafe { &*event };
let data = if let UserDataState::Intialized(ref mut data) = data {
data
} else {
panic!()
};
match event.type_ {
sapp::sapp_event_type_SAPP_EVENTTYPE_MOUSE_MOVE => {
event_call!(data, mouse_motion_event, event.mouse_x, event.mouse_y);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_MOUSE_SCROLL => {
event_call!(data, mouse_wheel_event, event.scroll_x, event.scroll_y);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_MOUSE_DOWN => {
event_call!(
data,
mouse_button_down_event,
MouseButton::from(event.mouse_button),
event.mouse_x,
event.mouse_y
);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_MOUSE_UP => {
event_call!(
data,
mouse_button_up_event,
MouseButton::from(event.mouse_button),
event.mouse_x,
event.mouse_y
);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_CHAR => {
if let Some(character) = std::char::from_u32(event.char_code) {
let key_mods = KeyMods::from(event.modifiers);
event_call!(data, char_event, character, key_mods, event.key_repeat)
}
}
sapp::sapp_event_type_SAPP_EVENTTYPE_KEY_DOWN => {
let keycode = KeyCode::from(event.key_code);
let key_mods = KeyMods::from(event.modifiers);
event_call!(data, key_down_event, keycode, key_mods, event.key_repeat)
}
sapp::sapp_event_type_SAPP_EVENTTYPE_KEY_UP => {
let keycode = KeyCode::from(event.key_code);
let key_mods = KeyMods::from(event.modifiers);
event_call!(data, key_up_event, keycode, key_mods);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_RESIZED => {
event_call!(
data,
resize_event,
event.framebuffer_width as f32,
event.framebuffer_height as f32
);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_TOUCHES_BEGAN
| sapp::sapp_event_type_SAPP_EVENTTYPE_TOUCHES_ENDED
| sapp::sapp_event_type_SAPP_EVENTTYPE_TOUCHES_CANCELLED
| sapp::sapp_event_type_SAPP_EVENTTYPE_TOUCHES_MOVED => {
for i in 0..(event.num_touches as usize) {
if event.touches[i].changed {
event_call!(
data,
touch_event,
event.type_.into(),
event.touches[i].identifier as u64,
event.touches[i].pos_x,
event.touches[i].pos_y
);
}
}
}
sapp::sapp_event_type_SAPP_EVENTTYPE_QUIT_REQUESTED => {
event_call!(data, quit_requested_event);
}
#[cfg(not(target_os = "ios"))]
sapp::sapp_event_type_SAPP_EVENTTYPE_RAW_DEVICE => {
event_call!(data, raw_mouse_motion, event.mouse_dx, event.mouse_dy);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_RESUMED => {
event_call!(data, window_restored_event);
}
sapp::sapp_event_type_SAPP_EVENTTYPE_SUSPENDED => {
event_call!(data, window_minimized_event);
}
_ => {}
}
}
pub fn start<F>(conf: conf::Conf, f: F)
where
F: 'static + FnOnce(Context) -> UserData,
{
let mut desc: sapp::sapp_desc = unsafe { std::mem::zeroed() };
let title = CString::new(conf.window_title.as_bytes()).unwrap_or_else(|e| panic!("{}", e));
let mut user_data = Box::new(UserDataState::Uninitialized(Box::new(f)));
desc.sample_count = conf.sample_count;
desc.width = conf.window_width;
desc.height = conf.window_height;
desc.fullscreen = conf.fullscreen as _;
desc.high_dpi = conf.high_dpi as _;
desc.window_title = title.as_ptr();
#[cfg(target_os = "windows")]
if let Some(icon) = conf.icon {
desc.icon = Some(sapp_windows::sapp_icon {
small: icon.small.as_ptr(),
medium: icon.medium.as_ptr(),
big: icon.big.as_ptr(),
});
}
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android",)))]
{
desc.window_resizable = conf.window_resizable as _;
}
desc.user_data = &mut *user_data as *mut _ as *mut _;
desc.init_userdata_cb = Some(init);
desc.frame_userdata_cb = Some(frame);
desc.event_userdata_cb = Some(event);
Box::leak(user_data);
unsafe { sapp::sapp_run(&desc as *const _) };
}