Tricks of the Windows Game Programming Gurus:Advanced DirectDraw and Bitmapped Graphics

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

And once you’ve created a surface with or without a color key, you can always set it after the fact using the function `IDIRECTDRAWSURFACE4:SetColorKey()`:

    HRESULT SetColorKey(DWORD dwFlags,

                        LPDDCOLORKEY lpDDColorKey);

The valid flags are listed in Table 7.6.

Table 7.6 Valid Flags for `SetColorKey()`

Value | Description
--- | ---

`DDCKEY_COLORSPACE` | Indicates that the structure contains a color space. You must set this if you’re setting a range of colors.
`DDCKEY_SRCBLT` | Indicates that the structure specifies a color key or color space to be used as a source color key for blit operations.
`DDCKEY_DESTBLT` | Indicates that the structure specifies a color key or color space to be used as a destination color key for blit operations.
`DDCKEY_DESTOVERLAY` | Set if the structure specifies a color key or color space to be used as a destination color key for overlay operations. (Advanced)
`DDCKEY_SRCOVERLAY` | Set if the structure specifies a color key or color space to be used as a source color key for overlay operations. (Advanced)

Here’s an example:

    // assume lpdds points to a valid surface

    // set color key

    DDCOLORKEY color_key; // used to set color key

    color_key.dwColorSpaceLowValue  = low_value;

    color_key.dwColorSpaceHighValue = high_value;

    // now set the color key for source blitting, notice

    // the use of DDCKEY_SRCBLT

    lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key);

> Note: If you set a range of colors for the source key, you must add the flag `DDCKEY_COLORSPACE` in the call to `SetColorKey()`. For example:

>           lpdds->SetColorKey(DDCKEY_SRCBLT | DDCKEY_COLORSPACE,

>           &color_key);

> Otherwise, DirectDraw will collapse the range to one value.

#### Destination Color Keying

Destination color keying is great in theory, but it never seems to get used. The basic concept of destination color keying is shown in Figure 7.29. The idea is as follows: You set a color or range of colors in the destination surface that can be blitted to. In essence, you’re creating a mask of sorts. This way you can simulate windows, fences, etc.

Figure 7.29 Destination color keying.

You can set a destination color key the exact same way you did for a source. Just change a couple of the flags. For example, to set a destination color key during creation of a surface, you set the exchange `DDSD_CKSRCBLT` for `DDSD_CKDESTBLT` when setting up the `DDRAWSURFACEDESC2.dwFlags`, and of course the key values will go into `ddsd.ddckCKDestBlt` rather than `ddsd.ddckCKSrcBlt`:

    // set the color key fields

    ddsd.ddckCKDestBlt.dwColorSpaceLowValue  = low_color;

    ddsd.ddckCKDestBlt.dwColorSpaceHighValue = high_color;

If you want to set a destination color key after a surface is created, you do everything the same except during the call to `SetColorKey()`, where you must switch the `DDCKEY_SRCBLT` flag to `DDCKEY_DESTBLT`, like this:

    lpdds->SetColorKey(DDCKEY_DESTBLT, &color_key);

> Warning: Destination color keying is currently only available in the HAL (Hardware Abstraction Layer), not the HEL. Hence, if there isn’t hardware support for destination color keying, it won’t work. This will probably change for future versions of DirectX.

Finally, there are two more types of keys: source overlays and destination overlays. They’re useless for your purposes, but they come in handy for video processing. If you’re interested, take a look at the DirectX SDK.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.