Getting Started with SFML using Code::Blocks

SFML is a simple fast multimedia library that is a pleasure to use. The instructions below for getting started, written at the end of May 2015, are even more detailed than the official page. The instructions apply to CodeBlocks installed on Windows following the instructions on the preceding page and are for the use of dynamic linked libraries rather than for static libraries.

There are many versions of SFML to download. Our C:\Program Files (x86)\CodeBlocks\MinGW\lib folder contains libgcc_s_sjlj-1.dll rather than libgcc_s_dw2-1.dll so we need one of the SJLJ versions. The readme file C:\Program Files (x86)\CodeBlocks\MinGW\README-gcc-tdm.txt has this information:

=== TDM-GCC Compiler Suite for Windows ===
---        GCC 4.6 & 4.7 Series        ---

  1. Download SFML version GCC 4.7.1 TDM (SJLJ) - 32-bit from the download page.
  2. Unzip to a convenient folder.
    We unzipped to C:\Program Files (x86)\CodeBlocks\.
  3. In CodeBlocks, select menu option File > New > Project... and open an Empty project. Name it SFML_Trial.
  4. Select menu item Project > Build options... then select the Search directories tab and the Compiler tab within it.
  5. Press the Add button, select the include path such as C:\Program Files (x86)\CodeBlocks\SFML-2.3\include and choose not to keep it as relative path.
  6. Select the Linker tab instead of Compiler tab, press the Add button, select the lib path such as C:\Program Files (x86)\CodeBlocks\SFML-2.3\lib and choose not to keep it as relative path.
  7. Select menu item Project > Build options... then select the Linker settings tab.
  8. Click the Add button and enter sfml-graphics.
  9. Similarly add sfml-window and sfml-system.
    See the screenshot on the official page.
  10. Click the OK button.
  11. Select menu item File > New... > Empty file and save it as main.cpp (for both debug and release).
  12. Copy the test program from the official page and paste it into main.cpp.
  13. Select menu option Build > compile current file.
    You should find the object file main.o in a folder such as C:\Working\SFMLCodeBlocks\SFML_Trial\obj\Debug.
  14. Copy all the .dll files from SFML-2.3\bin to the folder that will contain the final executable, creating new folders as necessary.
    We copied the dynamic link libraries from C:\Program Files (x86)\CodeBlocks\SFML-2.3\bin to C:\Working\SFMLCodeBlocks\SFML_Trial\bin\Debug.
  15. Click the Build and run button.
    You should see an empty console window and a graphics window with a green circle.
  16. If you do not want the console window, select menu item Project > Properties..., select the Build targets tab and select GUI application instead of Console application in the combo box. Select menu option Build > Rebuild and accept the invitation to continue.
    Continue following the instructions to try the stunning demonstration of shaders.
  17. Copy Effect.hpp and the resources folder from the examples\shader folder to your project folder.
    We copied from C:\Program Files (x86)\CodeBlocks\SFML-2.3\examples\shader to C:\Working\SFMLCodeBlocks\SFML_Trial.
  18. Open with a text editor such as Notepad++ examples\shader\Shader.cpp and copy and paste all of the code to replace the existing code in main.cpp.
  19. Rebuild, execute and prepare to be impressed!
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++