You are on page 1of 7

Raspberry Pi - Building SDL 2 on Raspbian | Solarian Programmer

https://solarianprogrammer.com/2015/01/22/raspberry-pi-raspbian-getting-started-sdl-2/

23-04-2017 19.51.07
Solarian Programmer
My programming ramblings
Home Archives Contact Privacy

VMware vCloud Air Network - A Unified Hybrid Cloud


Find VMware vCloud Air Network Partners & Get Started Today! Go to
vcloudproviders.vmware.com/vCAN/vCAN

Raspberry Pi - Building SDL 2 on Raspbian


Posted on January 22, 2015 by Sol

This is a short tutorial on how to get started with SDL 2 programming on a


Raspberry Pi device that has Raspbian installed. If you want to write C++, or
C, games that are easily portable to other platforms SDL 2 is the way to go
on Raspbian. Unfortunately, at the time of this writing Raspbian comes with
the outdated SDL 1.2 installed.
Categories
Lets start by updating our Raspbian installation, feel free to skip this step if
Arduino Armadillo Books C C++
your system was recently updated:
C++11 C++14 C++17 C11 C99
1 sudo apt-get update Charts Clang Cling C# CUDA
2 sudo apt-get upgrade Fortran Fractals GCC iOS
JavaScript Jekyll Linux macOS
Next, we will need to install some libraries that will be used when we build Matplotlib Multithreading Node.js
SDL 2: NumPy Objective-C OpenCV
OpenGL OpenMP OSX Perlin
Posix
1 sudo apt-get install build-essential libfreeimage-dev libopenal-dev libpango1.0-dev Productivity Python libudev-dev
libsndfile-dev Raspberry libasound2-dev libjpeg8-dev l
Pi Regex Roguelike Scheme SciPy
SDL 2 can be downloaded from https://www.libsdl.org/download-2.0.php, be Swift Videos Windows WSL
sure to select the source code archive, SDL2-2.0.3.tar.gz, or a newer version
if available. You can download and extract the archive directly on your
Raspberry Pi:
1 cd ~ Recent posts
2 wget https://www.libsdl.org/release/SDL2-2.0.3.tar.gz
3 tar zxvf SDL2-2.0.3.tar.gz Getting started with Swift 3 on Windows
4 cd SDL2-2.0.3 && mkdir build && cd build Subsystem for Linux
Using the Windows Subsystem for Linux with
Now, it is time to configure SDL 2:
Xfce 4
1 ../configure --disable-pulseaudio --disable-esd --disable-video-mir --disable-video-wayland
Install WSL - Windows--disable-video-x11
Subsystem for Linux --disable-video-opengl

Transferring data between Matlab and


or, if you have a Raspberry Pi 2:
Armadillo

1
1 ../configure --host=armv7l-raspberry-linux-gnueabihf --disable-pulseaudio --disable-esd --disable-video-mir --disable-video-wayland --disabl
Getting started with Armadillo a C++ Linear
Algebra Library on Windows, Mac and Linux
The above options will make sure, SDL 2 is built with the OpenGL ES
Install NumPy, SciPy, Matplotlib with Python 3
backend and that any SDL application will run as a full screen application, the
on Windows
windowed mode under X tends to be more buggy on Raspberry Pi. On the
iOS Remote Push Notifications with a Node.js
plus side, you will be able to launch your application, or game, directly from
backend
the text interface without the overhead of running the X server. If you wish
Arduino buzzer player with adjustable volume -
you can start your application from LXDE (the default window manager for
playing a part of Fr Elise by Ludwing van
Raspbian) and the application will run full screen.
Beethoven
Assuming, youve had no error in the configure phase, you can actually build Beginning C Game Development with SDL 2 -
and install the library: video series
Arduino - pulse an LED with a cosine wave
1 make -j 4
2 sudo make install function

Now, you should be able to compile any C++ code that uses SDL 2 with
something like:
1 g++ -o program_name program_name.cpp `sdl2-config --cflags --libs`

SDL 2 by itself can use only .bmp graphic files and .wav audio files. If you
want to be able to load other image formats, like .png, you can use
SDL_image 2.0 or something like FreeImage. The advantage of using
SDL_image is that this is directly compatible with SDL 2, basically you will
load an image file in a format that will just work with SDL 2. Similarly, there
are libraries for loading sound files, SDL_mixer 2.0, or font files SDL_ttf 2.0.

Since working with images in various formats is a must for almost any SDL 2
application, I will exemplify, shortly, how to download, build and install
SDL_image:

1 cd ~
2 wget http://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.0.tar.gz
3 tar zxvf SDL2_image-2.0.0.tar.gz
4 cd SDL2_image-2.0.0 && mkdir build && cd build
5 ../configure
6 make -j 4
7 sudo make install

If your code uses SDL_image you will need to explicitly link the SDL_image
library, e.g.:

1 g++ -o program_name program_name.cpp `sdl2-config --cflags --libs` -lSDL2_image

