By: Jan Zumwalt - October 15, 2013
Introduction
I have a new project that needed good 2D graphics. Even though SDL1, EGL, GLES, GLES2, VG are preloaded with the "Raspian" version of RPI that I am using, I was interested in scaling and rotation and wanted to checkout SDL2.
A google search turns up almost nothing so I found myself blazing a trail. Neither the "synaptic" or "apt-get" repositories have the SDL2 library at the time of this writing. Oddly (at least for Raspbian) there does not seem to be any issue with the installation.
The only comprehnesive example I found was this utube http://www.youtube.com/watch?v=Yo7hO7GZ-ug video that includes links to a custom version that uses "CodeBlocks". I could not get it working on my Raspbian (Weezy Debian) version.
I was desperate and was not optimistic - but once you know one trick, it is REALLY EASY to get SDL2 running!
Get Loaded
1) Download - Get the linux generic release http://www.libsdl.org/download-2.0.php and unzip it in any directory. The really neat thing is the necessary Debian install files are provided and will setup automatically.
2) Install - Navigate to the Debian subdirectory. The instructions are in the text file "install.txt". All that is needed is to go to the unzip directory as root and run the following command './configure; make; make install'. It took about 50min to 1hr to do it's thing.
3) Done - It's ready... except ...

I spent the next two days not getting anything to compile. I eventually found out that SDL1 and SDL2 expect a custom made config program to pass the compiler library and include file information. This program is set up during the install process.
4) - Compile - Use these commands for SDL1 or SDL2 compiles
For sdl1 progs use...
Code: Select all
gcc `sdl-config --cflags --libs` -v -o myprog main.c > compile.log 2>&1
Code: Select all
gcc `sdl2-config --cflags --libs` -v -o myprog main.c > compile.log 2>&1
(Note: I tried for several days to manually provide the LIB and INC settings but I was unable to get anything to compile. You can run the "sdl2-config --cflags --libs" from a terminal and see what is being sent to the gcc compiler but when I tried to cut and past the same thing in a manual compile, it just would not work for me.)
Goodies!
I quickly found that SDL needs several support libraries to be able to use images and fonts.
The packages are located at http://www.libsdl.org/projects/
SDL2_image-2.0.0 - bmp is built in but you need this for jpg, png etc.
SDL2_mixer-2.0.0 - audio support
SDL2_net-2.0.0 - network support
SDL2_ttf-2.0.12 - trutype font support
SDL_rtf-0.1.0 - rich text format support
The install instructions for each package are the same so I have provided just one example.
Image Support, SDL2_image-2.0.0 - instructions are for 'root' user (you can also use sudo)
1) Download the package from http://www.libsdl.org/projects/
2) unzip
3) cd to the working directory
4) autogen.sh
5) ./configure && make && make install
When you are done, you will receive this message
I just include the directories with the -L and -I compiler commandsLibraries have been installed in: /usr/local/lib
If you ever happen to want to link against installed libraries in a given directory,
LIBDIR, you must either use libtool, and specify the full pathname of the library,
or use the `-LLIBDIR' flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
# file locations
LIB="-L./ -L./lib -L~/lib -L/lib -L/usr/lib -L/usr/local/lib -L/usr/lib/arm-linux-gnueabihf"
INC="-I./ -I./lib -I~/include -I/usr/include"
# graphic file locations
GLIB="-L/opt/vc/lib"
GINC="-I/opt/vc/include -Iopt/vc/src/hello_pi/libs"
# compile program
gcc `sdl2-config --cflags --libs` $LIB $GLIB $INC $GINC -v -o <program> <source> >> compile.log 2>&1
That's it, your done!
A discussion of various options in using these packages can be found at http://www.raspberrypi.org/phpBB3/viewt ... 92#p428692
Development Environment
For completeness I am providing my actual compiler script "compile.sh" and ".desktop" link so it can all be compiled from a text or xterm terminal.
This file is called "compile.sh" and can be run from a terminal or "desktop" link.
Code: Select all
#!/bin/bash
printf "\n"
printf "\t+----------------------------+\n"
printf "\t| Compile main.c |\n"
printf "\t| SDL2 test |\n"
printf "\t+----------------------------+\n\t"
printf "\n\t Start Compile: "; date
date > compile.log; printf "\n" >> compile.log
# file locations
LIB="-L./ -L./lib -L~/lib -L/lib -L/usr/lib -L/usr/local/lib -L/usr/lib/arm-linux-gnueabihf"
INC="-I./ -I./lib -I~/include -I/usr/include"
# graphic file locations
GLIB="-L/opt/vc/lib"
GINC="-I/opt/vc/include -Iopt/vc/src/hello_pi/libs"
# compile program
gcc `sdl2-config --cflags --libs` $LIB $GLIB $INC $GINC -v -o sdl2_test main.c >> compile.log 2>&1
# run program
if [ -f sdl2_test ]; then
./sdl2_test
printf "\tEnd of program... \n"
else
cat ./compile.log
printf "\t *** There where errors, compile aborted. ***\n"
fi
# required so xterm will not close
printf "\n\t%s" "press any key to exit: "
read -n 1
This is the "compile.desktop" GUI link file to be able to click on and compile from a xterm.
Code: Select all
[Desktop Entry]
Name=Compile
Comment=Compiles C program
Type=Application
Encoding=UTF-8
Terminal=false
StartupNotify=true
Icon=/usr/share/icons/gear_lnk.png
Exec=xterm -sb -rightbar -fg Beige -bg "rgb:00/10/20" -e "cd /root/<path to program> && ./compile.sh"
Here is the "main.c" SDL2 test program...
Code: Select all
#include <SDL2/SDL.h>
// +--------------------------------------------------+
// | Minimal SDL2 Test Program |
// | Creates a graphics window then shuts it down |
// | after 10 seconds. |
// +--------------------------------------------------+
int main ( int argc, char** argv )
{
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"SDL2 TEST PROGRAM", // window title
SDL_WINDOWPOS_CENTERED, // the x position of the window
SDL_WINDOWPOS_CENTERED, // the y position of the window
400,400, // window width and height
SDL_WINDOW_RESIZABLE // create resizeable window
);
if(window == NULL) // if no win, show error
{
printf("Could not create SDL2 test window: %s\n", SDL_GetError());
return 1;
}
SDL_Delay(10000); // 10sec delay
SDL_DestroyWindow(window); // kill window
SDL_Quit(); // clean up
return 0; // return success
}
Lets all get together and start sharing our SDL2 programs!!!
Test Programs
Once you install SDL2 a comprehensive set of test routines are provided with the core package - but no compile info is provided. It was at this point that I ran into my problems until I ran across the need to pass LIB & INC info using the "sdl-config" program.
I created a script that tries to compile all of them. Some will compile while others don't.
I have not looked at the problems but I expect to find they need the "image" and "font" packages.
compile_all.sh
Code: Select all
#!/bin/bash
# +--------------------------------------+
# | Compile all SDL2 Test Programs |
# +--------------------------------------+
while read F ; do
printf "\n"
printf "\t+----- Compileing $F.c -----+\n"
printf "\n\t Start time: "
date
printf "\n"
gcc `sdl2-config --cflags --libs` -o $F $F.c
printf "\n\t End time: "
date
printf "\n"
done <./filelist.txt
Code: Select all
checkkeys
loopwave
testatomic
testaudioinfo
testdrawchessboard
testerror
testfile
testgamecontroller
testgesture
testgl2
testgles
testhaptic
testiconv
testjoystick
testkeys
testloadso
testlock
testmessage
testmultiaudio
testoverlay2
testplatform
testpower
testresample
testrumble
testsem
testshader
testshape
testspriteminimal
teststreaming
testthread
testtimer
testver
torturethread
These programs don't compile.
They also did not compile after loading Image and Font support.
testautomation_audio
testautomation
testautomation_clipboard
testautomation_events
testautomation_keyboard
testautomation_main
testautomation_mouse
testautomation_pixels
testautomation_platform
testautomation_rect
testautomation_render
testautomation_rwops
testautomation_sdltest
testautomation_stdlib
testautomation_surface
testautomation_syswm
testautomation_timer
testautomation_video
testdraw2
testime
testintersections
testnative
testnativew32
testnativex11
testrelative
testrendercopyex
testrendertarget
testscale
testsprite2
testwm2
To compile a single program use this script
compile1.sh
Code: Select all
#!/bin/bash
# check for 2 arguments
ARGS=2 # number of arguments
if [ $# -ne $ARGS ]; then
printf "\t +---------------------------------------+\n"
printf "\t | Compile SDL2 Program |\n"
printf "\t | |\n"
printf "\t | compile1.sh ver Oct 1, 2013 |\n"
printf "\t | |\n"
printf "\t | Usage: |\n"
printf "\t | compile1.sh <program> <source> |\n"
printf "\t | |\n"
printf "\t | Example: |\n"
printf "\t | compile1.sh myprog main.c |\n"
printf "\t +---------------------------------------+\n"
exit 1 # general error
fi
printf "\n"
printf "\t+----- Compileing $2 -----+\n"
printf "\n\t Start time: "; date
# file locations
LIB="-L./ -L./lib -L~/lib -L/lib -L/usr/lib -L/usr/local/lib -L/usr/lib/arm-linux-gnueabihf"
INC="-I./ -I./lib -I~/include -I/usr/include"
# graphic file locations
GLIB="-L/opt/vc/lib"
GINC="-I/opt/vc/include -Iopt/vc/src/hello_pi/libs"
# compile program
gcc `sdl2-config --cflags --libs` $LIB $GLIB $INC $GINC -v -o $1 $2 >> compile.log 2>&1
printf "\n\t End time: "; date
printf "\n"
exit 0
According to the test programs there are some problems with programs trying to use OpenGL, OpenGles, and Heptic that is pre-loaded on the Raspbian.
So, it would be nice to integrate these other common graphics packages along with the other major SDL2 packages - fonts, images, rich text, mixer, etc at http://www.libsdl.org/projects/