TGUI

GUI library for SFML

Starting with TGUI v0.6

There are five things you need to change in your code to get tgui working.
The first thing to do is including it. TGUI will include “SFML/Graphics.hpp” for you.

#include <TGUI/TGUI.hpp>

The next thing to do is creating the gui.

sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
tgui::Gui gui(window);

Since sfml no longer provides a default font, you will have to set your own font.

gui.setGlobalFont("TGUI/fonts/DejaVuSans.ttf"); // Load the font directly
gui.setGlobalFont(font); // or make a copy of a font that you had already loaded

When you will have widgets, they will need to receive events. Otherwise they wouldn’t know if your mouse is on top of them or not. Every time you receive an event, you will have to tell the gui about this event. So in your event loop you must add the following line:

gui.handleEvent(event);

The last thing to do is drawing the widgets on the screen. For this you must call the draw function of the gui. All widgets will be drawn on the screen in the order that you created them. So if your widgets overlap each other then the last created widget will be drawn in front.

gui.draw();

So the full code will look like this:

#include <TGUI/TGUI.hpp>

int main()
{
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("TGUI/fonts/DejaVuSans.ttf");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
    }

    return EXIT_SUCCESS;
}

Next: Working with widgets

Privacy Policy