How2 Install & Use SDL2 on Raspbian PI


 
13 posts
by jwzumwalt » Sat Sep 28, 2013 5:51 am
How To Install & Use SDL2 on Raspbian PI
By: Jan Zumwalt - October 1, 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

For sdl2 programs use...
Code: Select all
gcc `sdl2-config --cflags --libs` -v -o myprog main.c > compile.log 2>&1

NOW... you are up and running.

(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
Libraries 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.



I just include the directories with the -L and -I compiler commands

# 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/viewtopic.php?f=67&t=12693&p=428692#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"


Try It!
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
}


So there you have it!
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


filelist.txt - These test programs compile OK
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


Going Further
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/
Last edited by jwzumwalt on Sun Oct 06, 2013 8:35 am, edited 34 times in total.
User avatar
Posts: 44
Joined: Sun Aug 04, 2013 4:00 pm
by bloodline » Sat Sep 28, 2013 7:38 am
Does this use OpenGLES directly or X? Does the event handling work?
Posts: 74
Joined: Sun Jun 10, 2012 8:44 pm
Location: London - England
by jwzumwalt » Sat Sep 28, 2013 3:46 pm
SDL1 or SDL2 are "2D" packages while OpenGL, OpenGles, and Heptic are separate 3D packages. All these packages are supported by SDL1 and SDL2. I believe "events" is part of the SDL core but I am not sure.
User avatar
Posts: 44
Joined: Sun Aug 04, 2013 4:00 pm
by gabomdq » Tue Oct 01, 2013 5:42 pm
I've recently added proper support for the Raspberry Pi in SDL2 that works without X (including OpenGL ES 2.x, input, mouse pointer, etc) . You have to fetch the code from Mercurial (the 2.0 release does not support this), or get the binaries I made here
Posts: 9
Joined: Mon Jul 30, 2012 8:36 pm
by Vanfanel » Thu Oct 03, 2013 10:44 pm
gabomdq wrote:I've recently added proper support for the Raspberry Pi in SDL2 that works without X (including OpenGL ES 2.x, input, mouse pointer, etc) . You have to fetch the code from Mercurial (the 2.0 release does not support this), or get the binaries I made here


Wow, this is the greatest thing I've read here since RetroArch was ported to the Pi.
You, gabomdq, are my hero :)
Are you maintainig the Rpi stuff on SDL2.0?
Posts: 385
Joined: Sat Aug 18, 2012 5:58 pm
by ackbar345 » Fri Oct 04, 2013 5:23 pm
gabomdq I downloaded your binaries and unzipped them in root but I am having problems getting my program to compile. Do I need to install the generic SDL2 before I apply your binaries? Anything special I need to do after I unzip it? Thanks for your work!
Posts: 4
Joined: Sat Jan 05, 2013 7:17 pm
by ackbar345 » Fri Oct 04, 2013 7:21 pm
OK so I compiled the SDL2 library from source and then reapplied the patch. Then I added the `sdl2-config --cflags --libs` line to my Makefile. That seems to have gotten things going. Still trying to mess around with getting it working without X running.
Posts: 4
Joined: Sat Jan 05, 2013 7:17 pm
by Vanfanel » Sat Oct 05, 2013 4:11 pm
gabomdq: after cloning, building and installing from mercurial as you instructed, I compiled the examples on the test directory, but they don't work at all:

Code: Select all
pi@raspberrypi ~/src/SDL/test $ ./testdraw2
Window requested size 640x480, got 1360x768
Couldn't create renderer: Couldn't find matching render driver

pi@raspberrypi ~/src/SDL/test $ ./testoverlay2
ERROR: Couldn't set create renderer: Couldn't find matching render driver

pi@raspberrypi ~/src/SDL/test $ ./testgles
ERROR: No OpenGL ES support on this system

pi@raspberrypi ~/src/SDL/test $ ./testsprite2
Couldn't create renderer: Couldn't find matching render driver



EDIT: I got it to work with your binaries, but not when building from Mercurial sources. Some building instructions for the Pi are needed, please.
Posts: 385
Joined: Sat Aug 18, 2012 5:58 pm
by gabomdq » Mon Oct 14, 2013 1:36 am
Just a quick update, we've added instructions on how to cross compile from Desktop Linux, most issues should be ironed out. As always, feedback is most welcome!

(Also sorry, I forgot to subscribe to the post so I wasn't getting notifications for the replies)
Posts: 9
Joined: Mon Jul 30, 2012 8:36 pm
by PiGraham » Fri May 02, 2014 2:27 pm
Post content moved to Graphics programming forum
Last edited by PiGraham on Fri May 02, 2014 8:04 pm, edited 1 time in total.
Posts: 2414
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville
by jwzumwalt » Fri May 02, 2014 2:36 pm
Sorry, but I am not a PI expert. I just use it as a tool.
If someone else with more expertise does not answer your question,
I suggest you post to a PI hardware/software forum.
User avatar
Posts: 44
Joined: Sun Aug 04, 2013 4:00 pm
by PiGraham » Fri May 02, 2014 8:08 pm
jwzumwalt wrote:Sorry, but I am not a PI expert. I just use it as a tool.
If someone else with more expertise does not answer your question,
I suggest you post to a PI hardware/software forum.


OK, thanks.

If anyone has SDL2 running and could try a quick test I'd like to know if code that calls SDL_init() runs OK if run from gdb.

If your compiled program is myprog then:

Code: Select all
gdb myprog

gdb> run


Does it work or hang?

I have moved the post to the similar topic in graphics programming.
Posts: 2414
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville
by ShiftPlusOne » Sat May 03, 2014 10:09 am
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4230
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy