Setting Up SFML

Welcome to the first in a series of tutorials on how to make a simple zombie massacre game using C++ and SFML. The goal of this tutorial series is to introduce you to Simple Fast Multimedia Library (SFML), which is a cross platform library for C++ for making games and other multimedia rich applications with ease. In these tutorials we will run in to many problems common to many if not all games, and suggestions on how to solve them. At the end of this series you will have learned how to make a complete game with monsters, scoring, sound effects and even a main menu.

I assume you have already downloaded and installed Code::Blocks including the MinGW compiler. If not, do so now. If you are running Linux, you should install G++ and the latest libstd dev package. But if you are using linux i assume you are also able to do this.

Our first task is to get SFML up and running with Code::Blocks. Because SFML is compiled, like, a million platforms, this step is quite easy. First, download it from the SFML download page. Choose the C++ version that fits your platform.

After downloading the files, you need to put the files where your MinGW compiler will find them. If you installed Code::Blocks with the MinGW compiler, the compiler's directory is most likely within your Code::Blocks directory. On my computer it is

  • C:\Program Files\CodeBlocks\MinGW

or if you downloaded MinGW separately it is usually installed at (although I have never tried this)

  • C:\MinGW

In the MinGW directory you will find an include folder. Copy the content of SFML's include folder to MinGW's include folder. This is the folder called SFML.

The compiler is now able to find the files. But we need to copy the other files as well so that the linker is able to link the files together. Don't worry about all this right now. All you need to do is copy all the .a files from lib\mingw to the compiler's lib directory.

That's it. SFML is now installed and your SFML programs should compile. Let's see if it works.

  • Create a new Code::Blocks project, choose empty project, give it a name, let's call it Zombie Bonanza (I had to call it something), select a debug and release build targets, and then click finish.
  • Add an empty file to the project via File -> New -> Empty File. You will be asked if you want to add it to the project, select yes and call it main.cpp and click save. You can then select the target builds where to add the file. Select both debug and release targets and click ok.

Your file should now appear in the tree structure for your project, as well as be opened ready for our test program.

A simple example is using a Clock object to write out a few lines of text.

#include <SFML/System.hpp>
#include <iostream>
using namespace sf;
using namespace std;

int main()
{
    Clock clock;
    while (clock.GetElapsedTime() < 5.0f)
    {
        cout << clock.GetElapsedTime() << endl;
        Sleep(1.0f);
    }
    cout << "Hello world" << endl; // Keeping with tradition (although this does not use SFML)
    return 0;
}

Write the code into main.cpp. And i strongly recommend you write it out and do not copy paste because you need the experience. When done, hit ctrl-F9 to build. You should run into your first set of build errors. No worries, looking at the error we see a lot of undefined reference to 'sf::Clock::Clock()' This is a very common error so you should make a note of it. It means, the linker cannot find the implementation of Clock::Clock(). The implementation is found in all the .a files we copied earlier, however we need to tell the linker which libraries we want to use.

Go to Project -> Build Options… and select Release. Then select Linker settings tab and add sfml-system. Then, add select Debug and add sfml-system-d. This is the debug version of the library which might come in handy later when you have to debug your game.

Hit ctrl-F9 again and it should compile without errors. Hit F9 to run and you run into another very common problem.

  • This application has failed to start because sfml-system.dll was not found. Re-installing the application may fix this problem

Basically Windows cannot find the SFML .dll files. To solve this you can either copy all the .dll files from the SFML lib\mingw folder to C:\windows\system32 folder, or, if you don't want to clutter too much1 copy them the the root folder of your newly created project. Hit F9 again, and you should see your first SFML program in action.

Congratulations, you have completed your first steps and getting closer to your dream game. Please leave comments on what is missing, badly written, unexplained etc. or better yet, help improve this page by updating it yourself. Also feel free to make some screenshots of the steps explained here so people can better see the steps.

(todo: Install extlibs to the system as well)

In the next installment we will be looking at opening a game window for your graphics mayhem.

page_revision: 5, last_edited: 1258552422|%e %b %Y, %H:%M %Z (%O ago)