PDA

View Full Version : SDL - setting opacity



Ixy
27-01-2009, 07:08 PM
I have a BMP file which is supposed to be a player sprite.
The thing is that, since BMP doesn't support transparency, I have to go another way to achieve it.

I've read a tutorial on SetColorKey and SetAlpha, but I just don't get it - how do I define the color which should be left out? :?

I'd appreciate an explanation, or better yet - a piece of code with that in it.

paul_nicholls
28-01-2009, 03:44 AM
I have a BMP file which is supposed to be a player sprite.
The thing is that, since BMP doesn't support transparency, I have to go another way to achieve it.

I've read a tutorial on SetColorKey and SetAlpha, but I just don't get it - how do I define the color which should be left out? :?

I'd appreciate an explanation, or better yet - a piece of code with that in it.

Here is some code that may help - I use it in my SDL sprite unit:


Procedure TSDLSprite.SetAlpha(Const AValue : Byte);
Begin
If Not Assigned(FSurface) Then Exit;
Falpha := AValue;
If Falpha <> 255 Then
SDL_SetAlpha&#40;FSurface,SDL_SRCALPHA,Falpha&#41;
Else
SDL_SetAlpha&#40;FSurface,0,255&#41;;
End;
&#123;................................................. .............................&#125;

&#123;................................................. .............................&#125;
Procedure TSDLSprite.SetTransparent&#40;Const AValue &#58; Boolean&#41;;
Begin
If Not Assigned&#40;FSurface&#41; Then Exit;
FTransparent &#58;= AValue;
If FTransparent Then
SDL_SetColorKey&#40;FSurface,SDL_SRCCOLORKEY,SDL_MapRG B&#40;FSurface^.format,Fr,Fg,Fb&#41;&#41;
Else
SDL_SetColorKey&#40;FSurface,0,0&#41;;
End;
&#123;................................................. .............................&#125;

&#123;................................................. .............................&#125;
Procedure TSDLSprite.SetTransparentColour&#40;Const r,g,b &#58; Byte&#41;;
Begin
Fr &#58;= r;
Fg &#58;= g;
Fb &#58;= b;
End;
&#123;................................................. .............................&#125;

&#123;................................................. .............................&#125;


FSurface is an internal PSDL_Surface stored in each sprite containing the picture data.

Fr, Fg, Fb are the R, G, and B (0..255) byte values respectively of the transparent colour to be used for the colour-keying (transparency)

Falpha is the byte value of how much the sprite is opaque (0 - not there, 255 is totally opaque)

I hope this helps :-)
cheers,
Paul