Tricks of the Windows Game Programming Gurus:First Contact: DirectDraw

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

The only interesting parameter of the bunch is, of course, the flags parameter `dwFlags`. Let’s take a more in-depth look at what your options are. Refer to Table 6.4 for the possible values you can logically `OR` to create the flags `WORD`.

Table 6.4 Control Flags for `CreatePalette()`

Value | Description
--- | ---

`DDPCAPS_1BIT` | 1-bit color. There are two entries in the color table.
`DDPCAPS_2BIT` | 2-bit color. There are four entries in the color table.
`DDPCAPS_4BIT` | 4-bit color. There are 16 entries in the color table.
`DDPCAPS_8BIT` | 8-bit color. The most common. There are 256 entries in the color table.
`DDPCAPS_8BITENTRIES` | This is for an advanced feature referred to as indexed palettes and is used for 1-, 2-, and 4-bit palettes. Just say no.
`DDPCAPS_ALPHA` | Indicates that the `peFlags` member of the associated `PALETTEENTRY` structure is to be interpreted as a single 8-bit alpha value controlling the transparency. A palette created with this flag can only be attached to a D3D texture surface created with the `DDSCAPS_TEXTURE` capability flag. Again, this is advanced and for big G’s.
`DDPCAPS_ALLOW256` | Indicates that this palette can have all 256 entries defined. Normally, entries 0 and 255 are reserved for black and white, respectively, and on some systems like NT you can’t write to these entries under any circumstances. However, in most cases you don’t need this flag because 0 is usually black anyway, and most palettes can live with entry 255 being white. It’s up to you.
`DDPCAPS_INITIALIZE` | Initialize this palette with the colors in the color array passed at `lpDDColorArray`. This is used to enable the palette data sent to be downloaded into the hardware palette.
`DDPCAPS_PRIMARYSURFACE` | This palette is attached to the primary surface. Changing this palette’s color table immediately affects the display unless `DDPSETPAL_VSYNC` is specified and supported.
`DDPCAPS_VSYNC` | Forces palette updates to be performed only during the vertical blank period. This minimizes color anomalies and sparkling. Not fully supported yet, though.

A lot of confusing control words, if you ask me. Basically, you only need to work with 8-bit palettes, so the control flags you need to `OR` together are

    DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE

And if you don’t care about setting color entries 0 and 256, you can omit `DDPCAPS_ALLOW256`. Furthermore, if you’re not sending a palette during the `CreatePalette()` call, you can omit `DDPCAPS_INITIALIZE`.

Sucking all that down into your brain, here’s how you would create a palette object with your random palette:

    LPDIRECTDRAWPALETTE lpddpal = NULL; // palette interface

    if (FAILED(lpdd4->CreatePalette(DDPCAPS_8BIT |

                                    DDPCAPS_ALLOW256 |

                                    DDPCAPS_INITIALIZE,

                                    palette,

                                    &lpddpal,

                                    NULL)))

       {

       // error

       } // end if

If the function call is successful, `lpddpal` will return with a valid `IDirectDrawPalette` interface. Also, the hardware color palette will instantly be updated with the sent palette, which in this case is a collection of 256 random colors.

Normally, at this point I would drop a demo on you, but unfortunately we’re at one of those “chicken and the egg” points in DirectDraw. That is, you can’t see the colors until you can draw on the screen. So that’s what’s next!

### Building a Display Surface

As you know, the image displayed on the screen is nothing more than a matrix of colored pixels represented in memory for some format, either palletized or RGB. In either case, to make anything happen, you need to know how to draw into this memory. However, under DirectDraw the designers decided to abstract the concept of video memory just a little bit so that no matter how weird the video card in your system (or someone else’s) is, accessing the video surfaces will be the same for you (the programmer’s point of view). Thus, DirectDraw supports what are called surfaces.

Referring to Figure 6.7, surfaces are rectangular regions of memory that can hold bitmap data. Furthermore, there are two kinds of surfaces: primary and secondary.

Figure 6.7 Surfaces can be any size.

A primary surface directly corresponds to the actual video memory being rasterized by the video card and is visible at all times. Hence, you will have only one primary surface in any DirectDraw program, and it refers directly to the screen image and usually resides in VRAM. When you manipulate it, you see the results instantly on the screen. For example, if you set the video mode to 640×480×256, you must create a primary surface that is also 640×480×256 and then attach it to the display device—the `IDirectDraw4` object.

Secondary surfaces, on the other hand, are much more flexible. They can be any size, can reside in either VRAM or system memory, and you can create as many of them as memory will allow. In most cases, you will create one or two secondary surfaces (back buffers) for smooth animation. These will always have the same color depth and geometry as the primary surface. Then you update these offscreen surfaces with the next frame of animation, and then quickly copy or page flip the offscreen surface into the primary surface for smooth animation. This is called double or triple buffering. You’ll learn more on this in the next chapter, but that’s one use for secondary surfaces.

The second use for secondary surfaces is to hold your bitmap images and animations that represent objects in the game. This is a very important feature of DirectDraw because only by using DirectDraw surfaces can you invoke hardware acceleration on bitmap data. If you write your own bit blitting (bitmap image transferring) software to write bitmaps, you lose all acceleration.

Now, I’m getting a little ahead of myself here, so I want to come out of warp and back down to sub-light speed. I just wanted to get you thinking a bit. For now, let’s just see how to create a simple primary surface that’s the same size as your display mode, and then you’ll learn to write data to it and plot pixels on the screen.

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.