From GPWiki
Jump to: navigation, search

Most of the time when you display a bitmap, you'd like the have some part of it appear transparent. How else are you going to be able to see Jupiter when you fly your Vegan starship over it? It would look like a rectangle with a ship in it flying over Jupiter without transparency effects. What you need to do is set a certain colour to be the transparent colour, thus removing the unsightly black rectangle, and leaving only the colourful ship behind. I tend to use black, so that's what we'll do here.

    CKey.low = vbBlack
    CKey.high = vbBlack
    Sprite.SetColorKey DDCKEY_SRCBLT, CKey

CKey is defined as a DirectDraw Color Key. The CKey contains a low value and a high value. This enables us to define a range of colours as transparent (if we want). For our purposes a single colour will do fine: Black. We set CKey.low and CKey.high to vbBlack. All we have to do after that is set the Sprite.SetColorKey method using the color key we've created. Also the flag DDCKEY_SRCBLT indicates that we want to apply this key to the source bitmap when we blt it. This will make more sense in a second:

BackBuffer.Blt DestRect, Sprite, SrcRect, DDBLT_KEYSRC Or DDBLT_WAIT

Now when we blt the surface we have to specify the DDBLT_KEYSRC flag to ensure that the color key of the source (the Sprite surface) is considered transparent during the blt. That's all there is to it!

Have a look at this sample source code to see transparency in action.