Return to Robotics Tutorials

Installing SDL on Raspberry Pi for GUIslice

This page details the steps involved in installing the SDL 1.2 library for use with the GUIslice library.

Example SDL code in C
Output from test-sdl.c C code

Installing SDL library

Once the Raspberry Pi has been configured for the PiTFT, we can begin to test out the SDL1.2 graphics library.

The following steps show you how to install SDL1.2. If you don't need support for bitmaps or fonts, then only the first line is required. As a matter of practice, it is usually best to issue sudo apt-get update before any install commands (to ensure you are getting the latest update).

sudo apt-get install libsdl1.2-dev
sudo apt-get install libsdl-image1.2-dev
sudo apt-get install libsdl-ttf2.0-dev

Testing the SDL library with C code

The following code can be used to ensure that the SDL library has been installed correctly on the Raspberry Pi. Assuming that you have network access from the RPi, you can download the source code here (link soon).

#include "SDL/SDL.h"
#include "SDL/SDL_getenv.h"

// GUIslice: simple SDL test in C (test-sdl.c)
// - Calvin Hass
// -
// This program is a simple test to ensure that the SDL
// library is installed correctly. If everything is
// working then you should see the external screen
// (such as PiTFT) display a red-to-blue gradient and
// a green region.

// Define the primary surface for display
SDL_Surface* scrMain = NULL;

// Main entrypoint
int main(int argc, char* args[])
        // --------------------------------------
        // Initialization
        // --------------------------------------

        // Update the environment variables for SDL to
        // work correctly with the external display on
        // LINUX frame buffer 1 (fb1).

        // Initialize SDL
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
                fprintf(stderr,"ERROR in SDL_Init(): %s\n",SDL_GetError());
                return 0;

        // Fetch the best video mode
        // - Note that the Raspberry Pi generally defaults
        //   to a 16bits/pixel framebuffer
        const SDL_VideoInfo* vInfo = SDL_GetVideoInfo();
        if (!vInfo) {
                fprintf(stderr,"ERROR in SDL_GetVideoInfo(): %s\n",SDL_GetError());
                return 0;
        int     nResX = vInfo->current_w;
        int     nResY = vInfo->current_h;
        int     nDepth = vInfo->vfmt->BitsPerPixel;

        // Configure the video mode
        // - SDL_SWSURFACE appears to be most robust mode
        int     nFlags = SDL_SWSURFACE;
        scrMain = SDL_SetVideoMode(nResX,nResY,nDepth,nFlags);
        if (scrMain == 0) {
                fprintf(stderr,"ERROR in SDL_SetVideoMode(): %s\n",SDL_GetError());
                return 0;

        // --------------------------------------
        // Perform some simple drawing primitives
        // --------------------------------------

        // Draw a gradient from red to blue
        SDL_Rect        rectTmp;
        Uint32          nColTmp;
        for (Uint16 nPosX=0;nPosX<nResX;nPosX++) {
                rectTmp.x = nPosX;
                rectTmp.y = nResY/2;
                rectTmp.w = 1;
                rectTmp.h = nResY/2;
                nColTmp = SDL_MapRGB(scrMain->format,nPosX%256,0,255-(nPosX%256));

        // Draw a green box
        Uint32 nColGreen = SDL_MapRGB(scrMain->format,0,255,0);
        SDL_Rect rectBox = {0,0,nResX,nResY/2};

        // Now that we've completed drawing, update the main display

        // Wait for a short delay

        // Close down SDL

        return 0;
Simple SDL test in C (test-sdl.c)

Compiling & Running SDL test C code

Enter the following command to compile the above code:

gcc `sdl-config --cflags` `sdl-config --libs` -std=c99 -o test-sdl test-sdl.c

Run the built program with:

sudo ./test-sdl

When the code is run, you should see the top half of the screen filled with green and the bottom half display a gradient from blue to red (as shown at the top of this page).

In the GUIslice examples, the droid font has been used, so it may be useful to add: sudo apt-get install fonts-droid.

microSDL SDL GUI library in C
GUI written with SDL in C

More advanced examples with SDL

Take a look at the GUIslice library for Raspberry Pi to see more advanced examples!


Reader's Comments:

Please leave your comments or suggestions below!


Leave a comment or suggestion for this page:

(Never Shown - Optional)