Advertisement Jump to content
Sign in to follow this  

[SDL] Colorkey question

This topic is 3994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm learning using lazyfoo's tutorials. In the colorkeys tutorial he does this to map the colorkey.
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
Is that only for the colour he was using? I tried to use it with another image and it didn't work. If not, how do I know what is the colorkey of the image I'm using?

Share this post

Link to post
Share on other sites
The colorkey of a surface is stored as an unsigned 32 bit integer. The easiest way to set this is using SDL_MapRGB or SDL_MapRGBA. 0xff is equivalent to 255 in hex, so in the example you've given the colour will be a kind of turquoise I guess (Red:0, Green:255, Blue:255). If you want a different color then change the values.

The code you've posted doesn't actually set the colorkey, just gets the Uint32 value you need. Call SDL_SetColorKey to actually do this:

SDL_SetColorKey(optimisedImage, SDL_SRCCOLORKEY, colorkey);

The colorkey is stored in the surface's SDL_PixelFormat struct, and can be accessed like this:

Uint32 colorkey = optimisedImage->format->colorkey;

If you then want the separate rgb components the easiest way to get them is using SDL_GetRGB or SDL_GetRGBA.

// continued from above...
Uint8 red, green, blue, alpha;
SDL_GetRGBA(colorkey, optimisedImage->format, &red, &green, &blue, &alpha);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!