Advertisement

[SDL] 32 bit Surface: Colorkey won't work

Started by October 17, 2011 05:31 PM
2 comments, last by rip-off 13 years, 1 month ago
Short and simple: I create a 32 bit surface with alpha - channel (R8G8B8A8), and colorkeys stop working.

#include <SDL/SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(800, 480, 16, SDL_SWSURFACE);

//SDL_Surface *src = SDL_CreateRGBSurface(0, 400, 400, 32, 0xff<<16, 0xff<<8, 0xff, 0);
SDL_Surface *src = SDL_CreateRGBSurface(0, 400, 400, 32, 0xff<<24, 0xff<<16, 0xff<<8, 0xff<<0);


SDL_SetColorKey(src, SDL_SRCCOLORKEY, SDL_MapRGB(src->format, 255, 0, 128));
SDL_FillRect(src, NULL, SDL_MapRGB(src->format, 255, 0, 128));

SDL_BlitSurface(src, NULL, SDL_GetVideoSurface(), NULL);
SDL_Flip(SDL_GetVideoSurface());
SDL_Delay(2000);
}


...would I use the first, commented line to create the surface, everything works.
SDL_BlitSurface does not support simultaneous alpha channel and colour keying. One solution is to modify the colour keyed pixels to have a transparent alpha value.
Advertisement
Oha... Reffering to this:

if (source surface has SDL_SRCALPHA set) {
if (source surface has alpha channel (that is, format->Amask != 0))
blit using per-pixel alpha, ignoring any colour key
else {
if (source surface has SDL_SRCCOLORKEY set)
blit using the colour key AND the per-surface alpha value
else
blit using the per-surface alpha value
}
} else {
if (source surface has SDL_SRCCOLORKEY set)
blit using the colour key
else
ordinary opaque rectangular blit
}


I haven't set SDL_SRCALPHA(or is it set automaticly?), so following that code I should end with "blit using the colour key", shouldn't I?
You can check if SDL is setting it automatically by inspecting the value of (src->flags & SDL_SRCALPHA). This is the case on my system.

This topic is closed to new replies.

Advertisement