move autopause feature out of debug events

This commit is contained in:
Sebastian Krzyszkowiak 2019-01-08 04:54:53 +01:00
parent d0316cd789
commit 2ad77b071f
No known key found for this signature in database
GPG key ID: E8F235CF3BDBC3FF
4 changed files with 27 additions and 22 deletions

View file

@ -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__)

View file

@ -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;

View file

@ -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. */

View file

@ -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)) {