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

#### Color Keys

Color keys are a bit hard to explain, probably because of their naming convention under DirectDraw. Let me give it a try. When you’re performing bitmap operations, in most cases you’re blitting bitmap objects that are contained in rectangular cells. However, when you draw the bitmap of a little creature, you usually don’t want to copy the contents of the entire cell. You want only to copy the bits that relate to the creature, so you need to select a color (or colors) as transparent. Figure 7.28 shows a transparent blit vs. a nontransparent blit. I’ve discussed this before, and you’ve even implemented it in your software blitter for the exercise.

Figure 7.28 Transparent blit (top) versus nontransparent blit (bottom).

DirectDraw has a much more sophisticated color keying system than just selecting a simple transparent color. It can do much more than just perform blits with basic transparency. Let’s take a quick look at the different types of color keys, and then I’ll show you how to set up color keying for the type of operation you’re interested in.

#### Source Color Keying

Source color keying is the color keying that you want to use and is the easiest to understand. Basically, you select a single color index (in 256-color modes) or a range of RGB color values that will act as transparent for your source image. Then, when you blit the source to the destination, the pixels that have the same value as the transparent color(s) will not be copied. Figure 7.14 shows this process. You can set the color key for a surface while creating the surface, or do it after the fact with `IDIRECTDRAWSURFACE4::SetColorKey()`. I’ll show you both methods in a moment, but first look at the data structure that holds the color key. It’s called `DDCOLORKEY`:

    typedef struct _DDCOLORKEY

            {

            DWORD dwColorSpaceLowValue;  // low value (inclusive)

            DWORD dwColorSpaceHighValue; // high value (inclusive)

            } DDCOLORKEY,FAR* LPDDCOLORKEY;

The low- and high-color key values are a bit tricky, so listen up. If you’re using 8-bit surfaces, the values should be color indices. If you’re using 16-, 24-, or 32-bit surfaces, you actually use the RGB-encoded `WORD`s for the particular surface format as the values to store in the low- and high-color keywords. For example, let’s say you’re running in an 8-bit mode and you want color index 0 to be transparent. Here’s how you would set up the color key:

    DDCOLORKEY key;

    key.dwColorSpaceLowValue  = 0;

    key.dwColorSpaceHighValue = 0;

And if you wanted the range from color index 10-20 (inclusive) to be transparent:

    key.dwColorSpaceLowValue  = 10;

    key.dwColorSpaceHighValue = 20;

Next, let’s say you’re running in a 16-bit 5.6.5 mode and want pure blue to be transparent:

    key.dwColorSpaceLowValue  = _RGB16BIT565(0,0,32);

    key.dwColorSpaceHighValue = _RGB16BIT565(0,0,32);

Similarly, let’s say you want the range of colors from black to half-intensity red to be transparent in the same 16-bit mode:

    key.dwColorSpaceLowValue  = _RGB16BIT565(0,0,0);

    key.dwColorSpaceHighValue = _RGB16BIT565(16,0,0);

Get the idea? Now let’s take a look at how to set a DirectDraw surface’s color key during creation. All you need to do is add the flag `DDSD_CKSRCBLT` (other valid settings shown in Table 7.5) to the dwFlags `WORD` of the surface descriptor, and then assign the low- and high-color keywords in the `DDSURFACEDESC2.ddckCKSrcBlt`, `member.dwColorSpaceLowValue`, and `DDSURFACEDESC2`.`ddckCKSrcBlt. dwColorSpaceHighValue` fields (there are also members for destination and overlay color key information).

Table 7.5 Color Key Surface Flags

Value | Description
--- | ---

`DDSD_CKSRCBLT` | Indicates that the `ddckCKSrcBlt` member of the `DDSURFACEDESC2` is valid and contains color key information for source color keying.
`DDSD_CKDESTBLT` | Indicates that the `ddckCKDestBlt` member of the `DDSURFACEDESC2` is valid and contains color key information for destination color keying.
`DDSD_CKDESTOVERLAY` | Indicates that the `ddckCKDestOverlay` member of the `DDSURFACEDESC2` is valid and contains color key information for destination overlay color keying.
`DDSD_CKSRCBLT` | Indicates that the `ddckCKSrcBlt` member of the `DSURFACEDESC2` is valid and contains color key information for source color keying.
`DDSD_CKSRCOVERLAY` | Indicates that the `ddckCKSrcOverlay` member of the `DSURFACEDESC2` is valid and contains color key information for source overlay color keying.

Here’s an example:

    DDSURFACEDESC2 ddsd;         // working description

    LPDIRECTDRAWSURFACE4 lpdds;  // temporary surface

    // initialize structure

    DDRAW_INIT_STRUCT(ddsd);

    // set to access caps, width, and height

    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT;

    // set dimensions of the new bitmap surface

    ddsd.dwWidth  =  width;

    ddsd.dwHeight =  height;

    // set surface to offscreen plain

    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags;

    // set the color key fields

    ddsd.ddckCKSrcBlt.dwColorSpaceLowValue  = low_color;

    ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = high_color;

    // create the surface

    if (FAILED(lpdd->CreateSurface(&ddsd,&lpdds,NULL)))

       return(NULL);

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.