943,947 Members | Top Members by Rank

Ad:
Aug 26th, 2007
0

sdl color key, don't understand

Expand Post »
Hey guys I can't figure out how to get the sdl colorkey to work, I've tried to find a tutorial on it, but nothing seemed to make sense.
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
                {
                    printf( "Unable to init SDL: %s\n", SDL_GetError() );
                    return 1;
                }

            // make sure SDL cleans up before exit

            atexit(SDL_Quit);
            SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);

            if ( !screen )
                {
                    printf("Unable to set 640x480 video: %s\n", SDL_GetError());
                    return 1;
                }

            SDL_WM_SetCaption( "Battle Pong!", NULL );

            // load an image

            SDL_Surface* bmp = SDL_LoadBMP("pong.bmp");
            if(SDL_SetColorKey(bmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmp->format, 0, 0 ,0)) == -1)
            fprintf(stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError());
            }
Gives me an error at the maprrgb function, it just says error at this point.

What am I doing wrong?

Thanks,


Joe
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
avgprogramerjoe is offline Offline
7 posts
since Aug 2007
Aug 27th, 2007
0

Re: sdl color key, don't understand

Since no one here is answering the post, could a moderator please move it to the game dev forum?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
avgprogramerjoe is offline Offline
7 posts
since Aug 2007
Aug 27th, 2007
0

Re: sdl color key, don't understand

moved, but I don't know if it will help.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 27th, 2007
0

Re: sdl color key, don't understand

Are you getting a compiler error or a runtime error? I get a compiler error if I try to build that:
C? Syntax (Toggle Plain Text)
  1. if(SDL_SetColorKey(bmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmp->format, 0, 0 ,0)) == -1)
  2. //<----THERE SHOULD BE AN OPENING BRACE
  3. fprintf(stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError());
  4. } //<---- OR NO CLOSING BRACE

But, that might just be because you've pasted the body of a function/block and missed the opening brace from the start of the function/block....

I don't get any runtime error once I successfully compile; if you're getting a runtime error, try taking out the SDL_MapRGB(bmp->format, 0, 0 ,0) and replacing it with 0. Based on literature here: http://docs.mandragor.org/files/Comm...xelformat.html, I reckon it's a safe bet that RGB 0, 0, 0 will usually be 'color key' 0, UNLESS you're using a palleted bitmap.. Try it, see if it works and if you get the right effect... Also, check if the bitmap was successfully loaded before trying to access bitmap->format. I got a segfault the first time I ran that, because I didn't have a pong.bmp file.. Do a check like:
C++ Syntax (Toggle Plain Text)
  1. SDL_Surface* bmp = SDL_LoadBMP("pong.bmp");
  2. if( bmp == NULL )
  3. {
  4. cerr << "Bitmap didn't load: " << SDL_GetError();
  5. return 387;
  6. }
Last edited by MattEvans; Aug 27th, 2007 at 2:19 pm. Reason: pressed 'enter' too early..
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Aug 31st, 2007
0

Re: sdl color key, don't understand

heres a function that should work:
c++ Syntax (Toggle Plain Text)
  1. SDL_Surface* getImage(string filename, int r=255, int g=0, int b=198)
  2. {
  3. SDL_Surface* loadedImage=0;
  4. SDL_Surface* optimizedImage=0;
  5.  
  6. loadedImage=IMG_Load(filename.c_str());
  7. optimizedImage = SDL_DisplayFormat(loadedImage);
  8. SDL_FreeSurface(loadedImage);
  9.  
  10. Uint32 colorkey=SDL_MapRGB(optimizedImage->format, r, g, b);
  11. SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);
  12.  
  13. return optimizedImage;
  14. }
Reputation Points: 343
Solved Threads: 24
Veteran Poster
Sturm is offline Offline
1,067 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: J2ME Tutorial
Next Thread in Game Development Forum Timeline: 313(Heroes)[ROME]Forum





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC