~/C++ SFML Quickstart Guide

Feb 14, 2021


This guide provides a concise SFML overview for C++ graphics and multimedia development.

Installing SFML

On Ubuntu, install with:

1
sudo apt-get install libsfml-dev

On Windows, download from SFML Downloads.

Basic Window Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.display();
    }
    return 0;
}

Compile with:

1
g++ main.cpp -o app -lsfml-graphics -lsfml-window -lsfml-system

Drawing a Shape

1
2
3
4
5
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
window.clear();
window.draw(shape);
window.display();

Include this inside the rendering loop before window.display(); and after window.clear();.

Handling Input

Detect key press with:

1
2
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
    window.close();

Further Learning

See the full SFML tutorials to go in-depth, including graphics, audio, and network capabilities. For C++ syntax, refer to cppreference.

Tags: [sfml] [cpp] [graphics]