ihhub / fheroes2

fheroes2 is a recreation of Heroes of Might and Magic II game engine.
https://ihhub.github.io/fheroes2/
GNU General Public License v2.0
2.71k stars 376 forks source link

Game always defaults to launching on main screen in a multi-screen setup #6683

Open kfishster opened 1 year ago

kfishster commented 1 year ago

Preliminary checks

Platform

macOS

Describe the bug

I use my computer alongside an external display, so I have two screens to work with. In MacOS, there is a concept of a "main display" which will have your dock + top toolbar, with all other screens being "extended displays". I've noticed that regardless of which screen I open fheroes2, it will always run on my "main" screen. Additionally, if I disable full screen mode, and move the window to my second screen, the window position will not persist to the subsequent boot.

There are two problems here, and thankfully one has a solution. For the second issue (window position is not persisted) - after looking through the SDL code that initializes the game window, I found that there is some functionality that stores _prevWindowPos, although it seems like it's not fully hooked up. #6667 seems to address this, although that PR hasn't had any movement since the end of the last year.

For the first issue (game always launches on "main" screen), this is likely due to SDL defaults. The default position for the window is defined in this line:

_prevWindowPos( SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED )

which is a macro for (this link is to the current SDL3 implementation, but it's the same in SDL2)

#define SDL_WINDOWPOS_CENTERED         SDL_WINDOWPOS_CENTERED_DISPLAY(0)

The 0 index in that macro is the index of the display, so this will always default to the first display, which, in Mac, will be the "main". This might also affect Windows, although I am not sure if it has the same aspect of a "main display" (I believe it does, but I don't have a Windows machine handy to double check at the moment).

The possible solution here would be to

Not sure if this will affect other platforms where screens might also be emulated (Android or PS Vita?).

Wanted to float this idea, and given that this is a fairly straighforward fix, I'd be happy to make the change if you think it's worth patching!

Save file

N/A

Additional info

No response

ihhub commented 1 year ago

We need to remember display ID upon closing the application and its position.