Add ALT+Enter hotkey to toggle fullscreen; close #45

This commit is contained in:
Dominic Szablewski 2023-08-27 12:52:58 +02:00
parent da4d3b5c0c
commit 209c1fd627
4 changed files with 36 additions and 2 deletions

View file

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

View file

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

View file

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

View file

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