Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SFML Blueprints

You're reading from   SFML Blueprints Sharpen your game development skills and improve your C++ and SFML knowledge with five exciting projects

Arrow left icon
Product type Paperback
Published in May 2015
Publisher Packt
ISBN-13 9781784398477
Length 298 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Installing SFML 2.2

There are two ways to get the SFML library. The easier way is to download the prebuilt version, which can be found at http://sfml-dev.org/download/sfml/2.2/, but ensure that the version you download is compatible with your compiler.

The second option is to compile the library yourself. This option is preferable to the previous one to avoid any trouble.

Building SFML yourself

Compiling SFML is not as difficult as we might think, and is within the reach of everyone. First of all, we will need to install some dependencies.

Installing dependencies

SFML depends on a few libraries. Before starting to compile it, make sure that you have all the dependencies installed along with their development files. Here is the list of dependencies:

  • pthread
  • opengl
  • xlib
  • xrandr
  • freetype
  • glew
  • jpeg
  • sndfile
  • openal

Linux

On Linux, we will need to install the development versions of each of these libraries. The exact names of the packages depend on each distribution, but here is the command line for Debian:

sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev libxrandr-dev libfreetype6-dev libglew-dev libjpeg-dev libsndfile1-dev libopenal-dev -y

Other operating systems

On Windows and Mac OS X, all the needed dependencies are provided directly with SFML, so you don't have to download or install anything. Compilation will work out of the box.

Compilation of SFML

As mentioned previously, the SFML compilation is really simple. We just need to use CMake, by following these steps:

  1. Download the source code at http://sfml-dev.org/download/sfml/2.2/ and extract it.
  2. Open CMake and specify the source code directory and the build directory. By convention, the build directory is called build and is at the root level of the source directory.
  3. Press the Configure button, and select Code::Blocks with the right option for your system.

    Under Linux, choose Unix Makefiles. It should look like this:

    Compilation of SFML

    Under Windows, choose MinGW Makefiles. It should look like this:

    Compilation of SFML
  4. And finally, press the Generate button. You'll have an output like this:
    Compilation of SFML

Now the Code::Blocks file is built, and can be found in your build directory. Open it with Code::Blocks and click on the Build button. All the binary files will be built and put in the build/lib directory. At this point, you have several files with an extension that depend on your system. They are as follows:

  • libsfml-system
  • libsfml-window
  • libsfml-graphics
  • libsfml-audio
  • libsfml-network

Each file corresponds to a different SFML module that will be needed to run our future games.

Now it's time to configure our system to be able to find them. All that we need to do is add the build/lib directory to our system path.

Linux

To compile in Linux, first open a terminal and run the following command:

cd /your/path/to/SFML-2.2/build

The following command will install the binary files under /usr/local/lib/ and the headers files in /usr/local/include/SFML/:

sudo make install

By default, /usr/local/ is in your system path, so no more manipulations are required.

Windows

On Windows, you will need to add to your system path, the /build/lib/ directory, as follows:

  1. Go to the Advanced tab in System Properties, and click on the Environment Variables button:
    Windows
  2. Then, select Path in the System variables table and click on the Edit... button:
    Windows
  3. Now edit the Variable value input text, add ;C:\your\path\to\SFML-2.2\build\lib, and then validate it by clicking on OK in all the open windows:
    Windows

At this point, your system is configured to find the SFML dll modules.

Code::Blocks and SFML

Now that your system is configured to find the SFML binary files, it's time for us to configure Code::Blocks and finally test whether everything is fine with your fresh installation. To do so, follow these steps:

  1. Run Code::Blocks, go to File | New | Project, and then choose Console Application.
  2. Click on GO.
  3. Choose C++ as the programming language, and follow the instructions until the project is created. A default main.cpp file is now created with a typical Hello world program. Try to build and run it to check whether your compiler is correctly detected.
    Code::Blocks and SFML

If everything works correctly, you will have a new window created that has a Hello world! message, as follows:

Code::Blocks and SFML

If you have this output, everything is fine. In any other case, make sure you have followed all the steps for the installations.

Now we will configure Code::Blocks to find the SFML library, and ask it to link with our program at the end of the compilation. To do this, perform the following steps:

  1. Go to Project | Build options and select your project at the root level (not debug or release).
  2. Go to Search directories. Here we have to add the path where the compiler and the linker can find the SFML.
  3. For the compiler, add your SFML folder.
  4. For the linker, add the build/lib folder, as follows:
    Code::Blocks and SFML

Now we need to ask the linker which libraries our project needs. All our future SFML projects will need the System, Window, and Graphics modules, so we will add them:

  1. Go to the Linker settings tab.
  2. Add -lsfml-system, -lsfml-window and -lsfml-graphics in the Other linker options column.
  3. Now click on OK.
    Code::Blocks and SFML

Good news, all the configurations are now finished. We will eventually need to add a library to the linker in the future (audio, network), but that's it.

A minimal example

It's now time for us to test the SFML with a very basic example. This application will show us the window as in the following screenshot:

A minimal example

The following code snippet brings about this window:

int main(int argc,char* argv[])
{
    sf::RenderWindow window(sf::VideoMode(400, 
400),"01_Introduction");
    window.setFramerateLimit(60);

    //create a circle
    sf::CircleShape circle(150);
    circle.setFillColor(sf::Color::Blue);
    circle.setPosition(10, 20);

    //game loop
    while (window.isOpen())
    {
       //manage the events
        sf::Event event;
        while(window.pollEvent(event))
        {
            if ((event.type == sf::Event::Closed)
                or (event.type == sf::Event::KeyPressed and 
event.key.code == sf::Keyboard::Escape))
                window.close(); //close the window
        }
        window.clear(); //clear the windows to black
        window.draw(circle); //draw the circle
        window.display(); //display the result on screen
    }
    return 0;
}

All that this application does is to create a window with a width and height of 400 pixels and its title is 01_Introduction. Then a blue circle with a radius of 150 pixels is created, and is drawn while the window is open. Finally, the user events are checked on each loop. Here we verify if the close event has been asked (close the button or click Alt + F4), or if the user has pressed the Esc button on his keyboard. In both case, we close the window, that will result to the program exit.

You have been reading a chapter from
SFML Blueprints
Published in: May 2015
Publisher: Packt
ISBN-13: 9781784398477
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime