From 0063e1c3bc71e92c1ae537bc4c1962e689277d2d Mon Sep 17 00:00:00 2001 From: leo60228 Date: Wed, 31 Mar 2021 15:06:26 -0400 Subject: [PATCH] Use Emscripten's main loop On Emscripten, SDL_Delay is implemented as a busy loop. In addition, everything happens on a single thread. This effectively means that you have to let Emscripten manage the main loop, since if you do it yourself the browser will just be frozen. --- desktop_version/src/main.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index a384d534..b6dd64c3 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -23,6 +23,11 @@ #include "SoundSystem.h" #include "UtilityClass.h" +#ifdef __EMSCRIPTEN__ +#include +#include +#endif + scriptclass script; #if !defined(NO_CUSTOM_LEVELS) @@ -54,8 +59,11 @@ static std::string playtestname; static volatile Uint32 time_ = 0; static volatile Uint32 timePrev = 0; static volatile Uint32 accumulator = 0; + +#ifndef __EMSCRIPTEN__ static volatile Uint32 f_time = 0; static volatile Uint32 f_timePrev = 0; +#endif enum FuncType { @@ -339,6 +347,15 @@ static void inline deltaloop(void); static void cleanup(void); +#ifdef __EMSCRIPTEN__ +void emscriptenloop() +{ + timePrev = time_; + time_ = SDL_GetTicks(); + deltaloop(); +} +#endif + int main(int argc, char *argv[]) { char* baseDir = NULL; @@ -611,6 +628,9 @@ int main(int argc, char *argv[]) gamestate_funcs = get_gamestate_funcs(game.gamestate, &num_gamestate_funcs); loop_assign_active_funcs(); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(emscriptenloop, 0, 0); +#else while (true) { f_time = SDL_GetTicks(); @@ -632,6 +652,8 @@ int main(int argc, char *argv[]) } cleanup(); +#endif + return 0; } @@ -731,7 +753,9 @@ static void unfocused_run(void) graphics.render(); gameScreen.FlipScreen(); //We are minimised, so lets put a bit of a delay to save CPU +#ifndef __EMSCRIPTEN__ SDL_Delay(100); +#endif } static void focused_begin(void)