| | |
sdl color key, don't understand
![]() |
•
•
Join Date: Aug 2007
Posts: 7
Reputation:
Solved Threads: 0
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.
Gives me an error at the maprrgb function, it just says error at this point.
What am I doing wrong?
Thanks,
Joe
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());
}What am I doing wrong?
Thanks,
Joe
Are you getting a compiler error or a runtime error? I get a compiler error if I try to build that:
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)
if(SDL_SetColorKey(bmp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmp->format, 0, 0 ,0)) == -1) //<----THERE SHOULD BE AN OPENING BRACE fprintf(stderr, "Warning: colorkey will not be used, reason: %s\n", SDL_GetError()); } //<---- 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)
SDL_Surface* bmp = SDL_LoadBMP("pong.bmp"); if( bmp == NULL ) { cerr << "Bitmap didn't load: " << SDL_GetError(); return 387; }
Last edited by MattEvans; Aug 27th, 2007 at 2:19 pm. Reason: pressed 'enter' too early..
Plato forgot the nullahedron..
heres a function that should work:
c++ Syntax (Toggle Plain Text)
SDL_Surface* getImage(string filename, int r=255, int g=0, int b=198) { SDL_Surface* loadedImage=0; SDL_Surface* optimizedImage=0; loadedImage=IMG_Load(filename.c_str()); optimizedImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); Uint32 colorkey=SDL_MapRGB(optimizedImage->format, r, g, b); SDL_SetColorKey(optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey); return optimizedImage; }
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
![]() |
Similar Threads
- i++ and ++i (C)
- sdl_ttf not working (C++)
- Technical Writer Chico, CA (Internet Marketing Job Offers)
- The Web Designer Versus the Web Developer: Who is Right? (IT Professionals' Lounge)
- Help - surf sidekick 3 is attacking! (Viruses, Spyware and other Nasties)
- Problems with a color key in C++ DirectDraw (C++)
- Sorry, Don't know how to clean HJT Log (Viruses, Spyware and other Nasties)
- Is this possible? Is there a better approach? (C++)
- calling methods. (Java)
- special keys as inputs (Game Development)
Other Threads in the Game Development Forum
- Previous Thread: J2ME Tutorial
- Next Thread: 313(Heroes)[ROME]Forum
| Thread Tools | Search this Thread |
3d advertising ai algorithm ban c++ cambridge camera censorship china competition console development engine fov fpx game gamer games gaming gauntanamo government idaho in-gameadvertisement intellectualproperty laracroft live manhunt math mathematics matrix mercenaries microsoft mmorpg modded msn naked news nintendo obama opengl palin physics pirate playstation politics projection ps3 rpg search selection software sony stephenhawking stocks studio technology terrorism tombraider uk videogame web wii world-of-warcraft xbox xbox-live xbox360






