diff --git a/src/internal.h b/src/internal.h index 61c6746..041be97 100644 --- a/src/internal.h +++ b/src/internal.h @@ -53,6 +53,12 @@ #define SUPPRESS_END #endif +#ifdef __EMSCRIPTEN__ +#define IS_EMSCRIPTEN true +#else +#define IS_EMSCRIPTEN false +#endif + #ifdef ALLEGRO_WINDOWS #define LIBRARY_EXTENSION ".dll" #elif defined(__EMSCRIPTEN__) diff --git a/src/libsuperderpy.c b/src/libsuperderpy.c index 0272c28..6a1d744 100644 --- a/src/libsuperderpy.c +++ b/src/libsuperderpy.c @@ -105,18 +105,18 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* game->config.voice = strtol(GetConfigOptionDefault(game, "SuperDerpy", "voice", "10"), NULL, 10); game->config.fx = strtol(GetConfigOptionDefault(game, "SuperDerpy", "fx", "10"), NULL, 10); game->config.mute = strtol(GetConfigOptionDefault(game, "SuperDerpy", "mute", "0"), NULL, 10); - game->config.debug.enabled = strtol(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"), NULL, 10); game->config.width = strtol(GetConfigOptionDefault(game, "SuperDerpy", "width", GetDefaultWindowWidth(game)), NULL, 10); if (game->config.width < 100) { game->config.width = 100; } game->config.height = strtol(GetConfigOptionDefault(game, "SuperDerpy", "height", GetDefaultWindowHeight(game)), NULL, 10); if (game->config.height < 100) { game->config.height = 100; } + game->config.autopause = strtol(GetConfigOptionDefault(game, "SuperDerpy", "autopause", IS_EMSCRIPTEN ? "1" : "0"), NULL, 10); + game->config.debug.enabled = strtol(GetConfigOptionDefault(game, "SuperDerpy", "debug", "0"), NULL, 10); game->config.debug.verbose = strtol(GetConfigOptionDefault(game, "debug", "verbose", "0"), NULL, 10); game->config.debug.livereload = strtol(GetConfigOptionDefault(game, "debug", "livereload", "1"), NULL, 10); - game->config.debug.autopause = strtol(GetConfigOptionDefault(game, "debug", "autopause", "1"), NULL, 10); #ifdef __EMSCRIPTEN__ - game->config.fullscreen = false; + game->config.fullscreen = false; // we can't start fullscreen on emscripten #endif game->_priv.showconsole = game->config.debug.enabled; diff --git a/src/libsuperderpy.h b/src/libsuperderpy.h index 62afe1f..9ccecdd 100644 --- a/src/libsuperderpy.h +++ b/src/libsuperderpy.h @@ -159,11 +159,11 @@ struct Game { bool fullscreen; /*!< Fullscreen toggle. */ int width; /*!< Width of window as being set in configuration. */ int height; /*!< Height of window as being set in configuration. */ + bool autopause; /*!< Pauses/resumes the game when the window loses/gains focus. */ struct { bool enabled; /*!< Toggles debug mode. */ bool verbose; /*!< Prints file names and line numbers with every message. */ bool livereload; /*!< Automatically reloads gamestates on window focus. */ - bool autopause; /*!< Pauses/resumes the game when the window loses/gains focus. */ } debug; /*!< Debug mode settings. */ } config; /*!< Configuration values from the config file. */ diff --git a/src/mainloop.c b/src/mainloop.c index 8893bf0..2a96863 100644 --- a/src/mainloop.c +++ b/src/mainloop.c @@ -32,6 +32,22 @@ static inline void HandleEvent(struct Game* game, ALLEGRO_EVENT* ev) { ResumeExecution(game); break; + case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: + if (game->config.autopause) { + PrintConsole(game, "Focus lost, autopausing..."); + PauseExecution(game); + } + break; + + case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: + if (game->config.autopause) { + if (game->config.debug.enabled && game->config.debug.livereload) { + ReloadCode(game); + } + ResumeExecution(game); + } + break; + case ALLEGRO_EVENT_DISPLAY_RESIZE: #ifdef LIBSUPERDERPY_IMGUI ImGui_ImplAllegro5_InvalidateDeviceObjects(); @@ -121,24 +137,7 @@ static inline void HandleEvent(struct Game* game, ALLEGRO_EVENT* ev) { static inline void HandleDebugEvent(struct Game* game, ALLEGRO_EVENT* ev) { switch (ev->type) { - case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT: - if (game->config.debug.autopause) { - PrintConsole(game, "DEBUG: autopause"); - PauseExecution(game); - } - break; - - case ALLEGRO_EVENT_DISPLAY_SWITCH_IN: - if (game->config.debug.autopause) { - if (game->config.debug.livereload) { - ReloadCode(game); - } - ResumeExecution(game); - } - break; - case ALLEGRO_EVENT_KEY_DOWN: - switch (ev->keyboard.keycode) { case ALLEGRO_KEY_F1: if (!game->_priv.paused) { @@ -181,7 +180,7 @@ static inline bool MainloopEvents(struct Game* game) { do { ALLEGRO_EVENT ev; - if (game->_priv.paused) { + if (game->_priv.paused && !IS_EMSCRIPTEN) { // there's no frame flipping when paused, so avoid pointless busylooping al_wait_for_event(game->_priv.event_queue, &ev); } else if (!al_get_next_event(game->_priv.event_queue, &ev)) {