Archived

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

Pulse_Phalanx

SDL Bmp transparency

Recommended Posts

Does anyone knows an instruction or a way to display an Bmp image using SDL but with transparent background. I searched and a found this function ,SDL_SetColorKey, but i don''t understand how it works. thx in advance!

Share this post


Link to post
Share on other sites
It''s easy.

Here is an example using SDL_img to load the picture and blue as the transparent color :

SDL_Surface picture = IMG_Load("picture.png");
SDL_SetColorKey(picture, SDL_SRCCOLORKEY, SDL_MapRGB(picture->format, 0, 0, 255));

I hope that helps.

Andy

Share this post


Link to post
Share on other sites
The other way of course is to use PNG formats that have a full alpha channel. You can then have 255 levels of "transparent". However, setting the color key is faster and not having any transparancy is MUCH faster than blitting a png image (about 2.5x)

Share this post


Link to post
Share on other sites
There is a free book available in pdf format which uses SDL. It nicely explains most of it''s features and their usage in a different language then the docs. So it may help when trying to understand the usage of something...

Programming Linux Games

granted, you may not use linux, but sdl is cross-platform, so anything discussed about it will work on windows also.


------------------------------------------------------------
// TODO: Insert clever comment here.

Share this post


Link to post
Share on other sites
quote:
Original post by C-Junkie
yes. exactly what he said. an alpha channel.


Just dont''t go crazy with the alpha blending. Using a lot of it can really kill framerates in SDL.

Share this post


Link to post
Share on other sites
As mentioned, it''s called the "alpha channel". See, you have 3 regular channels: Red, Green, and Blue. Each channels takes up 8 bits (for true color). The alpha channel is another 8 bits that describes the pixel''s transparency . Like the other color channels, it has 256 possible values. PNG images are especially cool because they, unlike BMPs, have data for that alpha channel. When you call the image loading function, it should create a blended surface for you that picks up whatever alpha data your original image has.

To learn more about alpha, just play around in any decent image editor. If you don''t have one, pick this one up for free. It''s quite nice:

http://www.gimp.org/

And just to be repetetive, don''t go alpha blending everything. Blitting a fully alpha-blended SDL surface to the screen of a different format can be anywhere from 2x-5x more expensive and you''ll just slaughter your framerate. If you game is not action oriented, you may get away with it. If you need really smooth animation, only use it where you absolutely have to.

Share this post


Link to post
Share on other sites
drowner posted a link that i am interested in however it''s not working. does anybody know how else i could get my hands on that information

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
hey leave, what do you mean by 255 levels of transparency? are you talking about alhpha blending, as in one portion of the image could be 0% transparent, another could be 1%, 2%, all the way to 255??


Remember, a while ago, I was working on some window class that had a solid border and a transluscent background, and had problems because of how my surfaces were organized?

That's alpha blending. And yes, it does a number on the framerate. A 640x480 image blitted every frame as a background ran at about 60 FPS on my PC using SDL (PIII 650 with an ATI RAGE 128; there were a few other things being blitted but they used an insignificant amount of overhead in terms of framerate approximations). A ~420x200 50% transluscent window (edit: without that background; ie, this is a comparaison between just a 640x480 bg and just a ~420x200 window) dropped to 40 FPS. It's nothing horrible, but I can imagine a number of painful slow-downs in a tile-based game with a background, tiles, sprites, and some text/numbers being blitted every frame...

Just don't abuse it and you'll be fine. SDL makes it REALLY easy to use.

[edited by - RuneLancer on May 4, 2004 2:38:53 AM]

Share this post


Link to post
Share on other sites
hey run, how do you go about telling SDL which part of the image to blit at which transparency? like, i dont understand how you got the border of the text box to be more transclucent then the inside of the box. do you do this in an image editing program like paint shop pro ? or do you do this with the SDL code ? if so, how do you tell it "only the border of the image?"

Share this post


Link to post
Share on other sites
Well, the problem I had involved the fact I was building the window in an offscreen surface, so the alpha channel was lost in the process. But typically, you''d create the window in an image-editing program such as, say, Paintshop Pro, give it an alpha channel, save it as PNG, and then let SDL do the rest.

Share this post


Link to post
Share on other sites
ahhhh... so you DO do the alpha part inside psp... i figured that had to be how people did it, couldnt imagine you telling the code each pixel individually to blend... how did you figure out how to do that in PSP anyway? maybe a small description would help me =) thanks

Share this post


Link to post
Share on other sites
Well, it''s just that... y''know... natively, the PNG format allows for the use of an alpha channel so... y''know... it''s kinda intuitive to use it for this sort of thing.

It IS possible to manually set the alpha channel. However, since it would involve per-pixel rendering (well, setting...), it''s best to do it at load-time and not at run-time unless you have very small sprites or images. Or you can set the surface''s alpha to whatever you feel''s appropriate if you only need one level of transluscency (such as say... 50%).

With PSP, there''s a way to alter the alpha channel directly (check the menus; I forgot the name). You can just "draw" your transluscency and save the image as a PNG. Since PNGs contain an alpha channel, once you load it into a surface it''ll automatically fill in the surface alpha channel with the PNG''s alpha channel and make it all nice and ready for blitting.

Of course, you need to load the PNG yourself or use a library that does that, but that shouldn''t be a problem.

Share this post


Link to post
Share on other sites