Sdl - Blitting A Surface With Alpha Blending And Color Key


Alex.

Retired
Joined
Aug 24, 2005
Messages
4,616
Hi, a while ago I came upon this issue and ignored it, but now I really need to know how to get to the bottom of this:

CODE

// assume a 16-bit "sprite" surface, with a loaded image
// the mask colour is purple (255, 78, 207)
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, SDL_MapRGB(sprite->format, 255, 78, 207));

// create a surface
s = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, sprite->format, 0, 0, 0, 0);

// blit the sprite on it
SDL_BlitSurface(sprite, NULL, s, NULL);

// set the surface's alpha to 50%
SDL_SetAlpha(s, SDL_SRCALPHA, 128);

// blit the surface to the screen
SDL_BlitSurface(s, NULL, screen, NULL);



The problem is that the mask colour is also drawn to the screen, at the certain alpha I set the whole surface to. I know there has to be a way around this, any help would be greatly appreciated :) Thanks!

Edit: I got it to work, sorry for the idiotic question :X (solution posted below)
 
I figured it out, I overcomplicated things:

CODE

// assume "screen" surface, the framebuffer
// assume "sprite" surface with image loaded

SDL_SetAlpha(sprite, SDL_SRCALPHA, 128);

SDL_BlitSurface(sprite, NULL, screen, NULL);

SDL_SetAlpha(sprite, SDL_SRCALPHA, 255);



And it works perfectly, and faster than creating a new surface everytime :X
 
No, it doesn't, SDL_SetAlpha(sprite, SDL_SRCALPHA, x) and then SDL_SetAlpha(sprite, SDL_SRCALPHA, SDL_ALPHA_OPAQUE) to set the alpha back to normal works ace :)
 
I have done the very same before, the GP2X doesn't handle huge amounts of alpha blending at once too well though (which is why the controls are laggy in the Burokku demo).
 
Ryo said:
I really don't understand how setting the alpha to SDL_ALPHA_OPAQUE after the blit could do the trick, but it's it's nice to know...
No no, I set the alpha to SDL_ALPHA_OPAQUE after the blit in order to be able to blit other sprites form the same spritesheet at regular alpha. Sorry for the confusion.
 
Last edited by a moderator:
Back
Top