From fabc1ffbc71c650dfc661e4eb28f3c5c54709765 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:37:07 +0000 Subject: [PATCH] window: fix abnormal window states erroneously saving dimensions --- UnleashedRecomp/ui/window.cpp | 49 +++++++++++++----------------- UnleashedRecomp/ui/window.h | 10 ++++-- UnleashedRecompLib/config/SWA.toml | 2 +- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/UnleashedRecomp/ui/window.cpp b/UnleashedRecomp/ui/window.cpp index 006666e..0ef1ea7 100644 --- a/UnleashedRecomp/ui/window.cpp +++ b/UnleashedRecomp/ui/window.cpp @@ -38,7 +38,7 @@ int Window_OnSDLEvent(void*, SDL_Event* event) // Restore original window dimensions on F2. case SDLK_F2: Config::Fullscreen = Window::SetFullscreen(false); - Window::SetDimensions(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, DEFAULT_WIDTH, DEFAULT_HEIGHT); + Window::SetDimensions(DEFAULT_WIDTH, DEFAULT_HEIGHT); break; // Recentre window on F3. @@ -47,7 +47,7 @@ int Window_OnSDLEvent(void*, SDL_Event* event) if (Window::IsFullscreen()) break; - Window::SetDimensions(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Window::s_width, Window::s_height); + Window::SetDimensions(Window::s_width, Window::s_height); break; } @@ -82,42 +82,22 @@ int Window_OnSDLEvent(void*, SDL_Event* event) break; case SDL_WINDOWEVENT_RESTORED: - case SDL_WINDOWEVENT_MAXIMIZED: - { - Config::WindowState = Window::IsMaximised() - ? EWindowState::Maximised - : EWindowState::Normal; - + Config::WindowState = EWindowState::Normal; + break; + + case SDL_WINDOWEVENT_MAXIMIZED: + Config::WindowState = EWindowState::Maximised; break; - } case SDL_WINDOWEVENT_RESIZED: - { Window::s_width = event->window.data1; Window::s_height = event->window.data2; - - if (!Window::IsFullscreen()) - { - Config::WindowWidth = Window::s_width; - Config::WindowHeight = Window::s_height; - } - break; - } case SDL_WINDOWEVENT_MOVED: - { Window::s_x = event->window.data1; Window::s_y = event->window.data2; - - if (!Window::IsFullscreen()) - { - Config::WindowX = Window::s_x; - Config::WindowY = Window::s_y; - } - break; - } } break; @@ -157,7 +137,7 @@ void Window::Init() s_pWindow = SDL_CreateWindow("SWA", s_x, s_y, s_width, s_height, GetWindowFlags()); - if (Config::Fullscreen) + if (IsFullscreen()) SDL_ShowCursor(SDL_DISABLE); SetIcon(); @@ -171,6 +151,17 @@ void Window::Init() s_handle = info.info.win.window; } +void Window::Update() +{ + if (!Window::IsFullscreen() && !Window::IsMaximised()) + { + Config::WindowWidth = Window::s_width; + Config::WindowHeight = Window::s_height; + Config::WindowX = Window::s_x; + Config::WindowY = Window::s_y; + } +} + // CApplication::Update PPC_FUNC_IMPL(__imp__sub_822C1130); PPC_FUNC(sub_822C1130) @@ -178,5 +169,7 @@ PPC_FUNC(sub_822C1130) SDL_PumpEvents(); SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); + Window::Update(); + __imp__sub_822C1130(ctx, base); } diff --git a/UnleashedRecomp/ui/window.h b/UnleashedRecomp/ui/window.h index 35ac83a..f463168 100644 --- a/UnleashedRecomp/ui/window.h +++ b/UnleashedRecomp/ui/window.h @@ -106,12 +106,12 @@ public: return rect; } - static void SetDimensions(int x, int y, int w, int h) + static void SetDimensions(int w, int h, int x = SDL_WINDOWPOS_CENTERED, int y = SDL_WINDOWPOS_CENTERED) { - s_x = x; - s_y = y; s_width = w; s_height = h; + s_x = x; + s_y = y; SDL_SetWindowSize(s_pWindow, w, h); SDL_ResizeEvent(s_pWindow, w, h); @@ -151,6 +151,9 @@ public: auto x = s_x; auto y = s_y; + if (!Config::Fullscreen && s_width == bounds.w && s_height == bounds.h) + return false; + if (x == SDL_WINDOWPOS_CENTERED) x = bounds.w / 2 - s_width / 2; @@ -169,4 +172,5 @@ public: } static void Init(); + static void Update(); }; diff --git a/UnleashedRecompLib/config/SWA.toml b/UnleashedRecompLib/config/SWA.toml index 86d9ab2..e071707 100644 --- a/UnleashedRecompLib/config/SWA.toml +++ b/UnleashedRecompLib/config/SWA.toml @@ -347,4 +347,4 @@ registers = ["r11"] [[midasm_hook]] name = "WerehogBattleMusicMidAsmHook" address = 0x82B47728 -registers = ["r11"] +registers = ["r11"] \ No newline at end of file