From bbbe7b1b02f91217e3492b5ba787dd27efd2c4c5 Mon Sep 17 00:00:00 2001 From: Dextinfire <> Date: Fri, 19 Apr 2024 02:32:49 -0700 Subject: [PATCH] Simple alt-enter fullscreen toggle --- src/inputsystem.cpp | 28 ++++++++++++++++++++++++++-- src/renderer/dx9/window.cpp | 8 ++++++++ src/renderer/dx9/window.h | 1 + src/renderer/opengl/window.cpp | 8 ++++++++ src/renderer/opengl/window.h | 1 + src/renderer/vulkan/window.cpp | 8 ++++++++ src/renderer/vulkan/window.h | 1 + src/renderer/window.h | 1 + 8 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/inputsystem.cpp b/src/inputsystem.cpp index f2a3a184..5fe370c3 100644 --- a/src/inputsystem.cpp +++ b/src/inputsystem.cpp @@ -1,5 +1,5 @@ #include "inputsystem.h" -//#include "window.h" +// #include "window.h" #include "renderer/renderer.h" #include "profile/game.h" @@ -38,6 +38,8 @@ bool TouchWentDown = false; static SDL_FingerID CurrentFinger = 0; +bool fullscreenToggle = false; + void BeginFrame() { memset(ControllerButtonWentDown, false, sizeof(ControllerButtonWentDown)); memset(ControllerAxisWentDownLight, false, @@ -110,10 +112,32 @@ bool HandleEvent(SDL_Event const* ev) { return true; break; } - case SDL_KEYDOWN: + case SDL_KEYDOWN: { + SDL_KeyboardEvent const* evt = &ev->key; + CurrentInputDevice = IDEV_Keyboard; + // Alt+Enter fullscreen toggle + if (evt->keysym.mod & KMOD_ALT && evt->keysym.sym == SDLK_RETURN && + !fullscreenToggle) { + Window->ToggleFullscreen(); + fullscreenToggle = true; + return true; + } + + KeyboardButtonWentDown[evt->keysym.scancode] = + (evt->state == SDL_PRESSED && + !KeyboardButtonIsDown[evt->keysym.scancode]); + KeyboardButtonIsDown[evt->keysym.scancode] = evt->state == SDL_PRESSED; + } break; case SDL_KEYUP: { SDL_KeyboardEvent const* evt = &ev->key; CurrentInputDevice = IDEV_Keyboard; + // Alt+Enter fullscreen toggle + if (evt->keysym.mod & KMOD_ALT && evt->keysym.sym == SDLK_RETURN && + fullscreenToggle) { + fullscreenToggle = false; + return true; + } + KeyboardButtonWentDown[evt->keysym.scancode] = (evt->state == SDL_PRESSED && !KeyboardButtonIsDown[evt->keysym.scancode]); diff --git a/src/renderer/dx9/window.cpp b/src/renderer/dx9/window.cpp index 356eb99c..f6b6189d 100644 --- a/src/renderer/dx9/window.cpp +++ b/src/renderer/dx9/window.cpp @@ -92,6 +92,14 @@ void DirectX9Window::Init() { WindowWidth, WindowHeight); } +void DirectX9Window::ToggleFullscreen() { + if (SDL_GetWindowFlags(SDLWindow) & SDL_WINDOW_FULLSCREEN) { + SDL_SetWindowFullscreen(SDLWindow, 0); + } else { + SDL_SetWindowFullscreen(SDLWindow, SDL_WINDOW_FULLSCREEN); + } +} + void DirectX9Window::SetDimensions(int width, int height, int msaa, float renderScale) {} diff --git a/src/renderer/dx9/window.h b/src/renderer/dx9/window.h index 1a8bb69f..0c0e08a6 100644 --- a/src/renderer/dx9/window.h +++ b/src/renderer/dx9/window.h @@ -10,6 +10,7 @@ class DirectX9Window : public BaseWindow { void Init() override; void SetDimensions(int width, int height, int msaa, float renderScale) override; + void ToggleFullscreen() override; RectF GetViewport() override; RectF GetScaledViewport() override; void SwapRTs() override; diff --git a/src/renderer/opengl/window.cpp b/src/renderer/opengl/window.cpp index 16664d6e..4cf25b21 100644 --- a/src/renderer/opengl/window.cpp +++ b/src/renderer/opengl/window.cpp @@ -255,6 +255,14 @@ void GLWindow::SetDimensions(int width, int height, int msaa, RenderScale = renderScale; } +void GLWindow::ToggleFullscreen() { + if (SDL_GetWindowFlags(SDLWindow) & SDL_WINDOW_FULLSCREEN) { + SDL_SetWindowFullscreen(SDLWindow, 0); + } else { + SDL_SetWindowFullscreen(SDLWindow, SDL_WINDOW_FULLSCREEN); + } +} + void GLWindow::CleanFBOs() { if (drawRenderTexture) glDeleteTextures(1, &drawRenderTexture); if (ReadRenderTexture) glDeleteTextures(1, &ReadRenderTexture); diff --git a/src/renderer/opengl/window.h b/src/renderer/opengl/window.h index d6ade5b7..4fe1f81b 100644 --- a/src/renderer/opengl/window.h +++ b/src/renderer/opengl/window.h @@ -10,6 +10,7 @@ class GLWindow : public BaseWindow { void Init() override; void SetDimensions(int width, int height, int msaa, float renderScale) override; + void ToggleFullscreen() override; RectF GetViewport() override; RectF GetScaledViewport() override; void SwapRTs() override; diff --git a/src/renderer/vulkan/window.cpp b/src/renderer/vulkan/window.cpp index ac954796..765b83b9 100644 --- a/src/renderer/vulkan/window.cpp +++ b/src/renderer/vulkan/window.cpp @@ -36,6 +36,14 @@ void VulkanWindow::UpdateDimensions() { MainRendererInstance->RecreateSwapChain(); } +void VulkanWindow::ToggleFullscreen() { + if (SDL_GetWindowFlags(SDLWindow) & SDL_WINDOW_FULLSCREEN) { + SDL_SetWindowFullscreen(SDLWindow, 0); + } else { + SDL_SetWindowFullscreen(SDLWindow, SDL_WINDOW_FULLSCREEN); + } +} + RectF VulkanWindow::GetViewport() { RectF viewport; float scale = fmin((float)WindowWidth / Profile::DesignWidth, diff --git a/src/renderer/vulkan/window.h b/src/renderer/vulkan/window.h index 9dd85524..f208a662 100644 --- a/src/renderer/vulkan/window.h +++ b/src/renderer/vulkan/window.h @@ -10,6 +10,7 @@ class VulkanWindow : public BaseWindow { void Init() override; void SetDimensions(int width, int height, int msaa, float renderScale) override; + void ToggleFullscreen() override; RectF GetViewport() override; RectF GetScaledViewport() override; void SwapRTs() override; diff --git a/src/renderer/window.h b/src/renderer/window.h index d4b86424..faa40643 100644 --- a/src/renderer/window.h +++ b/src/renderer/window.h @@ -18,6 +18,7 @@ class BaseWindow { virtual void Init() = 0; virtual void SetDimensions(int width, int height, int msaa, float renderScale) = 0; + virtual void ToggleFullscreen() = 0; // Aspect ratio corrected viewport in window coordinates virtual RectF GetViewport() = 0; // Aspect ratio corrected viewport in window coordinates scaled by RenderScale