Skip to content

Commit

Permalink
Merge branch 'sokol_emsc_set_main_loop' of https://github.com/Dvad/sokol
Browse files Browse the repository at this point in the history
 into Dvad-sokol_emsc_set_main_loop
  • Loading branch information
floooh committed Mar 2, 2024
2 parents d18e8ef + b921294 commit 36790cc
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions sokol_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand All @@ -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]);
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 36790cc

Please sign in to comment.