moved, but I don't know if it will help.
Ancient Dragon
Retired & Loving It
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
Are you getting a compiler error or a runtime error? I get a compiler error if I try to build that:
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/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlpixelformat.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:
SDL_Surface* bmp = SDL_LoadBMP("pong.bmp");
if( bmp == NULL )
{
cerr << "Bitmap didn't load: " << SDL_GetError();
return 387;
}
MattEvans
Veteran Poster
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
heres a function that should work:
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;
}
Sturm
Veteran Poster
1,079 posts since Jan 2007
Reputation Points: 343
Solved Threads: 24