The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_SetColorKey problem
Jorge Navarro
Guest

Hello,

Im trying to use the SDL_SetColorKey function, but sprites are drawn with
green
color.
Can someone help me?

void create_bmp (char *fichbmp, SDL_Surface **pointer_fichbmp)
{
SDL_Surface *temp = SDL_LoadBMP(fichbmp);

if (SDL_SetColorKey(temp, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
SDL_MapRGB(temp->format, 0x00,0xFF,0x00 )) < 0)
printf("\n ERROR: %s\n", SDL_GetError());

*pointer_fichbmp = SDL_ConvertSurface(temp, gScreen->format,
SDL_SWSURFACE);
// *pointer_fichbmp = SDL_DisplayFormat(temp);

SDL_FreeSurface(temp);

}

Thank you very much.
SDL_SetColorKey problem
Alex Volkov
Guest

Jorge Navarro wrote:
Quote:
Im trying to use the SDL_SetColorKey function, but sprites are
drawn with green color.
...
Quote:
if (SDL_SetColorKey(temp, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
SDL_MapRGB(temp->format, 0x00,0xFF,0x00 )) < 0)
...
Quote:
*pointer_fichbmp = SDL_ConvertSurface(temp, gScreen->format,
SDL_SWSURFACE);

You are removing the the colorkey with that last call. Try something like:
*pointer_fichbmp = SDL_ConvertSurface(temp, gScreen->format,
temp->flags | SDL_SWSURFACE);

Also, keep in mind that depending on exact surface formats involved and on
which blitter is chosen to do the conversion from your loaded format to the
gScreen format, the full-green pixels (00,ff,00) may get changed to non-full
(for example, (00,f8,00)) and the colorkey may undergo a slightly different
conversion, which will make it desynchronized with the transparent surface
pixels. Since you are already hard-coding transparent green here, it may be
a better choice for you to read an already converted pixel from a
pre-defined location in the image (i.e. from x=0, y=0) and use that pixel as
the colorkey for the converted surface.

With images loaded from files, alpha channel transparency is often a better
choice. Alternatively, colorkeys work very well with indexed (8bpp paletted)
images, and you would not need to convert indexed images to a target format
beforehand -- just blit them to the screen (?) when you need to.

-Alex.