From bc2080e2c61d24a3e834b50fdbf857309ca2c2d5 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Thu, 29 Jun 2023 01:12:52 +1000 Subject: [PATCH] Improve windowing logic --- .../application/StarMainApplication_sdl.cpp | 54 +++++++++++-------- source/client/StarClientApplication.cpp | 1 - 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/source/application/StarMainApplication_sdl.cpp b/source/application/StarMainApplication_sdl.cpp index 5837aa6..0b5a6ba 100644 --- a/source/application/StarMainApplication_sdl.cpp +++ b/source/application/StarMainApplication_sdl.cpp @@ -445,8 +445,12 @@ private: if (SDL_SetWindowDisplayMode(parent->m_sdlWindow, &requestedDisplayMode) == 0) { if (parent->m_windowMode == WindowMode::Fullscreen) SDL_SetWindowFullscreen(parent->m_sdlWindow, 0); - else - parent->m_windowMode = WindowMode::Fullscreen; + else if (parent->m_windowMode == WindowMode::Borderless) + SDL_SetWindowBordered(parent->m_sdlWindow, SDL_TRUE); + else if (parent->m_windowMode == WindowMode::Maximized) + SDL_RestoreWindow(parent->m_sdlWindow); + + parent->m_windowMode = WindowMode::Fullscreen; SDL_SetWindowFullscreen(parent->m_sdlWindow, SDL_WINDOW_FULLSCREEN); } else { Logger::warn("Failed to set resolution {}, {}", (unsigned)requestedDisplayMode.w, (unsigned)requestedDisplayMode.h); @@ -469,16 +473,18 @@ private: } void setNormalWindow(Vec2U windowSize) override { + auto window = parent->m_sdlWindow; if (parent->m_windowMode != WindowMode::Normal || parent->m_windowSize != windowSize) { - if (parent->m_windowMode == WindowMode::Fullscreen || parent->m_windowMode == WindowMode::Borderless) { - SDL_SetWindowFullscreen(parent->m_sdlWindow, 0); - SDL_SetWindowBordered(parent->m_sdlWindow, SDL_TRUE); - } + if (parent->m_windowMode == WindowMode::Fullscreen) + SDL_SetWindowFullscreen(window, 0); + else if (parent->m_windowMode == WindowMode::Borderless) + SDL_SetWindowBordered(window, SDL_TRUE); else if (parent->m_windowMode == WindowMode::Maximized) - SDL_RestoreWindow(parent->m_sdlWindow); + SDL_RestoreWindow(window); - SDL_SetWindowSize(parent->m_sdlWindow, windowSize[0], windowSize[1]); - SDL_SetWindowPosition(parent->m_sdlWindow, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_SetWindowBordered(window, SDL_TRUE); + SDL_SetWindowSize(window, windowSize[0], windowSize[1]); + SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); parent->m_windowMode = WindowMode::Normal; parent->m_windowSize = windowSize; @@ -487,11 +493,12 @@ private: void setMaximizedWindow() override { if (parent->m_windowMode != WindowMode::Maximized) { - if (parent->m_windowMode == WindowMode::Fullscreen || parent->m_windowMode == WindowMode::Borderless) { + if (parent->m_windowMode == WindowMode::Fullscreen) SDL_SetWindowFullscreen(parent->m_sdlWindow, 0); + else if (parent->m_windowMode == WindowMode::Borderless) SDL_SetWindowBordered(parent->m_sdlWindow, SDL_TRUE); - } + SDL_RestoreWindow(parent->m_sdlWindow); SDL_MaximizeWindow(parent->m_sdlWindow); parent->m_windowMode = WindowMode::Maximized; } @@ -499,19 +506,24 @@ private: void setBorderlessWindow() override { if (parent->m_windowMode != WindowMode::Borderless) { - SDL_SetWindowFullscreen(parent->m_sdlWindow, 0); + if (parent->m_windowMode == WindowMode::Fullscreen) + SDL_SetWindowFullscreen(parent->m_sdlWindow, 0); + else if (parent->m_windowMode == WindowMode::Maximized) + SDL_RestoreWindow(parent->m_sdlWindow); + SDL_SetWindowBordered(parent->m_sdlWindow, SDL_FALSE); - SDL_MaximizeWindow(parent->m_sdlWindow); parent->m_windowMode = WindowMode::Borderless; SDL_DisplayMode actualDisplayMode; - if (SDL_GetWindowDisplayMode(parent->m_sdlWindow, &actualDisplayMode) == 0) { + if (SDL_GetDesktopDisplayMode(SDL_GetWindowDisplayIndex(parent->m_sdlWindow), &actualDisplayMode) == 0) { parent->m_windowSize = {(unsigned)actualDisplayMode.w, (unsigned)actualDisplayMode.h}; + SDL_SetWindowPosition(parent->m_sdlWindow, 0, 0); + SDL_SetWindowSize(parent->m_sdlWindow, parent->m_windowSize[0], parent->m_windowSize[1]); parent->m_renderer->setScreenSize(parent->m_windowSize); parent->m_application->windowChanged(parent->m_windowMode, parent->m_windowSize); } else { - Logger::error("Couldn't get window display mode!"); + Logger::error("Couldn't get desktop display mode!"); } } } @@ -607,14 +619,14 @@ private: if (event.window.event == SDL_WINDOWEVENT_MAXIMIZED || event.window.event == SDL_WINDOWEVENT_RESTORED) { auto windowFlags = SDL_GetWindowFlags(m_sdlWindow); - if (windowFlags & SDL_WINDOW_MAXIMIZED) { + if (windowFlags & SDL_WINDOW_MAXIMIZED) m_windowMode = WindowMode::Maximized; - } else if (windowFlags & SDL_WINDOW_FULLSCREEN || windowFlags & SDL_WINDOW_FULLSCREEN_DESKTOP) { - if (m_windowMode != WindowMode::Fullscreen && m_windowMode != WindowMode::Borderless) - m_windowMode = WindowMode::Fullscreen; - } else { + else if (windowFlags & SDL_WINDOW_FULLSCREEN) + m_windowMode = WindowMode::Fullscreen; + else if (windowFlags & SDL_WINDOW_BORDERLESS) + m_windowMode = WindowMode::Borderless; + else m_windowMode = WindowMode::Normal; - } m_application->windowChanged(m_windowMode, m_windowSize); diff --git a/source/client/StarClientApplication.cpp b/source/client/StarClientApplication.cpp index fb26dcc..be5e649 100644 --- a/source/client/StarClientApplication.cpp +++ b/source/client/StarClientApplication.cpp @@ -258,7 +258,6 @@ void ClientApplication::windowChanged(WindowMode windowMode, Vec2U screenSize) { config->set("maximized", true); config->set("fullscreen", false); config->set("borderless", false); - config->set("windowedResolution", jsonFromVec2U(screenSize)); } else { config->set("maximized", false); config->set("fullscreen", false);