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
#[cfg(target_os = "android")]
extern crate sapp_android as sapp;
#[cfg(target_os = "macos")]
extern crate sapp_darwin as sapp;
#[cfg(not(any(
    target_os = "linux",
    target_os = "macos",
    target_os = "android",
    target_arch = "wasm32",
    windows
)))]
extern crate sapp_dummy as sapp;
#[cfg(target_os = "linux")]
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::*;

use std::ffi::CString;

#[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 {
    /// This function simply quits the application without
    /// giving the user a chance to intervene. Usually this might
    /// be called when the user clicks the 'Ok' button in a 'Really Quit?'
    /// dialog box
    pub fn quit(&self) {
        // its not possible to quit wasm anyway
        #[cfg(not(target_arch = "wasm32"))]
        unsafe {
            sapp::sapp_quit();
        }
    }

    /// Calling request_quit() will trigger "quit_requested_event" event , giving
    /// the user code a chance to intervene and cancel the pending quit process
    /// (for instance to show a 'Really Quit?' dialog box).
    /// If the event handler callback does nothing, the application will be quit as usual.
    /// To prevent this, call the function "cancel_quit()"" from inside the event handler.
    pub fn request_quit(&self) {
        // its not possible to quit wasm anyway
        #[cfg(not(target_arch = "wasm32"))]
        unsafe {
            sapp::sapp_request_quit();
        }
    }

    /// Cancels a pending quit request, either initiated
    /// by the user clicking the window close button, or programmatically
    /// by calling "request_quit()". The only place where calling this
    /// function makes sense is from inside the event handler callback when
    /// the "quit_requested_event" event has been received
    pub fn cancel_quit(&self) {
        // its not possible to quit wasm anyway
        #[cfg(not(target_arch = "wasm32"))]
        unsafe {
            sapp::sapp_cancel_quit();
        }
    }

    /// Capture mouse cursor to the current window
    /// On WASM this will automatically hide cursor
    /// On desktop this will bound cursor to windows border
    /// NOTICE: on desktop cursor will not be automatically released after window lost focus
    ///         so set_cursor_grab(false) on window's focus lost is recommended.
    /// TODO: implement window focus events
    pub fn set_cursor_grab(&self, grab: bool) {
        unsafe {
            sapp::sapp_set_cursor_grab(grab);
        }
    }

    /// Show or hide the mouse cursor
    pub fn show_mouse(&self, shown: bool) {
        unsafe {
            sapp::sapp_show_mouse(shown);
        }
    }
}

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))
    }
}

/// call appropriate event handler function - with or without Context reference
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.window_width as f32,
                event.window_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);
        }
        sapp::sapp_event_type_SAPP_EVENTTYPE_RAW_DEVICE => {
            event_call!(data, raw_mouse_motion, event.mouse_dx, event.mouse_dy);
        }
        _ => {}
    }
}

/// Start miniquad.
/// Initialization callback will be called when miniquad's Context is ready.
/// User can take ownership on Context and store it in user Code. Or return it back
/// to miniquad and give miniquad ownership on Context.
///
/// Variant wth EventHandler:
/// ```no_run
/// # use miniquad::*;
/// struct Stage;
///
/// impl EventHandler for Stage {
///     fn update(&mut self, _ctx: &mut Context) {}
///     fn draw(&mut self, _ctx: &mut Context) {}
/// }
/// fn main() {
///     miniquad::start(conf::Conf::default(), |ctx| UserData::owning(Stage, ctx));
/// }
/// ```
///
/// Variant wth EventHandlerFree:
/// ```no_run
/// # use miniquad::*;
/// struct Stage {
///     ctx: Context,
/// }
/// impl EventHandlerFree for Stage {
///     fn update(&mut self) {}
///     fn draw(&mut self) {}
/// }
/// fn main() {
///     miniquad::start(conf::Conf::default(), |ctx| UserData::free(Stage { ctx }));
/// }
/// ```
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();
    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);

    std::mem::forget(user_data);

    unsafe { sapp::sapp_run(&desc as *const _) };
}