Tricks of the Windows Game Programming Gurus:Advanced Windows Programming

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

#### RGB and Palletized Modes

There are two ways to represent color on a video display: directly or indirectly. Direct color modes, or RGB modes, represent each pixel on the screen with either 16, 24, or 32 bits that represent the red, green, and blue components of the color (see Figure 3.15). This is possible due to the additive nature of the primary colors red, green, and blue.

Referring to Figure 3.15, you can see that for each possible color depth (16, 24, 32), there are a number of bits assigned to each color channel. Of course, with 16-bit and 32-bit color, these numbers aren’t evenly divisible by 3; therefore, there might be an unequal amount of one of the color channels. For example, with 16-bit color modes, there are three different RGB encodings you might find:

  • RGB (6.5.5)—Six bits of red, five bits of green, and five bits of blue.
  • RGB (1.5.5.5)—One bit alpha and five bits each of red, green, and blue. Alpha is a transparency control.
  • RGB (5.6.5)—Five bits of red, six bits of green, and five bits of blue. This is the most common, in my experience.

Figure 3.15 Color encoding for RGB modes.

The 24-bit mode is almost always eight bits per channel. However, the 32-bit mode can be weird, and in most cases there are eight bits for alpha (transparency) and eight bits each for the red, green, and blue channels.

Basically, RGB modes give you control over the exact red, green, and blue components of each pixel on the screen. Palettized modes work on a principle called indirection. When there are only eight bits per pixel, you could decide to allocate the three bits for red, three bits for green, and maybe two bits for blue or some combination thereof. However, this would leave you with only a few shades of each of the primary colors, and that wouldn’t be very exciting. Instead, 8-bit modes use a palette.

As shown in Figure 3.16, a palette is a table that has 256 entries, one for each possible value of a single byte—0 to 255. However, each of these entries is really composed of three 8-bit entries of red, green, and blue. In essence, it’s a full RGB 24-bit descriptor. The color lookup table (CLUT) works like this: When a pixel in an 8-bit color mode is read from the screen, say value 26, the 26 is used as an index into the color table. Then the 24-bit RGB value for color descriptor index 26 is used to drive the red, green, and blue channels for the actual color that is sent to the display. In this way, you can have just 256 colors on the screen at once, but they can be from among 16.7 million colors or 24-bit RGB values. Figure 3.16 illustrates the lookup process.

Figure 3.16 How 256-color palettized modes work.

We are getting a little ahead of ourselves with all this color stuff, but I want to let you chew on the concepts a bit so that when you see them again during the DirectDraw discussion, it won’t be for the first time. In fact, color is such a complex problem to work with in normal GDI-based Windows graphics that Windows has abstracted color to a 24-bit model no matter what. That way you don’t have to worry about the details of color depth and such when you’re programming. Of course, you will get better results if you do worry about them, but you don’t have to.

#### Basic Text Printing

Windows has one of the most complex and robust text-rendering systems of any operating system I have ever seen. Of course, for most game programmers, printing the score is all we want to do, but it’s nice to have nonetheless.

In reality, the GDI text engine is usually too slow to print text in a real-time game, so in the end you will need to design our own DirectX-based text engine. For now, though, let’s learn how to print text with GDI. At the very least, it will help with debugging and output with demos.

There are two popular functions for printing text: `TextOut()` and `DrawText()`. `TextOut()` is the “ghetto ride” version of text output, and `DrawText()` is the Lexus. I usually use `TextOut()` because it’s faster and I don’t need all the bells and whistles of `DrawText()`, but we’ll take a look at both. Here are their prototypes:

    BOOL TextOut(HDC hdc,  // handle of device context

         int nXStart,      // x-coordinate of starting position

         int nYStart,      // y-coordinate of starting position

         LPCTSTR lpString, // address of string

         int cbString);    // number of characters in string

    int DrawText( HDC hDC, // handle to device context

         LPCTSTR lpString, // pointer to string to draw

         int nCount,       // string length, in characters

         LPRECT lpRect,    // ptr to bounding RECT

         UINT uFormat);    // text-drawing flags

Most of the parameters are self-explanatory. For `TextOut()`, you simply send the device context, the x,y coordinates to print to, and the ASCII string, along with the length of the string in bytes. `DrawText()`, on the other hand, is a little more complex. Because it does word wrapping and formatting, it takes a different approach to printing via a rendering `RECT`. Thus, `DrawText()` doesn’t take an x,y for the place to start printing; instead, it takes a `RECT` that defines where the printing will take place within the window (see Figure 3.17). Along with the `RECT` of where to print it, you send some flags that describe how to print it (such as left-justified). Please refer to the Win32 documentation for all the flags, because there are a ton of them. I’ll just stick to `DT_LEFT`, which is the most intuitive and justifies all text to the left.

Figure 3.17 The drawing `RECT` of `DrawText()`.

The only problem with both calls is that there’s no mention of color. Hmmmm. That’s almost as strange as Boogie Nights, but who cares? Anyway, thankfully there is a way to set both the foreground color of the text and the background color behind it, in addition to the transparency mode of the text.

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.