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
- 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.
- 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.