diff --git a/sokol_app.h b/sokol_app.h index 4b1c6e520..f3a0e04de 100644 --- a/sokol_app.h +++ b/sokol_app.h @@ -1716,6 +1716,7 @@ typedef struct sapp_desc { bool html5_bubble_wheel_events; // same for wheel events bool html5_bubble_key_events; // if true, bubble up *all* key events to browser, not just key events that represent characters bool html5_bubble_char_events; // if true, bubble up character events to browser + bool html5_use_emsc_set_main_loop; // if true, use set_main_loop instead of request_animation_frame_loop bool ios_keyboard_resizes_canvas; // if true, showing the iOS keyboard shrinks the canvas } sapp_desc; @@ -5854,7 +5855,7 @@ _SOKOL_PRIVATE void _sapp_emsc_unregister_eventhandlers(void) { #endif } -_SOKOL_PRIVATE EM_BOOL _sapp_emsc_frame(double time, void* userData) { +_SOKOL_PRIVATE EM_BOOL _sapp_emsc_frame_animation_loop(double time, void* userData) { _SOKOL_UNUSED(userData); _sapp_timing_external(&_sapp.timing, time / 1000.0); @@ -5881,6 +5882,13 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_frame(double time, void* userData) { return EM_TRUE; } +_SOKOL_PRIVATE void _sapp_emsc_frame_main_loop(void) { + const double time = emscripten_performance_now(); + if(!_sapp_emsc_frame_animation_loop(time, NULL)) { + emscripten_cancel_main_loop(); + } +} + _SOKOL_PRIVATE void _sapp_emsc_run(const sapp_desc* desc) { _sapp_init_state(desc); sapp_js_init(&_sapp.html5_canvas_selector[1]); @@ -5911,8 +5919,11 @@ _SOKOL_PRIVATE void _sapp_emsc_run(const sapp_desc* desc) { sapp_set_icon(&desc->icon); // start the frame loop - emscripten_request_animation_frame_loop(_sapp_emsc_frame, 0); - + if (_sapp.desc.html5_use_emsc_set_main_loop) { + emscripten_set_main_loop(_sapp_emsc_frame_main_loop, 0, false); + } else { + emscripten_request_animation_frame_loop(_sapp_emsc_frame_animation_loop, 0); + } // NOT A BUG: do not call _sapp_discard_state() here, instead this is // called in _sapp_emsc_frame() when the application is ordered to quit }