A simple GUI module for SFML.
- Spritesheet based: a single image file to customize widget style
- Simple events: set a
std::function<void(void)>callback on widgets to trigger functions on UI events. - Layouts: automatically align content without computing positions
- Author: Alexandre Bodelot alexandre.bodelot@gmail.com
- License: MIT License (See LICENSE file)
- Download SFML 2.6.x for your platform
- Latest CMake Installer - version 3.20 or higher
- Visual Studio 2017 or later (select complete "Desktop C++ Development" workload)
- CMake for Visual Studio (download using Visual Studio installer).
- XCode latest with MacOS SDK.
- Follow official guide on installing SFML on MacOS
- Extra Apple Developer Tools. After Xcode installs, run this in Terminal:
sudo xcode-select --install
- After CMake GUI installs on your Mac, make sure to add its CLI to PATH:
sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install
- Use your package manager (
apt-getoryum) to download SFML dev dependencies listed in official docs
- On Windows, open this project in Visual Studio 2022 or later, choose "Release" or "Debug" from top toolbar. Then click "Build" > "Build All".
- If using CMake GUI (all platforms), set "Source Folder" to this project root. Then set "Build Folder" to new relative folder
/buildor/out. See image below. Then click "Configure", choose Unix Makefiles, then click Generate. Finally, open build folder, then runmake allin Terminal.
- Alternatively, you can use the CMake CLI on your Terminal:
mkdir build
cd build
cmake . .
cmake --build . --config Release --target all- Load resources (font, spritesheet) in static class
gui::Theme - Use
gui::Menuto create a new sfml-widgets menu. It needs to be connected to your SFML render window, which is given to the constructor. - Create widgets, add theme to the menu and define callbacks on them. NOTE: widgets must be dynamically allocated (
new). Thegui::Menudestructor will take care of deallocating widgets.
Minimal example:
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Gui/Gui.hpp"
int main()
{
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML Widgets", sf::Style::Close);
//Set FPS limit
//app.setFramerateLimit(60);
// Declare menu
gui::Menu menu(app);
gui::Theme::loadFont("resources/tahoma.ttf");
gui::Theme::loadTexture("resources/texture-default.png");
// Create some button widget
gui::Button* button = new gui::Button("My button");
// Insert button into menu
menu.add(button);
// Define a callback
button->setCallback([] {
std::cout << "click!" << std::endl;
});
// Start the application loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
// Send events to the menu
menu.onEvent(event);
if (event.type == sf::Event::Closed)
app.close();
}
// Optional: clear window with theme background color
app.clear(gui::Theme::windowBgColor);
// Render menu
app.draw(menu);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}demo/demo.cpp conains a more complex example, featuring all widgets.
A simple press button.
A button with enabled/disabled state.
Displays an SFML texture.
It's a simple wrapper around sf::Texture, to display a texture as part of the UI.
A static text element.
It's a simple wrapper around sf::Text, to display a text as part of the UI.
A list of label/value pairs.
Use templates to define value type. Example: gui::OptionsBox<sf::Color>.
Add value with: optionsBox->addItem("Red", sf::Color::Red);
A simple horizontal or vertical progress bar.
orientation:gui::Horizontalorgui::VerticallabelPlacement:gui::LabelNone, orgui::LabelOver, orgui::Outside
Provides an horizontal or vertical slider.
orientation:gui::Horizontalorgui::Vertical
A one-line text editor.
It supports text cursor, and text selection (with mouse or keyboard shortcuts).
Layouts are containers for widgets. They are also widgets themselves, and can be nested!
The special, unique root layout. It behave like a VBoxLayout.
Lines up widgets horizontally.
Use layout->add(widget) to append a widget on a new line.
Lines up widgets vertically.
Use layout->add(widget) to append a widget on a new column.
Manages forms of input widgets and their associated labels.
Use layout->addRow("my label", widget) to add a new line with label on the left, and widget on the right.
To customize the theme, you can:
- Change the theme values (padding, color, font, etc.) defined the static class
gui::Theme. - Use a custom spritesheet image.







