SFML - Part 3 - Display a sprite


This post presents how to display a sprite in fullscreen mode with SFML. Example presented on this page has been prepared with the following software versions:

The following video is a preview of the result:


The first part of the code switch the display in fullscreen mode with the same resolution than the desktop. The window object (sf::RenderWindow window is created with the sf::Style::Fullscreen attribute.

The vertical synchronisation is enabled thanks to setVerticalSyncEnabled(true). It will limit the number of frames displayed to the refresh rate of the monitor. This can avoid some visual artifacts, and limit the framerate to a good value.

The sprite is created with the sf::Texture object and load file file with the member function loadFromFile(). Note that the sprite (or texture) is smoothed to get a better result on screen, the texture appears smoother so that pixels are less noticeable. However if you want the texture to look exactly the same as its source file, comment this line.

Once every setup is ready, the main loop starts. It loops until the main window is open, it means that if the user or an event close the window, the loop will exit.

The main loop repeats the 4 following actions:

Note that the sprite is displayed in the middle of the screen thanks to window.getSize().

Example and source code

The following program create a fullscreen mode window and display the sprite on the screen.

 * \file    main.cpp
 * \brief   start SFML in full screen and display a sprite (https://www.lucidar.me/en/sfml/sfml-part-3-display-a-sprite/)
 * \author  Philippe Lucidarme
 * \version 1.0
 * \date    13/07/2018

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

int main()
    // _____________________
    // ::: Create window :::

    // Create a window with the same pixel depth as the desktop
    sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode();

    sf::RenderWindow window(sf::VideoMode(  desktopMode.width,
                            "SFML part 3",

    // Enable vertical sync. (vsync)
    window.setVerticalSyncEnabled (true);

    // ____________________
    // ::: Load texture :::

    // Create texture from PNG file
    sf::Texture texture;
    if (!texture.loadFromFile("../sfml-icon-small.png"))
        std::cerr << "Error while loading texture" << std::endl;
        return -1;
    // Enable the smooth filter. The texture appears smoother so that pixels are less noticeable.

    // _______________________________________
    // ::: Create sprite and apply texture :::

    // Create the sprite and apply the texture
    sf::Sprite sprite;
    sf::FloatRect spriteSize=sprite.getGlobalBounds();

    // _________________
    // ::: Main loop :::

    sf::Clock timer;
    while (window.isOpen())
        // Display fps
        std::cout << int(1./timer.restart().asSeconds()) << " fps" << std::endl;

        // Process events
        sf::Event event;
        while (window.pollEvent(event))
            // Close the window if a key is pressed or if requested
            if (event.type == sf::Event::Closed) window.close();
            if (event.type == sf::Event::KeyPressed) window.close();

        // Clear the window and apply grey background
        window.clear( sf::Color(127,127,127));

        // Draw the sprite in the middle of the screen
        sprite.setPosition(window.getSize().x/2., window.getSize().y/2.);

        // Update display and wait for vsync
    return 0;


Qt project for this example can be downloaded here:


See also

Last update : 07/13/2018