Using "colorkey" and "alpha" in SDL_PixelFormat

I’ve got a little problem using “colorkey” and “alpha” in the pixel format struct in a surface.

Let’s be sure first that I understand these values corret: When using colorkey on a surface, e.g. through mysurface->format->colorkey = 0xFF00FF (bright pink), would that mean, that each pixel on the (24 bit) surface, which is exactly that color, skipped, and so invisible when drawing on the screen? And would alpha mean, whether to draw the surface normal (255), 50% transparent (128), invisible (0) etc.?

Here is some of my code, but anyhow colorkey and alpha doesn’t have any affect:

Code:

#define SCREEN_DEPTH 24
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_HWSURFACE|SDL_ANYFORMAT);

SDL_Surface *bitmap1 = loadMySprite(); // this loads my 24 bit sprite
// pink pixels in my sprite should be transparent when drawing on screen, so:
bitmap1->format->colorkey = 0xFF00FF;
// when using SDL_BlitSurface() to draw my sprite on screen, all pixels are visible, even no matter whether I set the surface flas SDL_SRCCOLORKEY or not

// let’s try to make a bitmap transparent
SDL_Surface *bitmap2 = loadMyBitmap(); // this loads my 24 bit bitmap, having the SDL_SRCALPHA flag
SDL_SetAlpha(bitmap2, SDL_SRCALPHA, 64);
// SDL_BlitSurface(…); // put bitmap on screen, but it’s 100% visible

Am I doing anything wrong?

You have the idea correct. Try using:

SDL_Surface *bitmap1 = loadMySprite(); // this loads my 24 bit sprite
// pink pixels in my sprite should be transparent when drawing on screen,
so:

Uint32 nMagenta = bitmap1->format->Rmask | bitmap1->format->Bmask;
SDL_SetColorKey( bitmap1, SDL_SRCCOLORKEY, nMagenta );From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of asmodeus2
Sent: Tuesday, December 29, 2009 1:36 PM
To: sdl at lists.libsdl.org
Subject: [SDL] Using “colorkey” and “alpha” in SDL_PixelFormat

I’ve got a little problem using “colorkey” and “alpha” in the pixel format
struct in a surface.

Let’s be sure first that I understand these values corret: When using
colorkey on a surface, e.g. through mysurface->format->colorkey = 0xFF00FF
(bright pink), would that mean, that each pixel on the (24 bit) surface,
which is exactly that color, skipped, and so invisible when drawing on the
screen? And would alpha mean, whether to draw the surface normal (255), 50%
transparent (128), invisible (0) etc.?

Here is some of my code, but anyhow colorkey and alpha doesn’t have any
affect:

Code:


#define SCREEN_DEPTH 24
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT,
SCREEN_DEPTH, SDL_HWSURFACE|SDL_ANYFORMAT);

SDL_Surface *bitmap1 = loadMySprite(); // this loads my 24 bit sprite
// pink pixels in my sprite should be transparent when drawing on screen,
so:
bitmap1->format->colorkey = 0xFF00FF;
// when using SDL_BlitSurface() to draw my sprite on screen, all pixels are
visible, even no matter whether I set the surface flas SDL_SRCCOLORKEY or
not

// let’s try to make a bitmap transparent
SDL_Surface *bitmap2 = loadMyBitmap(); // this loads my 24 bit bitmap,
having the SDL_SRCALPHA flag
SDL_SetAlpha(bitmap2, SDL_SRCALPHA, 64);
// SDL_BlitSurface(…); // put bitmap on screen, but it’s 100% visible

Am I doing anything wrong?

I know I’m using the correct mask at which my colorkey should work. I found out, that it surprisingly works with the colorkey function (which I’ve found later in the reference [Rolling Eyes]), which sets the colorkey:
SDL_SetColorKey(bitmap1, SDL_SRCCOLORKEY, 0xFF00FF);
Just like in your post. So that problem is solved.

But about the alpha: There I’m already using the alpha function, but it changes nothing when drawing the bitmap. What could I have done wrong?

I don’t have any really specific answers for you because it’s been a while
since I played around with the colorkey stuff, but you should know that most
people wouldn’t consider combining usage of colorkey and alpha. The purpose
of colorkey is kind of dated when blending for every blit wasn’t practical,
or adding another channel to your sprites would consume too much memory. The
alpha channel can do everything colorkey can plus more.On Sat, Jan 2, 2010 at 10:35 AM, asmodeus2 <asmodeus at asmodeusgames.comze.com wrote:

I know I’m using the correct mask at which my colorkey should work. I
found out, that it surprisingly works with the colorkey function (which I’ve
found later in the reference [image: Rolling Eyes]), which sets the
colorkey:
SDL_SetColorKey(bitmap1, SDL_SRCCOLORKEY, 0xFF00FF);
Just like in your post. So that problem is solved.

But about the alpha: There I’m already using the alpha function, but it
changes nothing when drawing the bitmap. What could I have done wrong?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://codebad.com/

Using the color key isn’t really dated. In fact if you are using the old
SDL API, it is much faster than using surfaces with alpha. Once you move to
the new 1.3 API then alpha textures are plenty fast. For instance my game
used a number of images with Alpha and under the old API it was running at
20 FPS. Using the exact same images and ONLY changing the video API to the
new SDL 1.3 routines raised that to 400 FPS.

So if you are running SDL 1.2.x, expect any surfaces with Alpha to blit VERY
slowly. If you are using a lot of alpha based images then you should
migrate to SDL 1.3 and use the new API. I will be posting some routines to
show this once the issue with events and the new API under SDL 1.3 is fixed.From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Donny Viszneki
Sent: Saturday, January 02, 2010 2:46 PM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Using “colorkey” and “alpha” in SDL_Pixe

I don’t have any really specific answers for you because it’s been a while
since I played around with the colorkey stuff, but you should know that most
people wouldn’t consider combining usage of colorkey and alpha. The purpose
of colorkey is kind of dated when blending for every blit wasn’t practical,
or adding another channel to your sprites would consume too much memory. The
alpha channel can do everything colorkey can plus more.

On Sat, Jan 2, 2010 at 10:35 AM, asmodeus2 wrote:

I know I’m using the correct mask at which my colorkey should work. I found
out, that it surprisingly works with the colorkey function (which I’ve found
later in the reference Error! Filename not specified.), which sets the
colorkey:
SDL_SetColorKey(bitmap1, SDL_SRCCOLORKEY, 0xFF00FF);
Just like in your post. So that problem is solved.

But about the alpha: There I’m already using the alpha function, but it
changes nothing when drawing the bitmap. What could I have done wrong?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://codebad.com/

I wasn’t going to combine colorkey with alpha. I just had the problem with 24 bit bitmaps, choosing a color to be transparent, and any bitmap (24/32 bit) to fade in and out (set alpha value).