This is a short tutorial on how to get started with SDL 2 programming on a Raspberry Pi device that has Raspbian installed. If you want to write C++, or C, games that are easily portable to other platforms SDL 2 is the way to go on Raspbian. Unfortunately, at the time of this writing Raspbian comes with the outdated SDL 1.2 installed.
Let’s start by updating our Raspbian installation, feel free to skip this step if your system was recently updated:
Next, we will need to install some libraries that will be used when we build SDL 2:
SDL 2 can be downloaded from https://www.libsdl.org/download-2.0.php, be sure to select the source code archive, SDL2-2.0.3.tar.gz, or a newer version if available. You can download and extract the archive directly on your Raspberry Pi:
Now, it is time to configure SDL 2:
or, if you have a Raspberry Pi 2:
The above options will make sure, SDL 2 is built with the OpenGL ES backend and that any SDL application will run as a full screen application, the windowed mode under X tends to be more buggy on Raspberry Pi. On the plus side, you will be able to launch your application, or game, directly from the text interface without the overhead of running the X server. If you wish you can start your application from LXDE (the default window manager for Raspbian) and the application will run full screen.
Assuming, you’ve had no error in the configure phase, you can actually build and install the library:
Now, you should be able to compile any C++ code that uses SDL 2 with something like:
SDL 2 by itself can use only .bmp graphic files and .wav audio files. If you want to be able to load other image formats, like .png, you can use SDL_image 2.0 or something like FreeImage. The advantage of using SDL_image is that this is directly compatible with SDL 2, basically you will load an image file in a format that will just work with SDL 2. Similarly, there are libraries for loading sound files, SDL_mixer 2.0, or font files SDL_ttf 2.0.
Since working with images in various formats is a must for almost any SDL 2 application, I will exemplify, shortly, how to download, build and install SDL_image:
If your code uses SDL_image you will need to explicitly link the SDL_image library, e.g.:
The SDL_ttf library can be built similarly. If you want to build SDL_mixer you will need to build and install first smpeg2, after which you can build and install SDL_mixer like before.
Time to write a short code example that will open an SDL window and show an image for 10 seconds, after which will free the resources and exit. I assume that you’ve successfully installed SDL 2 and SDL_image on your Raspberry Pi.
Assuming that you’ve saved the above code as sdl2_test.cpp and saved this image in the same folder as the C++ source file, you can compile and run the code with:
The above instructions will use the default version of GCC, 4.6.x, from Raspbian that has only partial support for C++11. If you want to use a more modern C++ compiler, with full support for C++11 and partial support for C++14, read my previous article. In this case you should use something like:
This is what you should see:
If you want to learn more about SDL I would recommend reading SDL Game Development by Shaun Mitchel.