Resources and installation
You can download the latest stable pre-built version of the library at: http://www.sfml-dev.org/download.php. It is also possible for you to get the latest Git revision and compile it yourself from here: https://github.com/LaurentGomila/SFML. The former option is easier and recommended for beginners. You have to wait for major versions to be released, however they're more stable. To build SFML yourself, you will need to use CMake, which is a tool used to generate solutions or g++ Makefiles, depending on the software that will be used to compile it. The official SFML website provides tutorials on building it yourself at: http://www.sfml-dev.org/tutorials.
After either obtaining the pre-built version of SFML or compiling it yourself, it's a good idea to move it somewhere more permanent, hopefully with a short path. It's not unusual to dedicate a directory somewhere on your local drive that will hold SFML and potentially other libraries, which can be linked to quickly and at all times. This becomes useful when dealing with several versions of the same library as well. For the rest of this book, we will assume the location of our SFML library and header directories to be at C:\libs\SFML-2.3
, consequently being C:\libs\SFML-2.3\lib
and C:\libs\SFML-2.3\include
. These directories have to be set up correctly in your compiler of choice for the project to build. We will be using Microsoft Visual Studio 2013 throughout the course of this book, however instructions on setting up projects for Code::Blocks can be found in the tutorials section of the SFML website.
Setting up a Microsoft Visual Studio project
Create a new solution in your IDE. It can be a Win32 application or a console application, which is not really relevant, although a nice console window is often useful for debug purposes. I always go with the Empty Project option to avoid any auto-generated code. After that's done, let's prepare our project to use SFML:
- Navigate to the VC++ Directories underneath Configuration Properties by right clicking on our project and selecting Properties.
- Only two fields are of any concern to us, the Include Directories and Library Directories. Make sure the paths to the SFML library and include directories are provided for both Debug and Release configurations.
- When linking SFML statically, the Preprocessor section underneath C/C++ is where you need to define the
SFML_STATIC
macro. - Next is the Additional Library Directories in General underneath Linker. Make sure that it also points to the SFML library directory in both debug and release configurations.
- Lastly, we need to set up the project dependencies by editing the Additional Dependencies field in the Input section underneath Linker. It would look something like this for the debug configuration when using statically linked libraries:
sfml-graphics-s-d.lib; sfml-window-s-d.lib; sfml-system-s-d.lib; opengl32.lib; freetype.lib; jpeg.lib; winmm.lib; gdi32.lib;
Remember that we need to include the system library because of library dependencies. Also note the use of
-s
and-d
postfixes. Make sure both debug and release configurations are set up and that the release configuration omits the-d
postfix.