diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-10-10 19:52:03 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-10-10 19:52:03 -0700 |
commit | 36b0f83075c15fc8d107ece20bd6be95a8d2588c (patch) | |
tree | ad85c51109d3c765f88dc35585dd640d6e77ed96 /src | |
parent | cbbe0abec7e0e0f21f3315b455f3f795cedf2c9a (diff) |
Sometimes a more granular main loop integration is required.
In such situations play_run() taking over is untenable, so this
splits out the body of play_run()'s loop as a separately callable
function. Existing main loops can use this to run the play main
loop for a single slice.
The impetus for this has been experimenting with escripten
support in SARS, e.g.
emscripten_set_main_loop_arg((void(*)(void *))play_run_slice, play, -1, 1);
Diffstat (limited to 'src')
-rw-r--r-- | src/play.c | 43 | ||||
-rw-r--r-- | src/play.h | 1 |
2 files changed, 27 insertions, 17 deletions
@@ -448,29 +448,38 @@ int play_shutdown(play_t *play) } -void play_run(play_t *play) +int play_run_slice(play_t *play) { - for (;;) { - SDL_Event ev; + SDL_Event ev; - do { - play->update_needed = 0; + do { + play->update_needed = 0; - if (play->ops[play->context]->update) - play->ops[play->context]->update(play, play->contexts[play->context]); + if (play->ops[play->context]->update) + play->ops[play->context]->update(play, play->contexts[play->context]); - /* see comment in play_context_enter() for why play->update_needed exists */ - } while (play->update_needed); + /* see comment in play_context_enter() for why play->update_needed exists */ + } while (play->update_needed); - if (play->ops[play->context]->render) - play->ops[play->context]->render(play, play->contexts[play->context]); + if (play->ops[play->context]->render) + play->ops[play->context]->render(play, play->contexts[play->context]); - while (!play->update_needed && SDL_PollEvent(&ev)) { - if (ev.type == SDL_APP_TERMINATING || ev.type == SDL_QUIT) - return; + while (!play->update_needed && SDL_PollEvent(&ev)) { + if (ev.type == SDL_APP_TERMINATING || ev.type == SDL_QUIT) + return 0; - if (play->ops[play->context]->dispatch) - play->ops[play->context]->dispatch(play, play->contexts[play->context], &ev); - } + if (play->ops[play->context]->dispatch) + play->ops[play->context]->dispatch(play, play->contexts[play->context], &ev); + } + + return 1; +} + + +void play_run(play_t *play) +{ + for (;;) { + if (!play_run_slice(play)) + return; } } @@ -57,6 +57,7 @@ typedef struct play_ops_t { play_t * play_startup(int argc, char *argv[], unsigned flags, const play_ops_t *ops[]); int play_shutdown(play_t *play); +int play_run_slice(play_t *play); void play_run(play_t *play); void play_music_set(play_t *play, unsigned flags, const char *fmt, ...); |