Generesque
Posts: 2
Joined: Tue Aug 07, 2012 1:46 am

Idiot's guide to C++/SDL Setup

Tue Aug 07, 2012 2:08 am

Hello all,

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/viewt ... =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 ... re-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.

pvegh
Posts: 2
Joined: Tue Jul 31, 2012 8:50 am

Re: Idiot's guide to C++/SDL Setup

Tue Aug 07, 2012 8:05 pm

Well, "Idiot's guide" is not the most catchy title.
Thanks for the effort anyway:)
I am pretty sure some people will have a good use of this.

karrotsrkool
Posts: 23
Joined: Sun Jul 29, 2012 9:47 pm

Re: Idiot's guide to C++/SDL Setup

Tue Aug 07, 2012 8:32 pm

God send dude, thanks so much, saved me a bunch of time.

Generesque
Posts: 2
Joined: Tue Aug 07, 2012 1:46 am

Re: Idiot's guide to C++/SDL Setup

Tue Aug 07, 2012 8:50 pm

Haha, good point pvegh. The "idiot" part was meant to reference myself, not those using the guide. The fact that the meaning is unclear only seems to prove my point :P

NachoC
Posts: 4
Joined: Sat Oct 08, 2011 9:10 pm
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Wed Aug 08, 2012 10:26 pm

Thanks for recopilating this. I also wanted to port old SDL+C programs to the Pi, so I installed Geany using apt-get, decompressed the source code for my program, and tried simply a

Code: Select all

sudo apt-get install libsdl1.2-dev
But no way... barely 4 packages were properly downloaded and after that I got...

Code: Select all

Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libpulse-mainloop-glib0 armhf 2.0-3
  404  Not Found
Err http://mirrordirector.raspbian.org/raspbian/ wheezy/main libpulse-dev armhf 2.0-3
  404  Not Found
I also tried using the flag "--ignore-missing", but it didn't work, either, So I was going to quit for today and try another day, but you have given me something more to look at.

I got surprised by not having Pygame nor SDL-dev in the recommended distro, and even more by not being able to install SDL-dev from the repositories, and did not know anything about the update. I hope this will work. Thanks again.

User avatar
gordon@drogon.net
Posts: 1961
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Thu Aug 09, 2012 11:06 am

NachoC wrote: I got surprised by not having Pygame nor SDL-dev in the recommended distro, and even more by not being able to install SDL-dev from the repositories, and did not know anything about the update. I hope this will work. Thanks again.
sdl-dev is definately there:

Code: Select all

gordon @ raspberrypi: dpkg -l | grep -i sdl | grep dev
ii  libsdl1.2-dev                         1.2.15-5                           Simple DirectMedia Layer development files
ii  python-pygame                         1.9.1release+dfsg-6                SDL bindings for games development in Python
This is on a raspbian box.

Do make sure you do the apt-get update ; apt-get upgrade regularly though and check your /etc/apt/sources.list file. Mine has:

Code: Select all

deb http://archive.raspbian.org/raspbian/ wheezy main contrib non-free rpi
-Gordon
--
Gordons projects: https://projects.drogon.net/

blomkaal
Posts: 1
Joined: Fri Aug 10, 2012 12:42 pm

Re: Idiot's guide to C++/SDL Setup

Fri Aug 10, 2012 12:55 pm

I haven't followed the guide completely, but I seem to have SDL installed from the repos correctly anyway. However, when I try to run any SDL-code, I get a segmentation fault. In the case of the test program, I get a bunch of lines that look something like

Code: Select all

[838.350406] bcm2708_fb_check_var info(c78ec400) 1920x1080 (1920x1080), 4177920, 16
[838.350426] bcm2708_fb_check_var var(c68d9dd8) 320x200 (320x200), 32, 1044480
[\code]
Followed by the segfault.

The numbers vary, of course, there are just examples. I've tried throwing in a few prints into the code, and it seems to happen when calling SDL_GetVideoInfo().

This happens whenever I compile using gcc, or my usual way using g++ (which would be similar to [code]g++ sdl.cpp -lSDL[\code])
Any ideas as to what I might be missing/doing wrong?

NachoC
Posts: 4
Joined: Sat Oct 08, 2011 9:10 pm
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Fri Aug 10, 2012 3:50 pm

It worked!

In my just-installed Raspbian, sdl-dev was not there. I upgraded by using:

Code: Select all