2
The SDL_ttf library can be built similarly. If you want to build SDL_mixer you
will need to build and install first smpeg2, after which you can build and install
SDL_mixer like before.

Time to write a short code example that will open an SDL window and show
an image for 10 seconds, after which will free the resources and exit. I
assume that youve successfully installed SDL 2 and SDL_image on your
Raspberry Pi.
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_image.h>
3 #include <iostream>
4
5 // Manage error messages
6 void check_error_sdl(bool check, const char* message);
7 void check_error_sdl_img(bool check, const char* message);
8
9 // Load an image from "fname" and return an SDL_Texture with the content of the image
10 SDL_Texture* load_texture(const char* fname, SDL_Renderer *renderer);
11
12
13 int main(int argc, char** argv) {
14 // Initialize SDL
15 check_error_sdl(SDL_Init(SDL_INIT_VIDEO) != 0, "Unable to initialize SDL");
16
17 // Create and initialize a 800x600 window
18 SDL_Window* window = SDL_CreateWindow("Test SDL 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
19 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
20 check_error_sdl(window == nullptr, "Unable to create window");
21
22 // Create and initialize a hardware accelerated renderer that will be refreshed in sync with your monitor (at approx. 60 Hz)
23 SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
24 check_error_sdl(renderer == nullptr, "Unable to create a renderer");
25
26 // Set the default renderer color to corn blue
27 SDL_SetRenderDrawColor(renderer, 100, 149, 237, 255);
28
29 // Initialize SDL_img
30 int flags=IMG_INIT_JPG | IMG_INIT_PNG;
31 int initted = IMG_Init(flags);
32 check_error_sdl_img((initted & flags) != flags, "Unable to initialize SDL_image");
33
34 // Load the image in a texture
35 SDL_Texture *texture = load_texture("img_test.png", renderer);
36
37 // We need to create a destination rectangle for the image (where we want this to be show) on the renderer area
38 SDL_Rect dest_rect;
39 dest_rect.x = 50; dest_rect.y = 50;
40 dest_rect.w = 337; dest_rect.h = 210;
41
42 // Clear the window content (using the default renderer color)
43 SDL_RenderClear(renderer);

3
44
45 // Copy the texture on the renderer
46 SDL_RenderCopy(renderer, texture, NULL, &dest_rect);
47
48 // Update the window surface (show the renderer)
49 SDL_RenderPresent(renderer);
50
51 // Wait for 10 seconds
52 SDL_Delay(10000);
53
54 // Clear the allocated resources
55 SDL_DestroyTexture(texture);
56 IMG_Quit();
57 SDL_DestroyRenderer(renderer);
58 SDL_DestroyWindow(window);
59 SDL_Quit();
60
61 return 0;
62 }
63
64 // In case of error, print the error code and close the application
65 void check_error_sdl(bool check, const char* message) {
66 if (check) {
67 std::cout << message << " " << SDL_GetError() << std::endl;
68 SDL_Quit();
69 std::exit(-1);
70 }
71 }
72
73 // In case of error, print the error code and close the application
74 void check_error_sdl_img(bool check, const char* message) {
75 if (check) {
76 std::cout << message << " " << IMG_GetError() << std::endl;
77 IMG_Quit();
78 SDL_Quit();
79 std::exit(-1);
80 }
81 }
82
83 // Load an image from "fname" and return an SDL_Texture with the content of the image
84 SDL_Texture* load_texture(const char* fname, SDL_Renderer *renderer) {
85 SDL_Surface *image = IMG_Load(fname);
86 check_error_sdl_img(image == nullptr, "Unable to load image");
87
88 SDL_Texture *img_texture = SDL_CreateTextureFromSurface(renderer, image);
89 check_error_sdl_img(img_texture == nullptr, "Unable to create a texture from the image");
90 SDL_FreeSurface(image);
91 return img_texture;
92 }

Assuming that youve saved the above code as sdl2_test.cpp and saved this
image in the same folder as the C++ source file, you can compile and run the
code with:

4
1 g++ -std=c++0x -Wall -pedantic sdl2_test.cpp -o sdl2_test `sdl2-config --cflags --libs` -lSDL2_image
2 ./sdl2_test

The above instructions will use the default version of GCC, 4.6.x, from
Raspbian that has only partial support for C++11. If you want to use a more
modern C++ compiler, with full support for C++11 and partial support for
C++14, read my previous article. In this case you should use something like:

1 g++-4.9 -std=c++14 -Wall -pedantic sdl2_test.cpp -o sdl2_test `sdl2-config --cflags --libs` -lSDL2_image
2 ./sdl2_test

This is what you should see:

If you want to learn more about SDL I would recommend reading SDL Game
Development by Shaun Mitchel.

or The Black Art of Multiplatform Game Programming by Jazon Yamamoto.

5
We were unable to load Disqus. If you are a moderator please see our troubleshooting
guide.

Disclaimer:
All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or
validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information
is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This
information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale.
Thank you for understanding.

Copyright 2017 - solarianprogrammer.com

You might also like