Getting started with SFML

Here’s the most basic SFML application you’ll ever find: 

CPP




#include <SFML/Graphics.hpp>
int main()
{
    sf::Window window(
        sf::VideoMode(640, 480),
        "Hello World");
    return 0;
}


Now to compile the program – since SFML is a dynamically linked library one needs to link the program to the library. This step depends on what component of SFML one is using. Since we are using Graphics.hpp we say:-

g++ main.cpp -lsfml-graphics -lsfml-window -lsfml-system

Assuming the source filename you used main.cpp! The step will be different if someone uses Visual Studio. You can refer here to see how to compile a SFML project with Visual Studio. Now once you run the program you may notice that the window is created but it vanishes automatically. This is because our driver function main creates a window and once it is created it moves to the second statement which is return 0; and hence exits the program! So what we need is a basic loop that will keep running as long as the user doesn’t close the window. So let’s refactor our code and this time understand line-by-line what is actually going on: 

CPP




#include <SFML/Graphics.hpp>
int main()
{
    sf::Window window(
        sf::VideoMode(640, 480),
        "Hello World");
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type ==
            sf::Event::Closed)
                window.close();
    }
    return 0;
}


Before running the program let’s understand what’s going on! Explanation

  1. The first statement creates a window object using the Window constructor. In this, we pass the videomode and title of the window. And by the way, VideoMode is simply a type that defines the dimensions of the window (and bits per pixel but we don’t need that now). And also note that we are using ‘sf’ namespace as all the SFML classes and Methods are defined under this class.
  2. In the second statement we have some new logic. We are basically running a loop as long as the window is open. Turns out the Window class provides a method isOpen which returns whether or not is a window open. Now inside of this loop we check for events. We create an event object which then we later pass in window.pollEvent. What pollEvent method basically does is it checks if  the event queue is not empty. It updates event with the next event in the queue and then in the event loop we check if the event was not sf::Event::Closed which is just a cryptic way of asking SFML did the user X..ed the window? By default nothing happens when the user hits the close button, so we override this behaviour by closing the window.

Note that closing the window doesn’t terminate the program! But since our window is now closed the isOpen method will now return false and we will return out of what’s called – the main loop of the program! Now if you run the program you get a weird looking window (as shown below). Sure you can close it but it looks weird, the previous contents of the screen are mapped to it and look super weird. This is happening because we are not clearing the window!! In the next example, we are going to do just that (clearing the window) and also draw shapes to the screen? Sounds fun! Let’s get started-

SFML Graphics Library | Quick Tutorial

So in this very quick tutorial, let’s learn about the basics of the mighty OpenGL-powered SFML Graphics Library.

Similar Reads

What is SFML?

Simply put SFML is a multimedia library for C++ with bindings available for other languages such as Python, Rust, etc. It does not just let you use hardware-accelerated 2D Graphics with OpenGL but also has a variety of methods related to different types of media such as fonts, audio, etc. It stands for Simple and Fast Multimedia Library. Well, how simple is it? So simple that you could get a snake game running in less than 15 minutes. How fast is it? It’s so fast that you’d be running the application at several thousand frames per second....

Where is SFML used?

SFML is used heavily in making games and even game engines. And even though SFML doesn’t support 3D rendering yet it is used for context creation. Since OpenGL can’t create a window, 3D graphics programmers use SFML (at least most of the time) for the task....

Setting up Environment

Before we program in SFML we must set up the environment! Hopefully, it’s not a much of a headache especially not if you are in Linux! Debian Linux users (and any other flavour of Linux with apt/apt-get) can install SFML with the following command:-...

Getting started with SFML

Here’s the most basic SFML application you’ll ever find:...

Rendering Simple Shapes with SFML

...

Advantages of SFML

...

Disadvantages of SFML

SFML is not all about window-creation (even though many people use it only for that). It can be used to render hardware-accelerated graphics (including some basic as well as complex shapes). In this example, we’ll constrain ourselves to draw a basic shape (a circle):-...

Further Reading

...

References

The advantages of SFML over other multimedia libraries are:-...

Contact Us