From e3612afbd3ee656ec33003079578335a64f7fa4d Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 21 Mar 2023 00:23:42 -0700 Subject: [PATCH] Store display index of window to settings I have this annoying issue where the game will open on the wrong monitor in fullscreen mode, because that monitor is considered to be display 0, whereas the primary monitor I want is display 1. To mitigate this somewhat, the game now stores the display index that it was closed on, and will save it to settings. Then the next time the game opens, it will open on that display index. This should work pretty well as long as you aren't changing your monitor setup constantly. Of course, none of this applies if your window manager is busted. For example, on GNOME Wayland, which is what I use, in windowed mode the game will always open on the monitor the cursor is on, and it won't even be centered in the monitor. But it works fine if I use XWayland via SDL_VIDEODRIVER=x11. --- desktop_version/src/Game.cpp | 6 ++++++ desktop_version/src/Screen.cpp | 26 +++++++++++++++++++++++--- desktop_version/src/Screen.h | 1 + desktop_version/src/ScreenSettings.h | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index 82e81dae..221d7e99 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -4374,6 +4374,10 @@ void Game::deserializesettings(tinyxml2::XMLElement* dataNode, struct ScreenSett screen_settings->linearFilter = help.Int(pText); } + if (SDL_strcmp(pKey, "window_display") == 0) + { + screen_settings->windowDisplay = help.Int(pText); + } if (SDL_strcmp(pKey, "window_width") == 0) { screen_settings->windowWidth = help.Int(pText); @@ -4713,6 +4717,8 @@ void Game::serializesettings(tinyxml2::XMLElement* dataNode, const struct Screen xml::update_tag(dataNode, "useLinearFilter", (int) screen_settings->linearFilter); + xml::update_tag(dataNode, "window_display", screen_settings->windowDisplay); + xml::update_tag(dataNode, "window_width", screen_settings->windowWidth); xml::update_tag(dataNode, "window_height", screen_settings->windowHeight); diff --git a/desktop_version/src/Screen.cpp b/desktop_version/src/Screen.cpp index 6921c183..508bc3d4 100644 --- a/desktop_version/src/Screen.cpp +++ b/desktop_version/src/Screen.cpp @@ -16,6 +16,7 @@ void ScreenSettings_default(struct ScreenSettings* _this) { + _this->windowDisplay = 0; _this->windowWidth = SCREEN_WIDTH_PIXELS * 2; _this->windowHeight = SCREEN_HEIGHT_PIXELS * 2; _this->fullscreen = false; @@ -29,6 +30,7 @@ void Screen::init(const struct ScreenSettings* settings) { m_window = NULL; m_renderer = NULL; + windowDisplay = settings->windowDisplay; windowWidth = settings->windowWidth; windowHeight = settings->windowHeight; isWindowed = !settings->fullscreen; @@ -53,8 +55,8 @@ void Screen::init(const struct ScreenSettings* settings) m_window = SDL_CreateWindow( "VVVVVV", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED_DISPLAY(windowDisplay), + SDL_WINDOWPOS_CENTERED_DISPLAY(windowDisplay), SCREEN_WIDTH_PIXELS * 2, SCREEN_HEIGHT_PIXELS * 2, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI @@ -101,6 +103,13 @@ void Screen::destroy(void) void Screen::GetSettings(struct ScreenSettings* settings) { + windowDisplay = SDL_GetWindowDisplayIndex(m_window); + if (windowDisplay < 0) + { + vlog_error("Error: could not get display index: %s", SDL_GetError()); + windowDisplay = 0; + } + settings->windowDisplay = windowDisplay; settings->windowWidth = windowWidth; settings->windowHeight = windowHeight; @@ -134,6 +143,13 @@ void Screen::LoadIcon(void) void Screen::ResizeScreen(int x, int y) { + windowDisplay = SDL_GetWindowDisplayIndex(m_window); + if (windowDisplay < 0) + { + vlog_error("Error: could not get display index: %s", SDL_GetError()); + windowDisplay = 0; + } + if (x != -1 && y != -1) { // This is a user resize! @@ -163,7 +179,11 @@ void Screen::ResizeScreen(int x, int y) if (x != -1 && y != -1) { SDL_SetWindowSize(m_window, windowWidth, windowHeight); - SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_SetWindowPosition( + m_window, + SDL_WINDOWPOS_CENTERED_DISPLAY(windowDisplay), + SDL_WINDOWPOS_CENTERED_DISPLAY(windowDisplay) + ); } } } diff --git a/desktop_version/src/Screen.h b/desktop_version/src/Screen.h index 7573ef8f..17fefddb 100644 --- a/desktop_version/src/Screen.h +++ b/desktop_version/src/Screen.h @@ -31,6 +31,7 @@ public: bool isForcedFullscreen(void); + int windowDisplay; int windowWidth; int windowHeight; bool isWindowed; diff --git a/desktop_version/src/ScreenSettings.h b/desktop_version/src/ScreenSettings.h index 5369844d..8c0bc04e 100644 --- a/desktop_version/src/ScreenSettings.h +++ b/desktop_version/src/ScreenSettings.h @@ -11,6 +11,7 @@ enum struct ScreenSettings { + int windowDisplay; int windowWidth; int windowHeight; bool fullscreen;