SFML - Part 2 - Get available screen resolutions

Introduction

This post presents how to get available screen resolution with SFML. Example presented on this page has been prepared with the following software versions:

The following video is a preview of the result:

Explainations

The function getFullscreenModes() returns an array (std::vector) containing all the video modes supported in fullscreen mode.

Once the function is called, the array is populated, and we can simply access to the following attributes:

It is also possible to get the current desktop video mode with the function getDesktopMode(). This function returns a single VideoMode containing the same attributes as previously.

Example and source code

The following program lists all the available fullscreen resolutions and display, at the end, the current desktop resolution.

/*!
 * \file    main.cpp
 * \brief   Display the list of available video mode (https://www.lucidar.me/en/sfml/sfml-part-2-get-available-resolution/)
 * \author  Philippe Lucidarme (from official SFML tutorial)
 * \version 1.0
 * \date    12/07/2018
 */

// SFML libraries
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{

    // Display the list of all the video modes available for fullscreen
    std::vector<sf::VideoMode> modes = sf::VideoMode::getFullscreenModes();

    // Display each mode
    for (std::size_t i = 0; i < modes.size(); ++i)
    {
        sf::VideoMode mode = modes[i];
        std::cout << "Mode #" << i << "\t"
                  << mode.width << "x" << mode.height << " \t "
                  << mode.bitsPerPixel << " bpp" << std::endl;
    }

    // Get and display desktop mode
    sf::VideoMode mode = sf::VideoMode::getDesktopMode();
    std::cout << "Desktop"  << "\t"
              << mode.width << "x" << mode.height << " \t "
              << mode.bitsPerPixel << " bpp" << std::endl;

    // End of application
    return 0;
}

The previous program should display something like this (since it is quite long, the list has been shortened):

Mode #0 1920x1080    32 bpp
Mode #1 1680x1050    32 bpp
Mode #2 1400x1050    32 bpp
Mode #3 1280x1024    32 bpp
Mode #4 1280x960     32 bpp
Mode #5 1152x864     32 bpp
Mode #6 1024x768     32 bpp
[...]
Mode #75    1280x1024    1 bpp
Mode #76    1280x960     1 bpp
Mode #77    1152x864     1 bpp
Mode #78    1024x768     1 bpp
Mode #79    800x600      1 bpp
Mode #80    720x400      1 bpp
Mode #81    640x480      1 bpp
Mode #82    640x400      1 bpp
Mode #83    640x350      1 bpp
Desktop 1920x1080    24 bpp

Download

Qt project for this example can be downloaded here:

sfml-part-02.zip

See also


Last update : 07/13/2018