From f96b2ad16cc470295a8af869b4caf4e0a9c5bb22 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Mon, 4 Nov 2024 03:28:47 +0000 Subject: [PATCH] window: use SDL flags to determine centred window config --- UnleashedRecomp/config.h | 4 ++-- UnleashedRecomp/config_detail.h | 2 ++ UnleashedRecomp/ui/window.cpp | 30 +++++++++++++++++------------- UnleashedRecomp/ui/window.h | 13 +++++++++++-- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/UnleashedRecomp/config.h b/UnleashedRecomp/config.h index ec5ef76..e3e7caf 100644 --- a/UnleashedRecomp/config.h +++ b/UnleashedRecomp/config.h @@ -26,8 +26,8 @@ public: CONFIG_DEFINE("Audio", bool, WerehogBattleMusic, true); CONFIG_DEFINE_ENUM("Video", EGraphicsAPI, GraphicsAPI, EGraphicsAPI::D3D12); - CONFIG_DEFINE_HIDE("Video", int32_t, WindowX, -1); - CONFIG_DEFINE_HIDE("Video", int32_t, WindowY, -1); + CONFIG_DEFINE_HIDE("Video", int32_t, WindowX, WINDOWPOS_CENTRED); + CONFIG_DEFINE_HIDE("Video", int32_t, WindowY, WINDOWPOS_CENTRED); CONFIG_DEFINE("Video", int32_t, WindowWidth, 1280); CONFIG_DEFINE("Video", int32_t, WindowHeight, 720); CONFIG_DEFINE_ENUM_HIDE("Video", EWindowState, WindowState, EWindowState::Normal); diff --git a/UnleashedRecomp/config_detail.h b/UnleashedRecomp/config_detail.h index e9bf6d1..5b31540 100644 --- a/UnleashedRecomp/config_detail.h +++ b/UnleashedRecomp/config_detail.h @@ -28,6 +28,8 @@ #define CONFIG_GET_DEFAULT(name) Config::name.DefaultValue #define CONFIG_SET_DEFAULT(name) Config::name.MakeDefault(); +#define WINDOWPOS_CENTRED 0x2FFF0000 + class ConfigDefBase { public: diff --git a/UnleashedRecomp/ui/window.cpp b/UnleashedRecomp/ui/window.cpp index 3727f0e..f833742 100644 --- a/UnleashedRecomp/ui/window.cpp +++ b/UnleashedRecomp/ui/window.cpp @@ -40,6 +40,17 @@ int Window_OnSDLEvent(void*, SDL_Event* event) Config::Fullscreen = Window::SetFullscreen(false); Window::SetDimensions(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DEFAULT_WIDTH, DEFAULT_HEIGHT); break; + + // Recentre window on F3. + case SDLK_F3: + { + if (Window::IsFullscreen()) + break; + + Window::SetDimensions(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Window::s_width, Window::s_height); + + break; + } } break; @@ -128,28 +139,21 @@ void Window::Init() s_width = Config::WindowWidth; s_height = Config::WindowHeight; - auto isPositionValid = IsPositionValid(); - - if (!isPositionValid) + if (!IsPositionValid()) { s_x = SDL_WINDOWPOS_CENTERED; s_y = SDL_WINDOWPOS_CENTERED; s_width = DEFAULT_WIDTH; s_height = DEFAULT_HEIGHT; + + Config::WindowX = s_x; + Config::WindowY = s_y; + Config::WindowWidth = s_width; + Config::WindowHeight = s_height; } s_pWindow = SDL_CreateWindow("SWA", s_x, s_y, s_width, s_height, GetWindowFlags()); - if (!isPositionValid) - { - auto rect = Window::GetDimensions(); - - Config::WindowX = rect.x; - Config::WindowY = rect.y; - Config::WindowWidth = rect.w; - Config::WindowHeight = rect.h; - } - SetIcon(); SetTitle(); SDL_SetWindowMinimumSize(s_pWindow, 640, 480); diff --git a/UnleashedRecomp/ui/window.h b/UnleashedRecomp/ui/window.h index 10c5d51..35ac83a 100644 --- a/UnleashedRecomp/ui/window.h +++ b/UnleashedRecomp/ui/window.h @@ -148,8 +148,17 @@ public: SDL_Rect bounds; if (SDL_GetDisplayBounds(i, &bounds) == 0) { - if (s_x >= bounds.x && s_x < bounds.x + bounds.w && - s_y >= bounds.y && s_y < bounds.y + bounds.h) + auto x = s_x; + auto y = s_y; + + if (x == SDL_WINDOWPOS_CENTERED) + x = bounds.w / 2 - s_width / 2; + + if (y == SDL_WINDOWPOS_CENTERED) + y = bounds.h / 2 - s_height / 2; + + if (x >= bounds.x && x < bounds.x + bounds.w && + y >= bounds.y && y < bounds.y + bounds.h) { return true; }