sudo apt-get update
sudo apt-get dist-upgrade
sudo shutdown -r now
sudo apt-get install gcc build-essential libsdl1.2-dev
And since then, I was able to compile, and the resulting executable worked both in console and in X.

Thanks a lot, Gordon and Generesque.

And about blomkaal's question, I'm afraid we need more information. For example:

- Which graphics video mode are you trying to switch to? (e.g. 640x480x32).
- Which other options are you (or your program) using: SWSURFACE or HWSURFACE? DOUBLEBUF? FULLSCREEN?

Look for "SDL.setVideoMode" in your source, that might help.

gtoal
Posts: 86
Joined: Sun Nov 18, 2012 12:02 am

Re: Idiot's guide to C++/SDL Setup

Thu Nov 29, 2012 3:37 pm

Thanks for the pointers to get SDL working! I'm having trouble however working out how to install whatever is needed to use OpenGL on Raspbian (wheezy). Any clues? Is it freeglut3? OpenGLES?

thanks,

Graham

User avatar
hexelpdkk
Posts: 175
Joined: Fri Feb 24, 2012 4:40 pm

Re: Idiot's guide to C++/SDL Setup

Thu Nov 29, 2012 5:48 pm

gtoal wrote:Thanks for the pointers to get SDL working! I'm having trouble however working out how to install whatever is needed to use OpenGL on Raspbian (wheezy). Any clues? Is it freeglut3? OpenGLES?

thanks,

Graham
Everything is there out of the box for OpenGLES 2.0, onder the /opt/vc directories - for an example, see /opt/vc/src/hello_pi/hello_triangle. The pi does not support OpenGL, only OpenGLES.

User avatar
AndyD
Posts: 2320
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Fri Nov 30, 2012 1:23 am

@gtoal I am sure someone will correct me if I am wrong. My understanding is that the Raspberry Pi uses OpenGL ES rather than standard OpenGL. At the moment to use OpenGL ES you need to follow the examples in /opt/vc/src/hello_pi/hello_triangle or /opt/vc/src/hello_pi/hello_triangle2. At the moment SDL isn't hardware accelerated. If you use OpenGL on the Raspberry Pi, you will not get any hardware acceleration (you will be using Mesa). I don't think you can't use OpenGL ES with SDL.

gtoal
Posts: 86
Joined: Sun Nov 18, 2012 12:02 am

Re: Idiot's guide to C++/SDL Setup

Tue Dec 11, 2012 3:15 am

@hexelpdkk,@AndyD: thanks for the pointer to the example code. I'll have some free time over Christmas holidays, expect a posting with a vector game demo by way of thanks :-)

User avatar
Shawty
Posts: 57
Joined: Fri Nov 16, 2012 1:22 am
Location: North East UK
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Wed Dec 12, 2012 8:03 pm

@bloomkaal - Make sure your not running from an SSH session. I just did exactly that and got the same segmentation fault. As soon as I picked up the keyboard directly attached to my Pi and ran the code it worked as expected.

I'm using an SSH terminal on my PC to type code (The font on my TV is just to tiny to read) but in the case of SDL (and unlike many others that still use the frame-buffer even in ssh) it will bork if you try to rub frame buffer gfx from an SSH session.
still crazy (Even since the days of my BBC Model B) BEST and only way to be ;-)

IM: @shawty_ds on twitter
if you remember the Acorn and BBC days then I was "!Shawty! of DSPD" (Author of the BBC B Sound Tracker suite, and the Dreamscape demo)

kouky
Posts: 6
Joined: Sat Jun 09, 2012 10:48 am
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Fri Dec 28, 2012 3:48 pm

That was a great tutorial, thank you for that.

How can I install the SDL_ttf libs in addition to SDL?

I tried few tricks but none worked...

kouky
Posts: 6
Joined: Sat Jun 09, 2012 10:48 am
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Fri Dec 28, 2012 4:01 pm

Actualy I found the solution:

for SDL_ttf:
sudo apt-get install gcc build-essential libsdl-ttf2.0-dev

and for SDL_image:
sudo apt-get install gcc build-essential libsdl-image1.2-dev

correct me if I'm wrong...

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Idiot's guide to C++/SDL Setup

Mon Dec 31, 2012 1:31 pm

kouky wrote:Actualy I found the solution:

for SDL_ttf:
sudo apt-get install gcc build-essential libsdl-ttf2.0-dev

and for SDL_image:
sudo apt-get install gcc build-essential libsdl-image1.2-dev

