DirectDraw: ColorKeys

By: Jack Hoxley
Written: May 2000
Difficulty: Medium
Description: How to Make transparent images.

If you've been reading these tutorials in order, you'll only have come across rectangular images. These are defined, as you should know, as RECT. It is possible to create images that are still confined within a rectangle, but appear as an irregular shape. This is called transparency; it works by setting one colour on the surface to be the transparent colour; then, during Blits, this colour is not copied to the destination.

You can also change this so that it only copies the image over an existing colour on the destination surface. These are called:
Source Colour Keys
Destination Colour Keys

You will find them essential to any program, as only the simplest programs can be written to use completely rectangular sprites/images.

Source Colour Keys:
These are the ones used most often, and are very simple to use. They are also supported by almost every computer available - so you can assume that the end-users computer will support the method.

Here's The Code:

Dim ColorKey As DDCOLORKEY

'The high and low values allow you to specify a range of colours to be transparent. If you don't
'know exactly what colour will be transparent, but you know it is red, you could specify it to
'be just the top 5 reds are transparent

ColorKey.high = 0
ColorKey.low = 0
Surface.SetColorKey DDCKEY_SRCBLT, ColorKey 'The flag here is important -
'it tells it that you want a source colour key

'This next part replaces the normal BltFast call:


ddrval = backbuffer.BltFast(X, Y, surface, rRect, DDBLTFAST_SRCCOLORKEY)
'If you are using the Blt routine, just substitute the flags around...

Destination Colour Keys:
These are used only in special cases, and can be used to create a form of texturing. You could load a set of small textures into a program, and have each one cover a certain colour; ie, blue. You could create the main picture as a simple 16/32 colour picture and each texture would represent a certain colour.

The only problem is that these aren't as widely supported as source colour keys; so to be sure we'll have to enumerate the capabilities of the video card.

'The code is almost identical to before, so the comments are pretty much identical as well.

Dim HAL As DDCAPS, HEL As DDCAPS, CanDoDest as boolean
dd.GetCaps HAL, HEL
Dim lval As Long

If lval = (HAL.lSVBCKeyCaps And DDCKEYCAPS_DESTBLT) Then
Debug.Print "NO Hardware support for Destination Colour Keys" & vbCr
CanDoDest=False
Else
Debug.Print "Hardware support for Destination Colour Keys" & vbCr
CanDoDest=True
End If

'Should you want to be extra cautious, you can use this to check the source-key stuff...
'If lval = (HAL.lSVBCKeyCaps And DDCKEYCAPS_SRCBLT) Then
'Debug.Print "Hardware support for Source Colour Keys" & vbCr
'Else
'Debug.Print "NO Hardware support for Source Colour Keys" & vbCr
'End If


Dim ColorKey As DDCOLORKEY

'Previously this section specified which colour was to be transparent; but with destination keys,
'these two values specify what type of colour to copy to - ie, only copy the picture over
'colour 12345....

ColorKey.high = 0
ColorKey.low = 0
if CanDoDest=true then
Surface.SetColorKey DDCKEY_DESTBLT, ColorKey 'The flag here is important -
'it tells it that you want a destination colour key
end if

'This next part replaces the normal BltFast call:


if CanDoDest=true then
ddrval = backbuffer.BltFast(X, Y, surface, rRect, DDBLTFAST_DESTCOLORKEY)
end if
'If you are using the Blt routine, just substitute the flags around...