Free collection of beautiful vector icons for your web pages.

SFML - Part 10 - Text and font

This article is part of a tutorial dedicated to SFML. The reader may consult the other parts of the tutorial by following this link : SFML tutorial.


This post presents how to display text with SFML. Example presented on this page has been prepared with the following software versions:

The following video is a preview of the result:

Example and source code

This example shows how to display text with SFML:

 * \file    main.cpp
 * \brief   start SFML in full screen and display a sprite (
 * \author  Philippe Lucidarme
 * \version 1.0
 * \date    12/18/2016

// 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 font :::

    sf::Font font;
    if (!font.loadFromFile("../example_font.ttf"))
        std::cerr << ".Error while loading font" << std::endl;
        return -1;

    sf::Text text;
    text.setFont(font); // font est un sf::Font
    text.setCharacterSize(60); // exprimée en pixels, pas en points !
    text.setOrigin(text.getLocalBounds().width/2. , text.getLocalBounds().height/2.);

    std::cout << text.getLocalBounds().width << "\t" << text.getLocalBounds().height;
    // _________________
    // ::: Main loop :::

    sf::Clock timer;
    while (window.isOpen())
        // 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));
        text.setPosition(window.getSize().x/2. , window.getSize().y/2.);
        // Update display and wait for vsync
    return 0;


Qt project, files, shaders and source codes can be downloaded for GitLab:

Source code on GitLab

See also

Last update : 03/15/2021