I just spent absolutely ages trying to work out why my color key was
not working in glSDL, and eventually discovered that when creating an
SDL color with SDL_MapRGB the alpha component of the color is set to
0, whilst when glSDL is checking which pixels match the color key it
is checking pixels which can have an alpha component of 255.
My own little bit of code to make it work is below, though I don’t
normally faff with low level bit shifting so I won’t be surprised if
there is a mistake:
static bool rgb_match(Uint8* src, Uint32 color) {
Uint8 r, g, b;
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
b = (color >> 16) & 0xff;
g = (color >> 8) & 0xff;
r = color & 0xff;
} else {
b = color & 0xff;
g = (color >> 8) & 0xff;
r = (color >> 16) & 0xff;
}
if (src[0] == b
&& src[1] == g
&& src[2] == r)
return true;
return false;
}
static void key2alpha(SDL_Surface *surface) {
if(SDL_LockSurface(surface) < 0)
return;
Uint32 ckey = surface->format->colorkey;
int bytes_per_pixel = surface->format->BytesPerPixel;
Uint8* casted_buffer = reinterpret_cast<Uint8*>(surface->pixels);
for (int i = 0; i != surface->pitch * surface->h / bytes_per_pixel; ++i) {
if (rgb_match(casted_buffer, surface->format->colorkey)) {
casted_buffer[0] = 0;
casted_buffer[1] = 0;
casted_buffer[2] = 0;
casted_buffer[3] = 0;
}
casted_buffer += bytes_per_pixel;
}
SDL_UnlockSurface(surface);
}
James