For Windows Setup see my other video You have SDL already setup then skip to video shows you. SDL (Simple Directmedia Layer) is a cross-platform library providing low level access to audio, input and graphics hardware. There are alternatives to SDL, however none of them officially support Mobile platforms. I’m going to use CLion on Windows.
SDL2 is the newest version of the Simple Directmedia Layer API. It can be usedtogether with CMake to build a cross platform multimedia application. In thisblog post I will describe the necessary steps to use SDL2 with CMake on bothLinux (Ubuntu 17.04) and Windows.
- You can count on CLion for the continuous analysis of your entire code base. It offers extended support for MISRA C and C, Global Data Flow Analysis to help you catch dangling pointers, unreachable code, and other potential issues inside translation units, and Clazy analyzer integration for Qt projects.
- For Windows you have to download the development package SDL2-devel-2.0.5-VC.zip and extract it to some location on your hard disk. You can create Visual Studio project files with the CMake GUI under windows but when you hit configure it will fail because it will not find the SDL2 Library.
You need to create a
CMakeLists.txt file for your project that includes
SDL2and compiles a simple program.
You can use the following code:
Setting up SDL2 with CMake under Ubuntu Linux is pretty easy. All you need to dois install the required dependencies first.
Now you can use
cmake to generate your Makefiles and build your project. Toinclude SDL2 headers you just use
#include 'SDL.h'. The correct include pathshave been set up by cmake. For Linux nothing else is required.
For Windows you have to download the development package
SDL2-devel-2.0.5-VC.zip and extract it to some location on your hard disk.
You can create Visual Studio project files with the CMake GUI under windows butwhen you hit configure it will fail because it will not find the SDL2 Library.
In the configuration window you will see a
SDL2_DIR variable. You will have topoint that to the location where you extracted the SDL2 development package.
Sdl Clion Windows
Before you can reconfigure you also have to create a file
sdl2-config.cmakewhere you extracted the development libraries and put the following content:
After that you now should be able to reconfigure successfully and generate theVisual Studio project files.
An alternative to the config file is a
FindSDL2.cmake module.CMake comes with one that works for SDL1.2, it can be adapted to work with SDL2.Here is the full content for the one that I am using:
In order to use it you have to place it somewhere in your project for instance into a
cmake subdirectory.Then you have to modify the CMake module path so that it can be found.
Sdl Clion Mac
You can create a test application to verify that everything works. You could forinstance use the following code for that which renders a black window.
If you also want to use SDL2_image check out my other post Using SDL2_image with CMake.
There is also a complete project in my github repository here.