Getting transparency in a blit?

Hi all,

Given this code:

//Start code
SDL_DisplayFormat(buttons[OK_BUT_PRESSED]);

SDL_SetAlpha(buttons[OK_BUT_PRESSED],SDL_SRCCOLORKEY,
SDL_MapRGB(buttons[OK_BUT_PRESSED]->format,0,0,0));

SDL_SetColorKey(buttons[OK_BUT_PRESSED],SDL_SRCCOLORKEY,
SDL_MapRGB(buttons[OK_BUT_PRESSED]->format,0,0,0));

SDL_BlitSurface(buttons[OK_BUT_PRESSED],NULL,surface,&dstrect);

SDL_UpdateRect(surface,0,0,0,0);

//End code

Can anybody tell me WHY black pixels still do not get masked out? Both
surfaces are of the same format as far as I can determine. I. E. I want
"black" (RGB8 0,0,0) pixels NOT to be blitted from buttons[OK_BUT_PRESSED] to
surface, so I can use non-rectangular images.

I create “surface” like this:

surface = SDL_SetVideoMode(xsize,ysize,16,SDL_SWSURFACE);

and buttons[OK_BUT_PRESSED] is loaded from a .JPEG file, which has its
transparent areas set to RGB8 0,0,0 using the GIMP.

Any ideas anyone? Pointers to where I can get example code showing how to
implement totally transparent pixels in a source surface?

Thanks!–
Stefan Viljoen
F/EMS Dispatcher
Potchefstroom Public Safety Dept.
Republic of South Africa
http://home.intekom.com/rylan/

“We want you to be soldiers - deadly as long as you have one leg or one arm
and you are still alive.”

  • R. A. H. in Starship Troopers

Stefan Viljoen wrote:

Hi all,

Given this code:

//Start code
SDL_DisplayFormat(buttons[OK_BUT_PRESSED]);

SDL_SetAlpha(buttons[OK_BUT_PRESSED],SDL_SRCCOLORKEY,
SDL_MapRGB(buttons[OK_BUT_PRESSED]->format,0,0,0));

I don’t know what that line does, but it’s definitely wrong.
See the SDL_SetAlpha manpage for more info :
http://segfault.lnet.lut.fi/huru/opengl/sdl/man3/SDL_SetAlpha.3.html

SDL_SetColorKey(buttons[OK_BUT_PRESSED],SDL_SRCCOLORKEY,
SDL_MapRGB(buttons[OK_BUT_PRESSED]->format,0,0,0));

SDL_BlitSurface(buttons[OK_BUT_PRESSED],NULL,surface,&dstrect);

SDL_UpdateRect(surface,0,0,0,0);

//End code

Can anybody tell me WHY black pixels still do not get masked out? Both
surfaces are of the same format as far as I can determine. I. E. I want
"black" (RGB8 0,0,0) pixels NOT to be blitted from buttons[OK_BUT_PRESSED] to
surface, so I can use non-rectangular images.

I create “surface” like this:

surface = SDL_SetVideoMode(xsize,ysize,16,SDL_SWSURFACE);

and buttons[OK_BUT_PRESSED] is loaded from a .JPEG file, which has its
transparent areas set to RGB8 0,0,0 using the GIMP.

It might also happen that the jpeg compression loses "perfectly black"
pixels in favor of “almost black” pixels. Usually you can’t see the
difference, but SDL will.

Any ideas anyone? Pointers to where I can get example code showing how to
implement totally transparent pixels in a source surface?

It depends on what you want. Do you want both translucency and
colorkeying ? Looking at your message, it seems not. So you shouldn’t
use both SDL_SetAlpha and SDL_SetColorKey at the same time. Try removing
the SDL_SetAlpha call.

Stephane