Installing SDL2 on Raspbian Jessie

SDL2 on Raspberry Pi

Background

SDL2 is the newer version of SDL : Simple DirectMedia Layer. SDL is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL. You can find out more about SDL at www.libsdl.org. Be sure to check out the Wiki and the Forums as they are great informational resources.

The older version of SDL (1.2) has been used for many years on several platforms. Though it lacked many features it was simple to implement and became very popular. The newer SDL2 brings with it many improvements including support for hardware acceleration. This makes for a significant speed increase for the Raspberry Pi when it’s GPU is assists in the rendering of graphics.

Installing SDL2

Setting up an SDL2 development environment up and running on your Raspberry Pi has never been easier. Raspbian Jessie now makes it possible to install SDL2 (along with the extension libraries) without having to compile from source.

Install the SDL2 development libraries :

sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsdl2-mixer-dev

SDL2 by itself cannot handle image file formats other than BMP. The image library (libsdl2-image-dev) adds support for JPG and PNG images. True Type Font support is provided from the font library (libsdl2-ttf-dev). Finally, the sound library (libsdl2-mixer-dev) provides the means to play back sound effects and music.

A Simple test in C

The code below is a slightly modified version of Lazy Foo’ Productions Hello SDL: Your First Graphics Window which is a part of their Beginning Game Programming v2.0 tutorial. This recommended tutorial teaches how to use SDL2 with more than 50 lessons.

#include <SDL2/SDL.h>
#include <stdio.h>
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 270;

int main(int argc,char* args[]) {
	SDL_Window* window = NULL;
	SDL_Surface* screenSurface = NULL;
	if (SDL_Init(SDL_INIT_VIDEO)<0) {
		printf("Could not initialize SDL. SDL_Error: %s\n",SDL_GetError());
	} else {
		window = SDL_CreateWindow("SDL Test",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
		if (window==NULL) {
			printf("Cannot create window. SDL_Error: %s\n",SDL_GetError());
		} else {
			SDL_Rect arect;
			screenSurface=SDL_GetWindowSurface(window);
			SDL_FillRect(screenSurface,NULL,SDL_MapRGB(screenSurface->format,0x00,0x00,0x44));
			arect.x=64; arect.y=64; arect.w=SCREEN_WIDTH-128; arect.h=SCREEN_HEIGHT-128;
			SDL_FillRect(screenSurface,&arect,SDL_MapRGB(screenSurface->format,0x00,0x77,0x77));
			SDL_UpdateWindowSurface(window);
			SDL_Delay(3000);
		}
	}
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}

Compile the code and launch the executable. If all goes well you should see a small window containing two blue rectangles (for 3 seconds). That concludes this article on getting SDL2 up and running on your Raspberry Pi. Please send any questions or comments below.

gcc test.c -o test `sdl2-config --cflags --libs`
./test

SDL 2 in Rpi

Comments

  1. Thank you so much for this simple and easy article. It helped a lot specially if one need to use C (instead of C++) and SDL 2 on RPi.

    Best.

Speak Your Mind

*

Read previous post:
How to configure password-less SSH in Linux

Quickly and easily setup a passwordless and secure connection between two linux computers. This article will show you how to...

Close