Archived

This topic is now archived and is closed to further replies.

sdlprorammer

Transparency in SDL

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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*"?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I laothe IM software.

You can contact me via the email addy supplied in my profile, RuneLancer@Hotmail.com

I don''t check my email too often though, so it''s probably faster to just ask me on the boards. I''m here multiple times a day.

Share this post


Link to post
Share on other sites