Visit SFML's project page

Hello everybody and welcome! I decided that I will review a very, very useful cross-platform library that I have been using often lately called SFML, which stands for “Simple and Fast Media Library”. For those who don’t know, SFML is a game library (at version 1.6 at this time of writing), which is a collection of C++ functions that can aid you in making games, and is similar in spirit to another more mature game library called SDL (“Simple DirectMedia Layer”).

Let’s say that you wish to create a sidescroller like Mario, for example. Instead of researching deeply through the net and going into the nitty-gritty of setting up the window and coding unimportant (but complex) components, you could simply bundle SFML together with your project’s code. SFML can take care of playing the sound effects or video cut-scenes, displaying textures and loading game maps, interacting with OpenGL for 3D graphics, etc. while you focus on actually writing the game logic itself.

❗ NOTE: SFML is not a game engine! It is a bit more low-level than that, and it does not include any drag-and-drop editors or media creation tools. It’s purely source code, and you are expected to already know how to program in order to use it. However, it  is powerful enough that it could actually help you create a game engine of your own.

In this review, I will go over the features of the library and explain what it can do. I will also supply an example C++ snippet that demonstrates how to initialize SFML, along with step-by-step explanations of what the program does and how it works. I will finish by outlining SFML’s pros and cons, summarizing my overall impressions, and telling you whether it is worth being in your toolbox as a programmer.

Features

As mentioned in the introduction, SFML aims to be a replacement for another cross-platform game library called SDL (Simple DirectMedia Layer).

SDL is much older than SFML, and was written and released under the GNU LGPL in 1998 by Sam Lantinga, former lead software engineer at Blizzard Entertainment working on World of Warcraft. SDL powers many games and still works wonderfully up to this day, but it has its faults. It is sometimes known to be quite cryptic at points, and since it is written in C, it does not utilize the wonderful object-oriented paradigm that is so revered by programmers these days.

This is where SFML comes in. SFML was written in C++ by Laurent Gomila in 2007, licensed under the zlib license. Unlike SDL, SFML is a modular and fully object-oriented library. Its source code and SDK are very neat and orderly, making it a joy to work with. In terms of speed and performance, SFML either meets SDL, and sometimes exceeds it in some 2D and 3D tests, as verified here and here. The library has many components, but each “module”, so to speak, is split up into several simple headers which you must include into your main.cpp:

  1. Audio.hpp – Audio and sound effects subsystem
    • Contains all the functions that you need to play music or sound effects from a file or from a buffer.
  2. Graphics.hpp – 2D graphics rendering
    • Allows you to render 2D sprites, overlay text, display color, draw shapes, etc.
  3. Network.hpp – Server/client networking subsystem
    • Provides network LAN multiplayer functionality for your game.
  4. System.hpp – Miscellaneous low-level functionality
    • Lets you create threads, lock down game data so that other programs know they are in use, generate random numbers, etc.
  5. Window.hpp – Window management
    • This is the main SFML class. You can open a new window, set its options, and create an interface to OpenGL if you wish.

