From 209c1fd627c4bb56b87f91dbe30c18a1c7fe75a5 Mon Sep 17 00:00:00 2001 From: Dominic Szablewski Date: Sun, 27 Aug 2023 12:52:58 +0200 Subject: [PATCH] Add ALT+Enter hotkey to toggle fullscreen; close #45 --- src/platform.h | 1 + src/platform_sdl.c | 15 ++++++++++++++- src/platform_sokol.c | 15 ++++++++++++++- src/wipeout/game.c | 7 +++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/platform.h b/src/platform.h index bcb8b0e..2a21894 100644 --- a/src/platform.h +++ b/src/platform.h @@ -6,6 +6,7 @@ void platform_exit(); vec2i_t platform_screen_size(); double platform_now(); +bool platform_get_fullscreen(); void platform_set_fullscreen(bool fullscreen); void platform_set_audio_mix_cb(void (*cb)(float *buffer, uint32_t len)); diff --git a/src/platform_sdl.c b/src/platform_sdl.c index 4d45c46..a702e5a 100755 --- a/src/platform_sdl.c +++ b/src/platform_sdl.c @@ -61,8 +61,17 @@ SDL_GameController *platform_find_gamepad() { void platform_pump_events() { SDL_Event ev; while (SDL_PollEvent(&ev)) { + // Detect ALT+Enter press to toggle fullscreen + if ( + ev.type == SDL_KEYDOWN && + ev.key.keysym.scancode == SDL_SCANCODE_RETURN && + (ev.key.keysym.mod & (KMOD_LALT | KMOD_RALT)) + ) { + platform_set_fullscreen(!platform_get_fullscreen()); + } + // Input Keyboard - if (ev.type == SDL_KEYDOWN || ev.type == SDL_KEYUP) { + else if (ev.type == SDL_KEYDOWN || ev.type == SDL_KEYUP) { int code = ev.key.keysym.scancode; float state = ev.type == SDL_KEYDOWN ? 1.0 : 0.0; if (code >= SDL_SCANCODE_LCTRL && code <= SDL_SCANCODE_RALT) { @@ -179,6 +188,10 @@ double platform_now() { return (double)perf_counter / (double)perf_freq; } +bool platform_get_fullscreen() { + return SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN; +} + void platform_set_fullscreen(bool fullscreen) { if (fullscreen) { int32_t display = SDL_GetWindowDisplayIndex(window); diff --git a/src/platform_sokol.c b/src/platform_sokol.c index ef1087e..709fb43 100644 --- a/src/platform_sokol.c +++ b/src/platform_sokol.c @@ -155,6 +155,10 @@ double platform_now() { return stm_sec(stm_now()); } +bool platform_get_fullscreen() { + return sapp_is_fullscreen(); +} + void platform_set_fullscreen(bool fullscreen) { if (fullscreen == sapp_is_fullscreen()) { return; @@ -165,8 +169,17 @@ void platform_set_fullscreen(bool fullscreen) { } void platform_handle_event(const sapp_event* ev) { + // Detect ALT+Enter press to toggle fullscreen + if ( + ev->type == SAPP_EVENTTYPE_KEY_DOWN && + ev->key_code == SAPP_KEYCODE_ENTER && + (ev->modifiers & SAPP_MODIFIER_ALT) + ) { + platform_set_fullscreen(!sapp_is_fullscreen()); + } + // Input Keyboard - if (ev->type == SAPP_EVENTTYPE_KEY_DOWN || ev->type == SAPP_EVENTTYPE_KEY_UP) { + else if (ev->type == SAPP_EVENTTYPE_KEY_DOWN || ev->type == SAPP_EVENTTYPE_KEY_UP) { float state = ev->type == SAPP_EVENTTYPE_KEY_DOWN ? 1.0 : 0.0; if (ev->key_code > 0 && ev->key_code < sizeof(keyboard_map)) { int code = keyboard_map[ev->key_code]; diff --git a/src/wipeout/game.c b/src/wipeout/game.c index b7acadd..4066c16 100755 --- a/src/wipeout/game.c +++ b/src/wipeout/game.c @@ -620,6 +620,13 @@ void game_update() { game_scenes[scene_current].update(); } + // Fullscreen might have been toggled through alt+enter + bool fullscreen = platform_get_fullscreen(); + if (fullscreen != save.fullscreen) { + save.fullscreen = fullscreen; + save.is_dirty = true; + } + if (save.is_dirty) { // FIXME: use a text based format? // FIXME: this should probably run async somewhere