I just started playing around with the RPI, and one of the first things I wanted to do was get a C++/SDL setup going. I've got some old SDL projects lying around and I wanted to see how they'd compile and run. Sadly, I've not been working in a linux environment for some time, so I had to do some digging to remember the basic setup steps. In the hopes of saving other users some time, here is everything I had to do on a fresh Raspbian install to get C++ and SDL compiling and running:
Get rpi-update
Source: https://github.com/Hexxeh/rpi-update/
- Code: Select all
sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update && chmod +x /usr/bin/rpi-update
Install Git (for rpi-update)
- Code: Select all
sudo apt-get install git-core
Update Everything
Source: http://www.raspbian.org/RaspbianQuake3
- Code: Select all
sudo apt-get update
sudo apt-get dist-upgrade
sudo rpi-update 192
You will need to reboot after this step.
Install relevant packages
Source: http://www.raspbian.org/RaspbianQuake3
- Code: Select all
sudo apt-get install gcc build-essential libsdl1.2-dev
Note that this step may not work if you haven't run the above upgrade steps. This tripped me up for a little while.
Add User to Video Group
Source: http://www.raspbian.org/RaspbianQuake3
- Code: Select all
sudo usermod -a -G video [your_username]
This step allows your user to run SDL programs from the console. You may need to log out and log back in after this step.
Create a Test Program
Source: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=33&t=8454
Create a new .cpp file in a new directory. Edit the contents to look like this:
- Code: Select all
#include <stdio.h>
#include "SDL.h"
int main( int argc, char* args[] )
{
SDL_Surface* screen = NULL;
//Start SDL
SDL_Init( SDL_INIT_VIDEO );
const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo ();
int systemX = videoInfo->current_w ;
int systemY = videoInfo->current_h ;
Uint8 bpp = videoInfo->vfmt->BitsPerPixel ;
//Set up screen
screen = SDL_SetVideoMode( systemX, systemY, bpp, SDL_SWSURFACE );
if (!screen)
{
printf("SDL_SetVideoMode failed\n");
return 0;
}
SDL_Rect r = {0,0,320,240};
SDL_FillRect(screen,&r, SDL_MapRGB(screen->format, 200,200,0) );
//Update Screen
SDL_Flip( screen );
//Pause
SDL_Delay( 2000 );
//Quit SDL
SDL_Quit();
return 0;
}
Compile the Test Program
Source: http://www.linuxquestions.org/questions/programming-9/gcc-cant-find-sdl-h-but-its-there-133384/
Use GCC to compile the program using the following command:
- Code: Select all
gcc `sdl-config --cflags` `sdl-config --libs` -o sdltest main.cpp
Change main.cpp to whatever you named your source file.
Run the Test Program
- Code: Select all
./sdltest
You should see a yellow rectangle on the screen for a couple seconds. Not very impressive, but it means your program compiled correctly.
A couple important points I came across:
- SDL_HWSURFACE and/or SDL_DOUBLEBUFFER seem to cause problems. SDL_SWSURFACE seems to be the safest option for now
- Setting the Video Mode width and height arbitrarily causes issues. Use SDL_GetVideoInfo() to get around this problem
Please note I didn't come up with this myself, I just took the information I found in various places and put it in one post. I'd especially like to thank gordon@drogon.net, as his posts on this forum were extremely helpful.