floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.92k stars 487 forks source link

[sokol_app] add an option html5_use_emsc_set_main_loop #997

Closed Dvad closed 7 months ago

Dvad commented 7 months ago

Implementation of the idea suggested in https://github.com/floooh/sokol/issues/843#issuecomment-1959114136

emscripten_set_main_loop is a higher level function compared to emscripten_request_animation_frame_loop and seems to play nicer with various emcc options (EXIT_RUNTIME or PROXY_TO_PTHREAD). The downside is that we have to compute the timestamp on our side and hence it might be less accurate than the one the browser returns with [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame).

I tested both implementation on a test app. Not sure if we need more testing and how to test.

In particular for my use case, only the version SOKOL_EMSC_USE_SET_MAIN_LOOP works when PROXY_TO_PTHREAD is enabled. But A few more changes are needed for this case in sokol_app which will be for another PR.

floooh commented 7 months ago

See my comment https://github.com/floooh/sokol/issues/843#issuecomment-1960941234 about preprocessor define vs runtime flag.

floooh commented 7 months ago

...also check the build error (I just added that warning recently):

/home/runner/work/sokol/sokol/tests/compile/../../sokol_app.h:5837:47: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
 5837 | _SOKOL_PRIVATE void _sapp_emsc_frame_main_loop() {
      |                                               ^

(the function must have an explicit void param: void _sapp_emsc_frame_main_loop(void))

Dvad commented 7 months ago

SOKOL_EMSC_USE_SET_MAIN_LOOP -> _sapp.desc.html5_use_emsc_set_main_loop

floooh commented 7 months ago

Giving the PR a whirl now...

floooh commented 7 months ago

Ok merged. I also added another config option .html5_emsc_set_main_loop_simulate_infinite_loop to be used as the simulate_infinite_loop param to emscripten_set_main_loop since this was also mentioned in https://github.com/floooh/sokol/issues/843.

...and I changed the texcube-sapp sample to use the set-main-loop method.

Also updated the changelog.

Many thanks for the PR!