Skip to content

feat: remember position of game window #9853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

usidedown
Copy link
Contributor

Solves #1228

Saves the position of the window in fheroes2.conf.
The position is captured every time the window is moved, and saved to settings when the game exits.
It's possible to capture only when the game exits, but then we might not save the correct position if the user swaps to Fullscreen mode before quitting.

@usidedown usidedown force-pushed the feature/remember-window-pos branch 3 times, most recently from 15d83e0 to 7af36b3 Compare May 28, 2025 14:15
@usidedown usidedown force-pushed the feature/remember-window-pos branch 2 times, most recently from 016dcfd to 8f8b6cf Compare May 28, 2025 17:23
@usidedown usidedown force-pushed the feature/remember-window-pos branch 2 times, most recently from 0e4e728 to fbf8742 Compare May 28, 2025 18:10
@usidedown usidedown force-pushed the feature/remember-window-pos branch from fbf8742 to 0e0448b Compare May 28, 2025 18:16
@usidedown usidedown marked this pull request as ready for review May 28, 2025 18:54
@usidedown usidedown requested a review from oleg-derevenetz May 28, 2025 18:54
@oleg-derevenetz oleg-derevenetz added improvement New feature, request or improvement ui UI/GUI related stuff labels May 28, 2025
@oleg-derevenetz oleg-derevenetz added this to the 1.1.9 milestone May 28, 2025
Copy link
Owner

@ihhub ihhub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @usidedown , I left few comments in this pull request. Would you mind to take a look at them when you have time?

Comment on lines +976 to +982
if ( pos == fheroes2::Point{ -1, -1 } ) {
_prevWindowPos = { SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED };
}
else {
_prevWindowPos = pos;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must have a check whether the position is within the screen area. Moreover, the position plus the size of the windows must be within the screen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on the fence regarding these checks. They seem to be mostly useful if the user messes with fheroes2.cfg.

My main issue is that when setWindowPos is called _currentScreenResolution isn't set yet. Even if I postpone the check to engine::allocate, _currentScreenResolution is still not set.
Moreover, it's legal to be in a position where pos + size exceeds the screen size. Probably not useful even if you have multiple displays, but still valid.
What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the following situation:

  • make the resolution 1920x1080
  • move the window to the left part of the display
  • change resolution to 640x480

As a result, the whole window is going to be invisible and outside the display. At very least we need to have a mechanism if a user messes up and they would always blame us :)

Or I might be wrong as SDL handles such cases so we don't need to do anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right!
I've implemented the check.
I believe it is better to check when the window is created rather then when the position is set to avoid race conditions.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Clang-Tidy found issue(s) with the introduced code (1/1)

@usidedown usidedown force-pushed the feature/remember-window-pos branch from ecf3cb6 to d2fa303 Compare June 2, 2025 14:39
@usidedown usidedown requested a review from ihhub June 7, 2025 08:21
@ihhub ihhub modified the milestones: 1.1.9, 1.1.10 Jun 7, 2025
@ihhub
Copy link
Owner

ihhub commented Jun 7, 2025

We will merge this pull request after the upcoming release.

Copy link
Owner

@ihhub ihhub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @usidedown , I put just few more minor comment. Could you please check them and see if they make sense?

@@ -825,6 +825,9 @@ namespace
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
#endif

// update the window position, in case we get queried for it
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Very minor] Since every other comment starts from a capital letter I suggest to do the same here.

Comment on lines +976 to +982
if ( pos == fheroes2::Point{ -1, -1 } ) {
_prevWindowPos = { SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED };
}
else {
_prevWindowPos = pos;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the following situation:

  • make the resolution 1920x1080
  • move the window to the left part of the display
  • change resolution to 640x480

As a result, the whole window is going to be invisible and outside the display. At very least we need to have a mechanism if a user messes up and they would always blame us :)

Or I might be wrong as SDL handles such cases so we don't need to do anything.

@@ -398,6 +403,9 @@ std::string Settings::String() const
os << std::endl << "# video mode: in-game width x in-game height : on-screen width x on-screen height" << std::endl;
os << "videomode = " << display.width() << "x" << display.height() << ":" << display.screenSize().width << "x" << display.screenSize().height << std::endl;

os << std::endl << "# position of the game's window" << std::endl;
os << "main window position = [ " << _windowPos.x << ", " << _windowPos.y << " ]" << std::endl;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we name the parameter as game window position as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement New feature, request or improvement ui UI/GUI related stuff
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants