Skip to content

Commit

Permalink
SDL: Use the window display index when querying display modes
Browse files Browse the repository at this point in the history
  • Loading branch information
SupSuper committed Apr 29, 2021
1 parent 5219b1e commit f69c548
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 33 deletions.
5 changes: 3 additions & 2 deletions backends/graphics/openglsdl/openglsdl-graphics.cpp
Expand Up @@ -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;
}

Expand Down
16 changes: 1 addition & 15 deletions backends/graphics/sdl/sdl-graphics.h
Expand Up @@ -179,17 +179,6 @@ class SdlGraphicsManager : virtual public WindowedGraphicsManager, public Common
}
#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 =
Expand All @@ -205,10 +194,7 @@ class SdlGraphicsManager : virtual public WindowedGraphicsManager, public Common

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
Expand Down
19 changes: 5 additions & 14 deletions backends/graphics/surfacesdl/surfacesdl-graphics.cpp
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions backends/graphics/surfacesdl/surfacesdl-graphics.h
Expand Up @@ -171,6 +171,8 @@ class SurfaceSdlGraphicsManager : public SdlGraphicsManager {

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. */
Expand Down
13 changes: 11 additions & 2 deletions backends/platform/sdl/sdl-window.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions backends/platform/sdl/sdl-window.h
Expand Up @@ -115,6 +115,11 @@ class SdlWindow {
*/
SDL_Window *getSDLWindow() const { return _window; }

/**
* @return The display containing the ScummVM window.
*/
int getDisplayIndex() const;

/**
* Creates or updates the SDL window.
*
Expand Down

0 comments on commit f69c548

Please sign in to comment.