Introducing SFML
Before we start developing a game, we would like to tell you a little bit about the library we will use throughout the book. SFML is an object-oriented C++ framework. As can be guessed by the name, its philosophy consists of having a simple, user-friendly application programming interface (API), and allowing for both high performances and fast development.
SFML is a multimedia library, meaning that it provides a layer between you and the hardware. It is split into five modules:
System: This is a core module upon which all other modules are built. It provides two-dimensional and three-dimensional vector classes, clocks, threads, and Unicode strings, among other things.
Window: The Window module makes it possible to create application windows, and to collect user input, such as mouse movement or key presses.
Graphics: This module provides all functionalities that are related to two-dimensional rendering, such as images, texts, shapes, and colors.
Audio: SFML also offers a module to work with sound. When you want to load a music theme and play it on the computer's loudspeakers, this is the module you have to look for.
Network: Another medium SFML covers is the network, a more and more important part of our interconnected world. This module allows you to send data over LAN or the Internet; it also lets you work with protocols, such as HTTP or FTP.
If you don't need all the modules, it is possible to use only a part of SFML. We will cover every module in SFML, but of course we are not able to use every single class. We recommend having a look at the SFML documentation, which is available at www.sfml-dev.org/documentation.php. The documentation explains every class and function in a detailed manner, and is an invaluable tool when developing a game using SFML.
SFML is open source, which means that you have access to its complete source code. Usually, the implementations aren't relevant to the user, but if you are interested in how something was solved, don't hesitate to skim through SFML's code.
The library uses the zlib/libpng license, which is extremely permissive. You can use SFML in both open and closed source projects, both free and commercial.
Downloading and installation
There are two possibilities when using SFML: download the pre-built libraries, or recompile them yourself. The first option is simpler, but you have to wait for major versions (2.0, 2.1, and so on) to be released. If you want to use the latest development sources, you can download the current Git revision. The configuration software CMake is used to prepare the sources for compilation with a compiler of your choice. For example, CMake creates Visual Studio solutions or g++ Makefiles. The recompilation process is explained in detail in the SFML tutorials, which can be found at www.sfml-dev.org/tutorials.php.
As mentioned, SFML is split into five modules. There are five headers to include a complete module (and its dependencies). To include the whole Audio module, you can write:
#include <SFML/Audio.hpp>
On the other hand, if you need a specific header file, you can find it in the directory of the corresponding module:
#include <SFML/Audio/Sound.hpp>
Each module is compiled to a separate library, which makes it possible to use only the modules you need. SFML can be built for release or debug mode, and it can be linked statically or dynamically. The resulting libraries are named according to the scheme sfml-module[-s][-d]
. The
-s
postfix is required if you link statically; the -d
postfix specifies debug mode. For example, to link the Graphics module statically in release mode, you have to specify the library sfml-graphics-s
in your linker options. Depending on your compiler, a file extension (such as .lib
) might be necessary. Keep in mind that some modules depend on others; therefore, you have to link the dependencies too. For example, Graphics depends on Window, which depends on System; therefore, you should link the three (in this order).
An important point to note is that if you link SFML statically, you have to define the macro SFML_STATIC
in your projects, so that the linker knows what functions to resolve.
In case you do not know how linking a library works for a specific compiler, please refer to the online tutorials. They explain how to install everything correctly, and are always up-to-date.
A minimal example
Before you go deeper into the book and SFML itself, let's take a look at a minimal application example to show how an application that uses this library looks like, its general flow of execution, and some basic functionality.
#include <SFML/Graphics.hpp> int main() { sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Application"); sf::CircleShape shape; shape.setRadius(40.f); shape.setPosition(100.f, 100.f); shape.setFillColor(sf::Color::Cyan); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); window.draw(shape); window.display(); } }
All this application does is to open a window onto which we can render, with a width of 640 pixels and a height of 480 pixels. Its title says "SFML Application"
. Then, a cyan geometric circle is created, and while the window is open, it is drawn to the screen. Finally, for each time the circle is drawn, the program checks for user input that may have arrived from the underlying window. In our case, we only handle the
sf::Event::Closed
event, which arrives every time the application is requested to terminate, such as when we click on the close button, or press an application-terminating shortcut, such as Alt + F4.
If you failed to understand a part of or the whole snippet of code, don't fear. This book contains all you need to know about this and much more.