SDL - Alpha channel issues

Started by
50 comments, last by RuneLancer 20 years ago
Heya! I''ve been playing around with alpha transparency using SDL and I''m quite impressed by what it can do (and with so much ease, too!) However, I''ve run into a little bit of a snag. As you can see from the window, I can only seem to set alpha transparency for the full surface itself, not just a part of it (I''m trying to make the inside of the window 50% translucent). The obvious solution would be to use a PNG or other format with an alpha channel, however this raises an other issue: loading it. SDL only contains methods for BMP loading, and I don''t want to add support for a format I only plan on using in one place (as I understand it, this would mean adding support for a host of decompression algorithms and the likes; I''m not being lazy, I''m just trying to avoid something I really don''t need). All''s I need to do is, basically, set 0x00101010 to 0x80101010 (and support having 0x00FF00FF as my color key, too). What can I do?
Advertisement
dude, you need to be using SDL_image. go to www.libsdl.org and get SDL_image. it supports all kinds of file types, like PNG, BMP, and a million others. oh, and its extremely easy to use, the difference is this:

surface = Load_BMP("image.bmp")

to this

surface = IMG_Load("image.whatever");

thats the ONLY difference!
FTA, my 2D futuristic action MMORPG
First of all, know about SDL_image, which can load virtually any format you''d ever want to use. Get it here.

You can use alpha in two ways in SDL - per-pixel or per-surface. The latter you do with SDL_SetAlpha. The former you can set using for example the putpixel() function from the SDL docs, or any other function that sets pixel colors. Or access the pixels array directly.


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
SDL_image, I love you.

I will now resume my worshipping of SDL.
Just be aware that anything with per-pixel alpha (PNG graphics) will completely destroy your frame rate if you have large images or many smaller ones. So while it's very nice, use surface-level alpha or no alpha at all in cases where you don't absolutely, positively require per-pixel alpha.

For example: take your box there. You want an edge and middle. You can load two images, one for each. Blit the interior BMP at 50% surface alpha, then the seperate rim BMP with the interior blacked out (set color key for full transparancy on that color. SDL skips these pixels = faster blit). Also, any transparent pixel of fully transparent value will be skipped (i believe), so you could do a BMP interior with surface-level alpha, then a second PNG rim which blends nicely on top (just make sure the center is 100% transparent so it doesn't blit all those pixels = waste of time)


[edited by - leiavoia on April 4, 2004 4:33:00 PM]
just a quick question, using a .png format doesnt automatically reduce frame rates, correct? because im not doing any alpha blending but im using .png images instead of .bmp because the file size is smaller.....
FTA, my 2D futuristic action MMORPG
no, once loaded they are precisely the same. well, almost.

It''s sometimes worthwile to use SDL_DisplayFormat to convert ANY loaded image to the same pixel format as the screen (there''s also an alpha version)
yes, ive heard a lot about SDL_DisplayFormat()... but i dont understand what it does

"DisplayFormat to convert ANY loaded image to the same pixel format as the screen (there''s also an alpha version) "

what does that mean to convert it to the same pixel format? what is meant by pixel format? are we talking about the bpp here? as in 32 bit,24 bit, etc? if so, all my images are 24 bit and my screen is 24 bit also, so this should not be needed, correct?
FTA, my 2D futuristic action MMORPG
Well, it''s like this:

SDL_image likes to set the format of the image to what it should be to handle the data in that image. If you load a PNG then, it gives each pixel an alpha value. If you don''t want that (and i hear your argument for less disk space), you need to convert the image to the screen format (boring 3-channel BMP style) which of course negates any pixel-level alpha the original image had.

You should get into the habit of converting any surface you load, even BMPs. I never thought it of any value either until i tried and my framerate for blitting a 1024x768 background BMP just about doubled. Try it and see.

So if your screen is 32bit and and your image is not, each pixel has to get converted every time you blit. That''s a lot! So convert everything to the proper format after loading. Not everyone runs a true-color screen.
The above poster''s explanation looks long, so I didn''t read it.

The short of it is, it converts images loaded as BGR to the screen''s RGB. Also does 24bpp->16bpp or whatever, as needed.

I _think_ it will also put the surface into video memory, if you used HWSURFACE or whatever it is when you called SetVideoMode.

In short, it''ll probably make blitting a lot faster, in SOME cases.

This topic is closed to new replies.

Advertisement