[SDL] Re: Transparency not that transparent.
Mike Battersby
mib at roadrunner.its.unimelb.edu.au
Wed Mar 14 21:02:55 PST 2001
> In principle, one ought to be able to do it by simply setting the alpha
> value of every pixel to either fully transparent or fully opaque based
> on whether the RGB values match the color key. This seems fairly
> tricky, though, especially since both RGB and RGBA are represented
> as Uint32. I assume the color key is probably just RGB?
The easiest way to do this I found was to simply copy the Surface into
a similar-sized Surface filled with alpha = 0. This way the color-keyed
pixels won't be copied and will retain their 0 alpha value, and the
non-color-keyed pixels will get the correct colors and the default alpha.
C code something like this (may not be optimal, suggestions for improvement
gratefully accepted).
SDL_Surface *orig, *rgba;
orig = IMG_Load(filename);
rgba = SDL_CreateRGBSurface(SDL_SWSURFACE, orig->w, orig->h,
orig->format->BitsPerPixel,
orig->format->rmask, orig->format->bmask,
orig->format->gmask, orig->format->amask);
/* May need this if your image loader hasn't set the color key */
/* SDL_SetColorKey(orig, SDL_SRCCOLORKEY, colorkeyvalue); */
SDL_FillRect(rgba, NULL, SDL_MapRGBA(rgba->format, 0, 0, 0, 0));
SDL_BlitSurface(orig, NULL, rgba, NULL);
SDL_FreeSurface(orig);
This might be coloured by the fact that I needed to change the image
format anyway for OpenGL (my version of this code uses new depth and
Xmask params).
I guess you could do something like this if your Surface is already in the
right format:
foreach pixel
SDL_GetRGB(pixel, format, &r, &g, &b);
if (SDL_MapRGB(format, r, g, b) == format->colorkey)
pixel = SDL_MapRGBA(format, r, g, b, 0);
Cheers,
- Mike
More information about the SDL
mailing list