A few problems with SDL in Ubuntu - Dapper

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

A few problems with SDL in Ubuntu - Dapper

Postby GMorgan » Mon Jul 17, 2006 4:19 pm

I'm turning my programming attention towards SDL but I'm running into a few difficulties. I've been following the first tutorial on this website. The program I'm running is below.

Code: Select all
#include <SDL/SDL.h>
#include <iostream>

using namespace std;

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B);
void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *screen);

int main(){
   if( SDL_Init( SDL_INIT_VIDEO|SDL_INIT_AUDIO ) < 0 )
   {
      cout << "Unable to init SDL: " << SDL_GetError() << endl;
      return 1;
   }
   atexit(SDL_Quit);
   
   SDL_Surface *screen;
   screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
   
   if ( screen == NULL )
   {
      cout << "Unable to set 640x480 video: " << SDL_GetError() << endl;
      return 1;
   }
   
   return 0;
}

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B){
   Uint32 color = SDL_MapRGB(screen->format, R, G, B);
   switch (screen->format->BytesPerPixel)
   {
      case 1: // Assuming 8-bpp
      {
         Uint8 *bufp;
         bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
         *bufp = color;
      }
      break;
      case 2: // Probably 15-bpp or 16-bpp
      {
         Uint16 *bufp;
         bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
         *bufp = color;
      }
      break;
      case 3: // Slow 24-bpp mode, usually not used
      {
         Uint8 *bufp;
         bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
         if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
         {
            bufp[0] = color;
            bufp[1] = color >> 8;
            bufp[2] = color >> 16;
         } else{
            bufp[2] = color;
            bufp[1] = color >> 8;
            bufp[0] = color >> 16;
         }
      }
      break;
      case 4: // Probably 32-bpp
      {
         Uint32 *bufp;
         bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
         *bufp = color;
      }
      break;
   }
}

void Slock(SDL_Surface *screen){
   if ( SDL_MUSTLOCK(screen) )
   {
      if ( SDL_LockSurface(screen) < 0 )
      {
         return;
      }
   }
}

void Sulock(SDL_Surface *screen){
   if ( SDL_MUSTLOCK(screen) )
   {
      SDL_UnlockSurface(screen);
   }
}


and the output from the compiler looks like this.

Code: Select all
cd '/home/gareth/documents/Programming/C-C++/sdl1/debug' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" make -k
make all-recursive
Making all in src
compiling sdl1.cpp (g++)
linking sdl1 (libtool)
linking sdl1 (g++)
sdl1.o: In function `main':/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:13: undefined reference to `SDL_Init'
:/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:15: undefined reference to `SDL_GetError'
:/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:18: undefined reference to `SDL_Quit'
:/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:21: undefined reference to `SDL_SetVideoMode'
:/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:25: undefined reference to `SDL_GetError'
sdl1.o: In function `DrawPixel(SDL_Surface*, int, int, unsigned char, unsigned char, unsigned char)':/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:33: undefined reference to `SDL_MapRGB'
sdl1.o: In function `Slock(SDL_Surface*)':/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:79: undefined reference to `SDL_LockSurface'
sdl1.o: In function `Sulock(SDL_Surface*)':/home/gareth/documents/Programming/C-C++/sdl1/src/sdl1.cpp:89: undefined reference to `SDL_UnlockSurface'
collect2: ld returned 1 exit status
make[2]: *** [sdl1] Error 1
make[2]: Target `all' not remade because of errors.
make[2]: Nothing to be done for `all-am'.
make[1]: *** [all-recursive] Error 1
make: *** [all-recursive-am] Error 2
make: Target `all' not remade because of errors.
*** Exited with status: 2 ***


At first I thought the #include <SDL/SDL.h> statement was wrong but upon trying different combinations I got a ton more of errors, this second load of errors was repeated for every combination bar the original file so I'm assuming the statement was correct.

Any help would be appreciated.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: A few problems with SDL in Ubuntu - Dapper

Postby Nigel » Mon Jul 17, 2006 4:30 pm

Looks like you're not including the SDL libraries in your link... is it a home-brewed makefile ?
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: A few problems with SDL in Ubuntu - Dapper

Postby GMorgan » Mon Jul 17, 2006 5:11 pm

I'm using Kdevelop, I started it as a simple hello world.

//edit - I've switched to a simple SDL project and it works now but the question is what has changed. I don't want to be unnecessarily tied to one IDE//
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: A few problems with SDL in Ubuntu - Dapper

Postby Nigel » Tue Jul 18, 2006 9:05 am

Check the makefile it has generated. I'll bet the new version includes the SDL libraries in the link whereas the old one didn't.
Look for options something like
Code: Select all
 -lSDL -lSDL_mixer
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: A few problems with SDL in Ubuntu - Dapper

Postby GMorgan » Tue Jul 18, 2006 2:26 pm

I do have entries along these sorts of lines

LIBSDL_CFLAGS = @LIBSDL_CFLAGS@
LIBSDL_LIBS = @LIBSDL_LIBS@
LIBSDL_RPATH = @LIBSDL_RPATH@

# set the include path found by configure
AM_CPPFLAGS = $(LIBSDL_CFLAGS) $(all_includes)

# the library search path.
sdl1a_LDFLAGS = $(all_libraries) $(LIBSDL_RPATH)
sdl1a_LDADD = $(LIBSDL_LIBS)
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
CONFIG_HEADER = ../config.h
CONFIG_CLEAN_FILES =
bin_PROGRAMS = sdl1a$(EXEEXT)
PROGRAMS = $(bin_PROGRAMS)

but I couldn't find the options mentioned when I greped the makefile.

I'm having trouble with seg faults on the second tutorial but I'll try to solve it myself before I come crying too much.
GMorgan
LXF regular
 
Posts: 684
Joined: Thu Jan 12, 2006 6:58 pm
Location: South Wales, UK

RE: A few problems with SDL in Ubuntu - Dapper

Postby Nigel » Wed Jul 19, 2006 9:39 am

That's the downside of using an IDE... most of them will try to cater for so many possible situations that the makefiles they produce are horrendous tangles of spaghetti... almost as bad as the ones I write by hand :roll:
Still, if you're up & running, I'd stick with Kdevelop - switching IDEs mid-project is not for the faint-hearted !
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK


Return to Programming

Who is online

Users browsing this forum: Bing [Bot] and 0 guests