Transparency in SDL

Started by
9 comments, last by sdlprorammer 19 years, 10 months ago
First off, i ''ve read the docs.
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b)); 
i don''t understand wht this function is supposed to do. How is transparency related with the RGB value, and what SRCCOLORKEY does??? thanks in advance
Advertisement
You have to set which RGB color you want to be transparent.
SDL_SRCCOLORKEY - says that we want to use a colorkey when blitting with SDL_BlitSurface and
SDL_MapRGB(temp->format, r, g, b) describes what color should be the colorkey.

What this means, is that if you set the color (r,g,b) to be the colorkey, this color will be transparent when blitting, i.e. those pixels in the source image that are of this color won''t affect the destination image.
This can be used if you want to draw, for example, a character on a background. If you don''t use a colorkey (and don''t use alpha blending), the character will always block out a rectangular area on the screen even if the character is, say, round.
Just use a color that isn''t otherwise used in the character image as the "transparent color" - i.e. the colorkey - together with this function to make parts of the image transparent.
Ok thanks!
But 1)what if the color i choose with RGB doesn''t exist in one of the pixels of the image?
and 2) you said "and don''t use alpha blending" . What do you mean???
thanks..
quote:Original post by sdlprorammer
But 1)what if the color i choose with RGB doesn''t exist in one of the pixels of the image?

Then setting the colorkey won''t have any visible effect, so there is really no point in doing that..
quote:Original post by sdlprorammer
and 2) you said "and don''t use alpha blending" . What do you mean???
thanks..

If the image has an alpha channel, you can use this to get more advanced effects than just transparency. For example, you can have pixels that are partially transparent (like colored glass.)
If you need to use this, just google for more info (and check the SDL docs.)
Here''s a spritesheet.



Now, obviously, when you''re going to use these, you don''t want that icky pink (R 255 G 000 B 255) to show up. You want that to be left out. This is why you use what''s called "color keying:" setting a single color to be fully transparent. Basically, this tells SDL that, when it encounters the color you specify, not to draw it onscreen.

This is vital for drawing sprites.
Ok many MANY thanks!!!

But i have another 2 questions:

1) I thought that this:
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b));

should be equivalent with this:
temp->format->colorkey = 0xrrggbb; //in hexademical - you get what i mean right?
since SetColorKey sets the variable "colorkey".

But with a test i made, the background of the picture is shown after i replace the previous function call Why?

2) i understand what Tomas said about advanced effects, but what''s that -> "If the image has an *alpha channel*"?
quote:Original post by sdlprorammer
1) I thought that this:
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, r, g, b));

should be equivalent with this:
temp->format->colorkey = 0xrrggbb; //in hexademical - you get what i mean right?
since SetColorKey sets the variable "colorkey".

But with a test i made, the background of the picture is shown after i replace the previous function call Why?

Several reasons:
* The format field in SDL_Surface is marked read-only. Do you really think you should be writing to it? Use the supplied functions instead.
* Also, it''s definitely not equivalent. SDL_SetColorKey also changes the flags for the surface. If the SDL_SRCCOLORKEY flag is not set, changing the colorkey won''t have any effect.
* Finally, the surface might not be stored in that format. It doesn''t have to be rrggbb, it might as well be bbggrr (or whatever.) That''s what the SDL_MapRGB function is used for.

quote:Original post by sdlprorammer
2) i understand what Tomas said about advanced effects, but what''s that -> "If the image has an *alpha channel*"?

A picture can have several channels. Often it''s one red, one green and one blue. You can also have an alpha channel which can be used for transparency effects.
Ok many many many many thanks! i do get it
hey rune, whats your email/icq/messenger/aol sn ? just wanted to hit you up about something.... mines graveyardfilla at hotmail dot com , ICQ 82985827, aol gfilla69 ...
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement