Skip to content
Frank Bauernöppel edited this page Dec 7, 2018 · 30 revisions

SDL = Simple Directmedia Layer

https://www.libsdl.org says: "Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D."

SDL2 on RasPi has OpenGLES HW accelerated graphics support see: http://choccyhobnob.com/tutorials/sdl2-2-0-5-on-raspberry-pi/

For complex OpenGL accelerated GUI development, you might also consider Qt5.

get the source code:

root@raspberrypi3:~# wget https://www.libsdl.org/release/SDL2-2.0.9.tar.gz
root@raspberrypi3:~# tar xvf SDL2-2.0.9.tar.gz
root@raspberrypi3:~# cd SDL2-2.0.9

Now, configure, make and install libsdl2 as follows:

root@raspberrypi3:~# ./configure --enable-video-opengles2 --disable-video-mir --disable-video-wayland --disable-video-x11 --disable-video-opengl --host=arm-raspberry-linux-gnueabihf
root@raspberrypi3:~# make -j4
root@raspberrypi3:~# make install

Note: The --host option is crucial to detect a Raspi during ./configure!

Run test prog

#include <iostream>
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    int d = SDL_GetNumVideoDisplays();
    SDL_Rect r;
    SDL_GetDisplayBounds(0, &r);
    SDL_Window * window = SDL_CreateWindow( "fullscreen", r.x, r.y, r.w, r.h, SDL_WINDOW_FULLSCREEN );
    if (window == NULL) {
        std::cout << "SDL_CreateWindow: " << SDL_GetError() << std::endl;

    }
    SDL_Surface* screenSurface = SDL_GetWindowSurface( window );
    SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x76, 0xB9, 0x00 ) );
    SDL_UpdateWindowSurface( window );
    SDL_Delay( 10000 ); // milliseconds

    SDL_Quit();
}

compile and link with:

root@raspberrypi3:~# g++ -o SDL2_hello SDL2_hello.cpp `sdl2-config --cflags --libs`

Run the binary ./SDL2_hello. There attached HDMI should show a green screen for 10 seconds.

For more tutorials, see http://lazyfoo.net/tutorials/SDL/index.php

References

Clone this wiki locally