From 60f1fd98aa29080e13160fec6a2ec2acde09184e Mon Sep 17 00:00:00 2001 From: SupSuper Date: Thu, 29 Apr 2021 05:18:37 +0100 Subject: [PATCH] SDL: Use the window display index when querying display modes --- .../graphics/openglsdl/openglsdl-graphics.cpp | 5 +++-- backends/graphics/sdl/sdl-graphics.h | 16 +--------------- .../surfacesdl/surfacesdl-graphics.cpp | 19 +++++-------------- .../graphics/surfacesdl/surfacesdl-graphics.h | 2 ++ backends/platform/sdl/sdl-window.cpp | 13 +++++++++++-- backends/platform/sdl/sdl-window.h | 5 +++++ 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/backends/graphics/openglsdl/openglsdl-graphics.cpp b/backends/graphics/openglsdl/openglsdl-graphics.cpp index b793962d0d1..09f86495eda 100644 --- a/backends/graphics/openglsdl/openglsdl-graphics.cpp +++ b/backends/graphics/openglsdl/openglsdl-graphics.cpp @@ -140,10 +140,11 @@ OpenGLSdlGraphicsManager::OpenGLSdlGraphicsManager(SdlEventSource *eventSource, // Retrieve a list of working fullscreen modes #if SDL_VERSION_ATLEAST(2, 0, 0) - const int numModes = SDL_GetNumDisplayModes(0); + const int display = _window->getDisplayIndex(); + const int numModes = SDL_GetNumDisplayModes(display); for (int i = 0; i < numModes; ++i) { SDL_DisplayMode mode; - if (SDL_GetDisplayMode(0, i, &mode)) { + if (SDL_GetDisplayMode(display, i, &mode)) { continue; } diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index 256e8bec798..acac7ffb746 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -179,17 +179,6 @@ protected: } #endif } - /** - * Gets the display index that the ScummVM window is on - */ - void getWindowDisplayIndexFromSdl(int *index) const { -#if SDL_VERSION_ATLEAST(2, 0, 0) - assert(_window); - *index = SDL_GetWindowDisplayIndex(_window->getSDLWindow()); -#else - *index = 0; -#endif - } void getDisplayDpiFromSdl(float *dpi, float *defaultDpi) const { const float systemDpi = @@ -205,10 +194,7 @@ protected: if (dpi) { #if SDL_VERSION_ATLEAST(2, 0, 4) - int displayIndex = 0; - getWindowDisplayIndexFromSdl(&displayIndex); - - if (SDL_GetDisplayDPI(displayIndex, NULL, dpi, NULL) != 0) { + if (SDL_GetDisplayDPI(_window->getDisplayIndex(), NULL, dpi, NULL) != 0) { *dpi = systemDpi; } #else diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index bf36ca8339f..8f0be68ba4e 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -521,18 +521,8 @@ void SurfaceSdlGraphicsManager::detectSupportedFormats() { #if SDL_VERSION_ATLEAST(2, 0, 0) { - SDL_Window *window = _window->getSDLWindow(); - if (window == nullptr) { - error("Could not find ScummVM window for retrieving default display mode"); - } - - const int displayIndex = SDL_GetWindowDisplayIndex(window); - if (displayIndex < 0) { - error("Could not find ScummVM window display index"); - } - SDL_DisplayMode defaultMode; - if (SDL_GetDesktopDisplayMode(displayIndex, &defaultMode) != 0) { + if (SDL_GetDesktopDisplayMode(_window->getDisplayIndex(), &defaultMode) != 0) { error("Could not get default system display mode"); } @@ -805,7 +795,7 @@ void SurfaceSdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFo _transactionDetails.sizeChanged = true; } -static void fixupResolutionForAspectRatio(AspectRatio desiredAspectRatio, int &width, int &height) { +void SurfaceSdlGraphicsManager::fixupResolutionForAspectRatio(AspectRatio desiredAspectRatio, int &width, int &height) const { assert(&width != &height); if (desiredAspectRatio.isAuto()) @@ -821,10 +811,11 @@ static void fixupResolutionForAspectRatio(AspectRatio desiredAspectRatio, int &w uint bestMetric = (uint)-1; // Metric is wasted space #if SDL_VERSION_ATLEAST(2, 0, 0) - const int numModes = SDL_GetNumDisplayModes(0); + const int display = _window->getDisplayIndex(); + const int numModes = SDL_GetNumDisplayModes(display); SDL_DisplayMode modeData, *mode = &modeData; for (int i = 0; i < numModes; ++i) { - if (SDL_GetDisplayMode(0, i, &modeData)) { + if (SDL_GetDisplayMode(display, i, &modeData)) { continue; } #else diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h index da4ed8a8ff8..628a55740da 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.h +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h @@ -171,6 +171,8 @@ protected: virtual void setupHardwareSize(); + void fixupResolutionForAspectRatio(AspectRatio desiredAspectRatio, int &width, int &height) const; + #if SDL_VERSION_ATLEAST(2, 0, 0) /* SDL2 features a different API for 2D graphics. We create a wrapper * around this API to keep the code paths as close as possible. */ diff --git a/backends/platform/sdl/sdl-window.cpp b/backends/platform/sdl/sdl-window.cpp index 2ec160ddb4d..2a53429c6e6 100644 --- a/backends/platform/sdl/sdl-window.cpp +++ b/backends/platform/sdl/sdl-window.cpp @@ -232,9 +232,8 @@ bool SdlWindow::getSDLWMInformation(SDL_SysWMinfo *info) const { Common::Rect SdlWindow::getDesktopResolution() { #if SDL_VERSION_ATLEAST(2, 0, 0) - int displayIndex = _window ? SDL_GetWindowDisplayIndex(_window) : 0; SDL_DisplayMode displayMode; - if (!SDL_GetDesktopDisplayMode(displayIndex, &displayMode)) { + if (!SDL_GetDesktopDisplayMode(getDisplayIndex(), &displayMode)) { _desktopRes = Common::Rect(displayMode.w, displayMode.h); } #endif @@ -265,6 +264,16 @@ SDL_Surface *copySDLSurface(SDL_Surface *src) { return res; } +int SdlWindow::getDisplayIndex() const { + if (_window) { + int displayIndex = SDL_GetWindowDisplayIndex(_window); + if (displayIndex >= 0) + return displayIndex; + } + // Default to primary display + return 0; +} + bool SdlWindow::createOrUpdateWindow(int width, int height, uint32 flags) { if (_inputGrabState) { flags |= SDL_WINDOW_INPUT_GRABBED; diff --git a/backends/platform/sdl/sdl-window.h b/backends/platform/sdl/sdl-window.h index b84cbd60396..29fe7f43f2d 100644 --- a/backends/platform/sdl/sdl-window.h +++ b/backends/platform/sdl/sdl-window.h @@ -115,6 +115,11 @@ public: */ SDL_Window *getSDLWindow() const { return _window; } + /** + * @return The display containing the ScummVM window. + */ + int getDisplayIndex() const; + /** * Creates or updates the SDL window. *