Building SDL2-2.0.4 on the Raspberry Pi to enable OpenGLES 2 in Hardware

Raspbian Wheezy didn’t come with SDL2 installed, Raspbian Jessie comes with SDL2-2.0.2, a mere apt-get away.

New in SDL2-2.0.5 is a RPI video target that uses a hardware accelerated OpenGLES2 renderer for the frame buffer to speed up screen drawing. This is obviously something we want, especially if we are playing about with games or emulation. This isn’t packaged for us anywhere to install yet (it will be in Raspbian Stretch when it releases, but it’s not there yet)

First things first, if you have already installed SDL2-2.0.2 by typing sudo apt-get install libsdl2-dev at some point in the past you will need to get rid of it as it occupies the default location and will be used in preference to our new build. So let’s start by doing that.

sudo apt-get remove -y --force-yes libsdl2-dev
sudo apt-get autoremove -y

Next we need to set up the build environment; so add a few needed packages

sudo apt-get install libfontconfig-dev qt5-default automake mercurial libtool libfreeimage-dev libopenal-dev libpango1.0-dev libsndfile-dev libudev-dev libtiff5-dev libwebp-dev libasound2-dev libaudio-dev libxrandr-dev libxcursor-dev libxi-dev libxinerama-dev libxss-dev libesd0-dev freeglut3-dev libmodplug-dev libsmpeg-dev

I’m going to assume you use some form of  ‘code’ or ‘develop’ type folder structure for your programming efforts (I use ‘code’, just substitute your development directory structure for mine)

cd
mkdir code
cd code

Now we have a choice to make. There is a ‘stable’ and a ‘development’ version of SDL2-2.0.4 available. The stable one is (obviously) stable, and was released in Jan 2016, the development one is less stable and may have bugs in it but does include all the latest bug fixes for the bugs that shipped in the stable version. ¯\_(ツ)_/¯

If you want to use the stable version you can grab it from the SDL2 site and decompress it to your pi by typing:-

wget http://www.libsdl.org/release/SDL2-2.0.5.tar.gz
tar zxvf SDL2-2.0.5.tar.gz
cd SDL2-2.0.5

If you would rather use the latest development build, you can get that from the SDL teams mercurial by typing:-

hg clone http://hg.libsdl.org/SDL
cd SDL

The choice is yours!

Whichever one you pick, you will end up with a copy of the SDL2 source code and leave yourself in the source folder, so now we need to actually build it.

./autogen.sh
./configure --disable-pulseaudio --disable-esd --disable-video-mir --disable-video-wayland --disable-video-opengl --host=arm-raspberry-linux-gnueabihf
make
sudo make install

That will take a little while but when it’s done if you type sdl2-config --version it should say 2.0.5.

Great success!

Next up we will build all the utility libraries, starting by downloading them all.

cd
cd code
wget http://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.1.tar.gz
wget http://www.libsdl.org/projects/smpeg/release/smpeg2-2.0.0.tar.gz
wget http://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.1.tar.gz
wget http://www.libsdl.org/projects/SDL_net/release/SDL2_net-2.0.1.tar.gz
wget http://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.tar.gz

Uncompress them all

tar zxvf SDL2_image-2.0.1.tar.gz 
tar zxvf smpeg2-2.0.0.tar.gz
tar zxvf SDL2_mixer-2.0.1.tar.gz 
tar zxvf SDL2_net-2.0.1.tar.gz 
tar zxvf SDL2_ttf-2.0.14.tar.gz

Build the Image file loading library

cd SDL2_image-2.0.1 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the MPEG decoder (you need this for the mixer)

cd smpeg2-2.0.0 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the Audio mixer library

cd SDL2_mixer-2.0.1 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the Networking library

cd SDL2_net-2.0.1 
./autogen.sh 
./configure 
make 
sudo make install
cd ..

Build the Truetype font library

cd SDL2_ttf-2.0.14
./autogen.sh
./configure
make
sudo make install
cd ..

All done. Any program that uses SDL2 to draw to the framebuffer should now be doing it in hardware on the raspberry pis videocore4 using opengles2!