SFML, at version 1.6 at this time of writing, is fully cross-platform and works under multiple compilers. It runs on Windows (MSVC/MinGW-GCC), Mac (Xcode LLVM/GCC), and Linux or FreeBSD (GCC, LLVM/Clang, YACC, etc). One of the best things about SFML is that if you prefer programming in another language other than C++, you don’t have to worry. There are extra bindings to C, Microsoft .NET languages (C#/VB/etc), Python, D, and Ruby, which are also available for download at the SFML website here.

Writing an example program

To help any interested beginners out there understand how to use the library, I will walk you through with this example C++ source file, called main.cpp:

#include <SFML/Window.hpp>

int main(int argc, char *argv[])
{
    // Create the SFML window.
    sf::WindowSettings Settings;
    sf::Window App(sf::VideoMode(1024, 768, 32), "Test Game", sf::Style::Close, Settings);

    // Enter the game loop.
    while (App.IsOpened())
    {
        // Process mouse and keyboard input.
        sf::Event Event;

        while (App.GetEvent(Event))
        {
            // Clicking close button on window : exit
            if (Event.Type == sf::Event::Closed)
                    App.Close();

            // Hitting Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                    App.Close();
        }
        // Run your game logic stuff here...

        // Finally, display rendered frame on screen.
        App.Display();
    }

    return 0;
}

Assuming that the appropriate SFML development headers were downloaded from the SFML website and placed into a directory that your compiler can find, this program should compile fine on any platform. If all goes well, when the program is executed, you should see a command-line window (this is purely for displaying SFML’s notifications or error messages, should it have to) along with another empty, black window titled “Test Game” which should look something like this:

SFML Test Program
The result of the SFML test program

To explain how this program works under the hood, I will explain what the source code does, step by step:

  1. Starts the program.
  2. Creates a new “sf::Window“that is titled “Test Game”:
    • Window Name: App
    • Resolution: 1024 x 768
    • Color Depth: 32-bit
    • Window Style: Minimize (–) and Close (x) buttons enabled only.
  3. Runs the game loop:
    • Checks input events. If the user clicks “X” on the window or hits the ESC key, quit the loop.
    • Runs the game logic. Since we didn’t write that yet in this example, you will only see an empty, black window when the program is run.
    • The current frame is ready to render! The current frame is displayed in the window by executing  App.Display().
    • Repeats the previous steps forever as long as the SFML window is open.
  4. When the program exits the loop, the typical return 0;” is issued to quit the game.

Overall impressions

I had used SDL plenty of times before and had a long, loving relationship with it, but that’s not to say I never had any bumps in the road. I try to keep my code self-contained; that is, I like to make sure may code has no external third-party dependencies.

If I ever need an external library, I just bundle its headers and source files with my program, add custom search directories in my Code::Blocks project, and make sure that they retain their appropriate licenses. This makes compiling my code easier for others and myself; no extra packages to download, no configuration files to tweak, etc. Just download, open the project, hit “Build”, and it’s done.

In contrast to SFML, I had enormous difficulty making the SDL headers play nice on different machines or platforms, and that was my biggest (and only) gripe about it.

When I found out about SFML, I was only mildly interested, and I didn’t know what my expectations were. But I can tell you now that if I had any high expectations at all, SFML would have blown them all away.

It was exactly what I wanted! A cross-platform, cross-compiler game library, similar in spirit to SDL, with very high performance, clear and concise documentation, a modular design, slim size, easy code management, and best of all, it was easy to embed into my program and make its source code play nice on any platform I pleased! It was a matter of downloading SFML, copying it to my project’s internal “include” directory, pointing Code::Blocks to it, and voilà! Everything ran smoothly and initialization worked flawlessly on my computer right out of the box.

Also, I never really liked C++ very much and I have always preferred to use C, C#, and shell scripting as my primary language. But I have realized that SFML was the one library that successfully gave me the courage to tackle C++. Not only that, it actually made learning the language fun and interesting! No more boring “hello world” programs for me!

My only complaint is a minor one. Although SFML is easy to set up and use, it has the disadvantage of having less popularity and a smaller community than SDL. Because of this obscurity, it supports less platforms and has less tutorials and workshops than SDL. Once again, this is an extremely minor gripe, as SFML’s online documentation, wiki, and forum posts are absolutely stunning help resources, and should most certainly be enough to remedy any issues you may have.

SFML Pros:

  1. Easy to download, include, and initialize (easier than SDL, in my opinion), and runs fast.
  2. Takes full advantage of object-oriented programming paradigms (great for most modern languages).
  3. Supports OpenGL 3.x (SDL doesn’t).
  4. Similarities to SDL make it easier to migrate your project from SDL.
  5. SDK requires no external dependencies (yay!)
  6. Incredible online documentation and wiki.
  7. Rigidly enforces how your app is supposed to be written (“write a program in, rather than with, SFML”). Some may dislike this approach, but I’ve found SFML’s architecture and paradigms to be incredibly clean and powerful, so it’s all right IMO.

SFML Cons:

  1. Lack of popularity makes finding third-party resources and tutorials harder, and supports less platforms than SDL.
  2. Modularity and object-oriented design is typically unnecessary if you are programming in C.
  3. Image loading is rather basic in comparison to SDL_image.

Overall, I find SFML to be a wonderful game library! It runs great, has lots of features, is easier to integrate into your program than SDL in my opinion, and is a pleasure to use. I am honored to give it a score of:

« 96% »

Awesome!

Advertisements

One thought on “Library Review: SFML

  1. Nice post! In fact a comprehensive discussion of the library, in particular with comparison to SFML is sorely needed.

    The community may be small, but if we keep blogging and getting more people to use the library, then we can change that! 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s