Skip to content

Commit

Permalink
Store display index of window to settings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
InfoTeddy committed Mar 21, 2023
1 parent 5499063 commit e3612af
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions desktop_version/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 23 additions & 3 deletions desktop_version/src/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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)
);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions desktop_version/src/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Screen

bool isForcedFullscreen(void);

int windowDisplay;
int windowWidth;
int windowHeight;
bool isWindowed;
Expand Down
1 change: 1 addition & 0 deletions desktop_version/src/ScreenSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum

struct ScreenSettings
{
int windowDisplay;
int windowWidth;
int windowHeight;
bool fullscreen;
Expand Down

0 comments on commit e3612af

Please sign in to comment.