Skip to content

Commit 18120b4

Browse files
Bring the workaround back
1 parent e03cb52 commit 18120b4

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/engine/screen.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -830,14 +830,32 @@ namespace
830830
#else
831831
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
832832
#endif
833+
834+
// If the window size was manually changed to a non-standard one (which does not correspond to any of the resolutions supported by the display),
835+
// then after switching to full-screen mode, the in-game display area may not take up the entire screen and black bars will remain on the sides
836+
// of the screen. In this case, even if it is specified to use the SDL_WINDOW_FULLSCREEN, the SDL_WINDOW_FULLSCREEN_DESKTOP will still be used
837+
// under the hood. To avoid this, we need to remember the window size (to restore it later when turning off full-screen mode) and then set the
838+
// window size to the size of the in-game display area before switching to the full-screen mode.
839+
SDL_GetWindowSize( _window, &_windowedSize.width, &_windowedSize.height );
840+
841+
if ( const fheroes2::Display & display = fheroes2::Display::instance(); display.width() != 0 && display.height() != 0 ) {
842+
assert( display.screenSize().width >= display.width() && display.screenSize().height >= display.height() );
843+
844+
SDL_SetWindowSize( _window, display.screenSize().width, display.screenSize().height );
845+
}
833846
}
834847

835-
const int returnCode = SDL_SetWindowFullscreen( _window, flags );
836-
if ( returnCode < 0 ) {
848+
if ( const int returnCode = SDL_SetWindowFullscreen( _window, flags ); returnCode < 0 ) {
837849
ERROR_LOG( "Failed to set fullscreen mode flags. The error value: " << returnCode << ", description: " << SDL_GetError() )
838850
}
839851

840852
_syncFullScreen();
853+
854+
// If the full-screen mode has been turned off, then restore the remembered window size.
855+
if ( !isFullScreen() && _windowedSize.width != 0 && _windowedSize.height != 0 ) {
856+
SDL_SetWindowSize( _window, _windowedSize.width, _windowedSize.height );
857+
}
858+
841859
_retrieveWindowInfo();
842860
_toggleMouseCaptureMode();
843861
}
@@ -954,6 +972,8 @@ namespace
954972
fheroes2::Size _currentScreenResolution;
955973
fheroes2::Rect _activeWindowROI;
956974

975+
fheroes2::Size _windowedSize;
976+
957977
bool _isVSyncEnabled{ false };
958978

959979
RenderEngine() = default;
@@ -987,6 +1007,7 @@ namespace
9871007
}
9881008

9891009
_driverIndex = -1;
1010+
_windowedSize = {};
9901011
}
9911012

9921013
void render( const fheroes2::Display & display, const fheroes2::Rect & roi ) override

0 commit comments

Comments
 (0)