[Solved!] SDL setup on Raspbian?

General programming chat and advice for beginners

1 post
by DangerPenguin » Tue Jul 02, 2013 10:10 pm
I Have SDL running and compiling, look for my last edit at the bottom of this post, it has full instructions to get SDL compiling from a FRESH Raspbian Wheezy image.

Haven't touched my PI in QUITE a while, and had some questions about the Raspbian distro and simple game dev (the current image is what I'm writing to my card right now).

*Is SDL ready to go out-of-the-box on this image?
*I've done a ton of Google-fu, and everyone compiles/links SDL differently on the Pi (and only a handful apt-get the libsdl1.2-dev). From a fresh Raspbian image, what is needed to compile/link a simple SDL program?

Keep in mind, I come from an almost purely windows background, but have some experience in C/C++, almost zero SDL experience( though my google-fu made SDL look simpler that openGl es 2.0 for 2d stuffs ).

Now time to boot this thing!

EDIT:: Using the command dpkg --list from the terminal shows that raspbian already has some SDL packages installed.
*libsdl-image
*libsdl-mixer
*libsdl-ttf2.0
*libsdl1.2debian

Since libsdl1.2debian is already installed, I shouldn't need to install anything right? (All evidence points to libsdl1.2debian being the core files, but I'm a madman and have been wrong before)

EDIT2:: I might have answered my question. I made the terminal bigger to get the full package names, and none of the SDL packages were the -dev packages. So im installing all the dev packages using
sudo apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev
Lets see how this goes.

FINAL EDIT:: SDL WORKING AND COMPILING!
Instructions from a FRESH Raspbian Wheezy install.

Go to the terminal and run
Code: Select all
sudo apt-get update
sudo apt-get upgrade

This is to make sure everything on your system is up to date.


Now from the terminal run
Code: Select all
sudo apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev

This installs all the dev packages for SDL, it might prompt you to press Y a couple of times, just do it.

Then make a folder for your projects. You can do it in the file manager, or form the command line (when you open up a terminal you should already be in the /home/user/ folder) type
Code: Select all
mkdir c_games
cd c_games
mkdir your_project_name
cd your_project_name

This makes the file structure /home/user/c_games/your_project_name/ and puts you in the /your_project_name/ folder

Now you need to create a .cpp file. You can either do this from your favorite editor, or since you're already at the folder in your terminal, type
Code: Select all
touch your_project_name.cpp


Now we need to add the actual code, you can again do this in your favorite text editor, or to open up the file in the terminal type
Code: Select all
nano your_project_name.cpp


Once your file is open paste in this code (if at the terminal you HAVE to right-click to paste)
Code: Select all
#include "SDL/SDL.h"

int main(int argc, char* args[])
{
   //The images
   SDL_Surface* screen = NULL;
   SDL_Surface* my_image = NULL;

   //Start SDL
   SDL_Init( SDL_INIT_EVERYTHING );

   //The video mode - to work on rPI
   const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo();

   //Setup screen
   screen = SDL_SetVideoMode(videoInfo->current_w, videoInfo->current_h, videoInfo->vfmt->BitsPerPixel, SDL_SWSURFACE);

   //load image
   my_image = SDL_LoadBMP("my_image_file.bmp");

   //Apply image to screen
   SDL_BlitSurface( my_image, NULL, screen, NULL);

   //Update Screen
   SDL_Flip(screen);

   //Pause
   SDL_Delay(3000);

   //Free images
   SDL_FreeSurface( my_image );

   //Quit SDL
   SDL_Quit();

   return 0;
}

Now save this. From the terminal in nano you hit ctrl-X (ctrl and X together), type Y to save changes and when it prompts you for the file name just hit enter to keep the old file name.

If you look at that code, I'm just initializing SDL, loading a BMP image, drawing the image to the screen, flipping the screen (this displays the "screen" to the users monitor), waiting 3 seconds, then exiting.

All you need to do is put a bmp file in the same folder as your cpp file. One side note, you need to MAKE SURE it's a true bmp file, and not just another image with the extension changed (if that's the case it WON'T display, you'll just have that black screen). To check this, open up the image in a text editor. It will be mostly gibberish, but the FIRST 2-3 characters tell you the true file type, make sure they're BM, NOT GIF, JPG, ect. If it isn't BM open the image in a image editor and export it as a BMP file.

Now we have to compile this bad boy! If you weren't using the terminal, you have to now. Use the instructions earlier to change directories (the cd command) to /home/user/c_games/your_project_name/ .

To compile the project we type this code
Code: Select all
g++ -o your_output_exe_name your_project_name.cpp -lSDL

There's only a tiny bit of black magick going on here. You're asking the g++ compiler to compile this project, (the -o is black magick to me, but a tutorial suggested it and it works), the your_output_exe_name part can be replaced with what you want the exe to be named, like AwesomeGame2013, the your_project_name.cpp HAS to be the name of your .cpp file, then to finish it off you're telling the g++ compiles we're doing SDL (its a hyphen, LOWERCASE L, then capital SDL, without spaces)

When that's done you can run your exe one of two ways, find and double click the exe file in file manager, or if you're still in the project folder directory in your terminal, just type
Code: Select all
./your_output_exe_name

NOTE: you don't need to add .exe to the end of it, but you DO need the preceeding ./ which tells the terminal to look in the current directory (folder). Just replace your_output_exe_name with what you used when you compiled the project!

If you copied the code exactly, then you now get to see your image in the top left of the screen for 3 seconds before it disappears! You did it! WOOOOO!
User avatar
Posts: 5
Joined: Tue Jul 17, 2012 8:39 pm