I want to use 32 bit bitmaps with a transparent color in my game. I’ve
got questions on several issues:
- how is transparency represented in a 32 bit bitmap ?
Either using 8 bits as an alpha channel, or using a specific bit pattern
as color key, just as for any other pixel format.
I mean can I use a specific pixel color to do this ?
Yes. Use SDL_MapRGB() to translate from Uint8 R,G,B into whatever pixel
format you’re using.
I don’t want to use alpha
transparency because the screen pixel depth might not support alpha. or
is there a work-around this ?
Alpha has little to do with screen pixel format - it’s a "cross fade"
operation between the target pixel and the source pixel, controlled by
the source surface alpha, or the alpha field of the source pixel.
However, it can be a performance issue regardless of target pixel
format, unless you’re using hardware accelerated OpenGL. (SDL 2D alpha
blending is done in software, as most 2D APIs lack alpha blending
- at the start of the game, 32 bit bitmaps should be converted to
screen pixel depth (8,16 or 24 bits), right ?
then how to make sure
that the transparent ‘pixel’ is indeed transparent ?
You should set up any colorkeying, alpha or whatever before you convert
the surfaces into screen format - or just use SDL_MapRGB() with the pixel
format struct of the surface you’re addressing, as always.
I think that SDL_MapRGB() translates your RGB values exactly the same
way as does the SDL surface conversion code, which would mean that
everything works even if conversion loses bits.
You have to be careful about “real” colors that are too close to the
colorkey, though. They may end up with the same pixel value as the
colorkey pixels - which is a perfect demonstration of why you should deal
with this stuff before converting anything to an “unknown” format, such
as the screen format.
(In the Kobo Deluxe/Project Spitfire engine, I have a filter plugin
system, and to keep things simple, the first filter just converts
everything into 32 bit RGBA, so the actual filters have a fixed format to
deal with, regardless of file formats. As an extra bonus, the
colorkey->alpha translation feature has a “fuzziness factor”, to deal
with the rounding errors that GIMP and PhotoShop introduce when
converting between pallettized and RGB modes.)
and how to
construct a common optimized palette from all the 32 bit bitmaps?
You don’t. Only 8 bit surfaces have palettes.
how to convert these 32 bit bitmaps into the screen pixel depth so that
the color index info corresponds to the common palette ?
This isn’t applicable to 32 bit surfaces; see above.
(Right, it applies to some extent if your screen format is 8 bit - but do
you really want to optimize your code specifically for that, if you’re
using 32 bit image data?)
Please advise on any other issue regarding using bitmaps with a single
transparent color in a game…This is my first game.
First, pick a key that’s as different as possible from the colors used in
your game. (That gives a “fuzziness factor” like the one in my engine a
better chance of working properly if you end up really needing it.)
Second, note that if you’re working with PhotoShop or GIMP, these
applications have two issues:
1) they can hardly do anything in indexed color mode, so you
have to convert to RGB to do anything interesting, and
2) when converting between indexed color and RGB modes, both
introduce some kind of rounding errors that will break the
"exact match" colorkey system used by SDL. (For example, the
bright red colorkey of the Kobo Deluxe graphics turns from
0xff0000 to 0xfe0000 after an indexed->RGB->indexed conversion.
Older PhotoShop was *totally* broken, and ruined the whole
image, just not the palette!)
Finally, think again about alpha blending. I’m using it for antialiazing
and some special effects in Kobo Deluxe, all over the place. (The logo,
the rounded frame and the font are always using it.) In fact, it’s
applied to every single sprite in 640x480+ resolutions, as a result of
the interpolated image scaling.
Disabling it doesn’t make a significant difference in performance, as
only the pixels that have alpha values other than 0 or 255 are affected.
The RLE encoding of SDL encodes surfaces so that “empty” pixels are
skipped, opaque pixels are copied, and only alpha blended pixels are
//David Olofson — Programmer, Reologica Instruments AB
.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 04 October 2001 01:53, Meetul Kinarivala wrote: