Search code examples
c++linuxsdl-2screen-resolution

SDL desktop resolution detection in Linux


I have got some reports that for some Linux users, especially those who use SteamOS, my game opens in wrong resolution. The game tries to detect the current desktop resolution and create a borderless fullscreen window by using that resolution.

For example, the resolution of the SteamOS is usually 1920x1080, but the SDL reports it to be something like 4096x2160! Thus when the game starts, players see only lower-left portion of the game area.

My function for detecting screen resolution is the following:

bool View::checkDisplaySize() {
    int display_count = 0;
    int display_index = 0;
    int mode_index = 0;
    SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };

    if ((display_count = SDL_GetNumVideoDisplays()) < 1) {
        printf("SDL_GetNumVideoDisplays returned: %i", display_count);
        return false;
    }

    if (SDL_GetDisplayMode(display_index, mode_index, &mode) != 0) {
        printf("SDL_GetDisplayMode failed: %s", SDL_GetError());
        return false;
    }

    m_display.w = mode.w;
    m_display.h = mode.h;

    return true;
}

I then use the information which is stored in m_display struct to enter fullscreen. Window creation and going to fullscreen are in separate functions, because players who use some other Linux distro than SteamOS have also an option to enter to windowed mode during the game:

window = SDL_CreateWindow("Game", 0, 0, m_display.w, m_display.h, window_flags);

...

SDL_SetWindowBordered(window, SDL_FALSE);
SDL_SetWindowPosition(window, 0, 0);
SDL_SetWindowSize(window, m_display.w, m_display.h);
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);

For me this has worked without problems with all the Linux computers I have tested. I haven't been able to reproduce the problem in my own test environment.

My questions are:

  • Is this a problem in SDL implementation in Linux or am I doing something wrong?

And, if indeed I am to blame here:

  • Is this correct way to query the screen resolution?
  • If not, should I use some other method to query resolution (which is more reliable)?

Solution

  • The documentation for SDL_GetDisplayMode has an enlightening remark:

    The display modes are sorted in this priority:

    • width -> largest to smallest
    • height -> largest to smallest
    • ...

    That means that what you are actually querying seems to be the largest supported resolution for the display, rather than the actual resolution.

    You'll probably want to use SDL_GetCurrentDisplayMode or SDL_GetDesktopDisplayMode to get the currently active display mode for the display.