correct me if I'm wrong...
As far as I know, you donät need to include the 'gcc build-essential' bit in every call to apt-get - your call effectively translates to:

sudo apt-get install gcc
sudo apt-get install build-essential
sudo apt-get install libsdl-image1.2-dev

...so to install one single lib/app you would only do this:

sudo apt-get install libsdl-image1.2-dev

(not that it hurts - other than maybe wasting some time checking for updates)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Luutifa
Posts: 16
Joined: Mon Nov 26, 2012 6:20 pm

Re: Idiot's guide to C++/SDL Setup

Thu Mar 07, 2013 8:29 pm

I really want to see that you CAN compile your own SDL programs on the Pi (I've never really done any SDL), but I get:

Code: Select all

pi@luuti-raspberrypi ~/programs/testsdl $ make
gcc `sdl-config --cflags` `sdl-config --libs` -O2 -o sdltest main.cpp
/tmp/ccuFVBNJ.o: In function `main':
main.cpp:(.text.startup+0x94): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp:(.text.startup+0xa8): undefined reference to `std::cout'
/tmp/ccuFVBNJ.o: In function `_GLOBAL__sub_I_main':
main.cpp:(.text.startup+0xb8): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text.startup+0xd4): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status
make: *** [build] Error 1
I've done every update and cleaning/optimization.

The problem seems to be related to "SDL_HWSURFACE" at line 19 (The "Set up screen" line).
EDIT: I
meant "SDL_SWSURFACE"
Thanks for any help!

User avatar
Paeryn
Posts: 1608
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Idiot's guide to C++/SDL Setup

Thu Mar 07, 2013 10:02 pm

Luutifa wrote:I really want to see that you CAN compile your own SDL programs on the Pi (I've never really done any SDL), but I get:

Code: Select all

pi@luuti-raspberrypi ~/programs/testsdl $ make
gcc `sdl-config --cflags` `sdl-config --libs` -O2 -o sdltest main.cpp
If you're compiling C++ you'll want to use g++ not gcc.

Whilst gcc will compile C++ it won't automatically link in the C++ libraries. If you want to stick with gcc you'll need to add -lstdc++ to the command line.

Code: Select all

gcc `sdl-config --cflags` `sdl-config --libs` -O2 -o sdltest main.cpp -lstdc++
She who travels light — forgot something.

Luutifa
Posts: 16
Joined: Mon Nov 26, 2012 6:20 pm

Re: Idiot's guide to C++/SDL Setup

Fri Mar 08, 2013 9:35 am

Oh!
I'll try that right away when I get home.

I just used "gcc" because the instructions said so.

lparker
Posts: 2
Joined: Wed Mar 27, 2013 12:20 pm

Re: Idiot's guide to C++/SDL Setup

Wed Mar 27, 2013 12:29 pm

I've gone through the guide and done everything in order. But I'm having a bit of problem compiling the code (which I've been through a few times.).

When I compile it with either gcc and g++ I get the following error.

Code: Select all

MakeWin.cpp: In fintion 'int main(int, char**)':
MakeWin.cpp:24:27: error: conversion from 'int' to non-scalar type 'SDL_Rect' requested.
I have a basic understanding of c++ and c (Along with a few more languages.) but I can't seem to find anything from this on google. etc.

User avatar
Shawty
Posts: 57
Joined: Fri Nov 16, 2012 1:22 am
Location: North East UK
Contact: Website

Re: Idiot's guide to C++/SDL Setup

Wed Mar 27, 2013 2:52 pm

I couldn't tell you the exact cause (I don't have the source code to hand and my rPI isn't even switched on.

BUT.... in general

What the error message is telling you is that you have an "int" variable somewhere and your trying to use it as a "SDL_Rect" variable.

now there are a number of reasons for this, but the 2 most likely are

1) You've forgotten to put a #include <somefile.h> statement in your code to include a required header file that defines the relationship between "int" and "SDL_Rect" (Most commonly the file you need will be sdl.h)

or

2) Where you meant to type something like:

SDL_Rect myRect = .......

you've typed

int myRect = .....

instead.

Hopefully that gives you a bit of direction to start investigating. :-)

It could be an error in the include file too, but someone else will have to help you with that one.

Shawty
still crazy (Even since the days of my BBC Model B) BEST and only way to be ;-)

IM: @shawty_ds on twitter
if you remember the Acorn and BBC days then I was "!Shawty! of DSPD" (Author of the BBC B Sound Tracker suite, and the Dreamscape demo)

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 5 guests