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

#### 16-Bit High-Color Mode

Referring to Figure 7.1, there are a number of possible bit encodings for 16-bit modes:

Alpha.5.5.5—This mode uses a single bit at position D15 to represent a possible Alpha component (transparency), and the remaining 15 bits are equally distributed with five bits for red, five bits for green, and five bits for blue. This makes a total of 25 = 32 shades for each color and a palette of 32×32×32 = 32,768 colors.

X.5.5.5—This mode is similar to the Alpha.5.5.5 mode, except the MSB (most significant bit) is unused and can be anything. The color range is still 32 shades of each primary color (red, green, and blue), with a total of 32×32×32 = 32,768 colors.

5.6.5—This is the most common mode and uses all 16 bits of the `WORD` to define the color. The format is, of course, five bits for red, six bits for green, and five bits for blue, for a total of 32×64×32 = 65536 color. Now, you may ask, “Why six bits for green?” Well, my little leprechaun, the answer is that human eyes are more sensitive to green, and therefore the increased range for green is the most logical choice of the three primaries.

Now that you know the RGB bit-encoding formats, the question is how to build them up. You accomplish this task with simple bit shifting and masking operations, as shown in the following macros:

    // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)

    #define _RGB16BIT555(r,g,b) ((b%32) + ((g%32) << 5) + ((r%32) << 10))

    // this builds a 16 bit color value in 5.6.5 format (green dominate mode)

    #define _RGB16BIT565(r,g,b) ((b%32) + ((g%64) << 6) + ((r%32) << 11))

You’ll notice from the macros and Figure 7.2 that the red bits are located in the high-order bits of the color `WORD`, the green bits are in the middle bits, and the blue bits are located in the low-order bits of the color `WORD`. This may seem backwards because PCs are little-endian and place data in low-to-high order, but in this case the bits are in big-endian format, which is much better because they follow RGB order from MSB to LSB.

Figure 7.2 Color `WORDS` are big-endian

> Warning: Before you build a quick demo of 16-bit mode, there’s one more little detail that I must address—how on Earth do you detect if the video mode is 5.5.5 or 5.6.5? This is important because it’s not under your control. You can tell DirectDraw to create a 16-bit mode, but the bit encoding is up to the hardware. You must know this detail because the green channel will be all jacked up if you don’t take it into consideration! What you need to know is the pixel format.

#### Getting the Pixel Format

To figure out the pixel format of any surface, all you need to do is call the function `IDIRECTDRAWSURFACE4:GetPixelFormat()`, shown here:

    HRESULT GetPixelFormat(LPDDPIXELFORMAT lpDDPixelFormat);

You already saw the `DDPIXELFORMAT` structure in the previous chapter, but the fields you’re interested in are

    DWORD dwSize;    // the size of the structure, must be set by you

    DWORD dwFlags;   // flags describing the surface, refer to Table 7.1

    DWORD dwRGBBitCount; // number of bits for Red, Green, and Blue

The `dwSize` field must be set before you make the call to the size of a `DDPIXELFORMAT` structure. After the call, both the `dwFlags` field and the `dwRGBBitCount` fields will be valid and contain the informational flags, along with the number of RGB bits for the surface in question. Table 7.1 lists a subset of the possible flags contained in `dwFlags`.

Table 7.1 Valid Flags for `DDPIXELFORMAT.dwFlags`

Value | Description
--- | ---

`DDPF_ALPHA` | The pixel format describes an alpha-only surface.
`DDPF_ALPHAPIXELS` | The surface has alpha channel information in the pixel format.
`DDPF_LUMINANCE` | The pixel format describes a luminance-only or luminance-alpha surface.
`DDPF_PALETTEINDEXED1` | The surface is 1-bit color indexed.
`DDPF_PALETTEINDEXED2` | The surface is 2-bit color indexed.
`DDPF_PALETTEINDEXED4` | The surface is 4-bit color indexed.
`DDPF_PALETTEINDEXED8` | The surface is 8-bit color indexed. Most common.
`DDPF_PALETTEINDEXEDTO8` | The surface is 1-, 2-, or 4-bit color indexed to an 8-bit palette.
`DDPF_RGB` | The RGB data in the pixel format structure is valid.
`DDPF_ZBUFFER` | The pixel format describes a z-buffer surface.
`DDPF_ZPIXELS` | The surface contains z information in the pixels.

Note that there are a lot more flags especially for D3D-related properties. Please refer to the DirectX SDK for more information.

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.