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

#### Playing with Palettes

There’s not much to talk about when it comes to palettes. DirectDraw only gives you a bit-encoded `WORD` that describes the abilities of any given palette. The function is called `IDIRECTDRAWPALETTE::GetCaps()` and is shown here:

    HRESULT GetCaps(LPDWORD lpdwCaps);

`lpdwCaps` is a bit-encoded `WORD` with the values in Table 7.7.

Table 7.7 Possible Flags for Palette Capabilities

Value | Description
--- | ---

`DDPCAPS_1BIT` | Supports 1-bit color palettes.
`DDPCAPS_2BIT` | Supports 2-bit color palettes.
`DDPCAPS_4BIT` | Supports 4-bit color palettes.
`DDPCAPS_8BIT` | Supports 8-bit color palettes.
`DDPCAPS_8BITENTRIES` | Palette is an index palette.
`DDPCAPS_ALPHA` | Supports an alpha component with each palette entry.
`DDPCAPS_ALLOW256` | All 256 colors can be defined.
`DDPCAPS_PRIMARYSURFACE` | Palette is attached to primary surface.
`DDPCAPS_VSYNC` | Palette can be modified synchronously with monitor’s refresh.

### Using DirectDraw in Windowed Modes

The last subject that I want to touch upon is using DirectDraw in windowed mode. The problem with windowed mode, as far as games go, is that you have very little control over the initial setting of the color depth and resolution. Writing DirectDraw applications that run full-screen is hard enough, but generalizing to a windowed mode is much more complex. You have to take into consideration that the user may start your application at any resolution and/or color depth. This means that the performance of your application may suffer. Not only that, but your game may be designed to work solely for 8- or 16-bit modes, and your code could fall apart completely if the user is in a higher color depth.

Although writing games that work in both windowed mode and full-screen mode is the best of both worlds, I’m going to stick to full-screen mode to simplify most of the work and demos. However, I may create a number of windowed applications that run specifically in 800×600 or higher resolutions with a color depth of 8 bits. This way you can more easily debug your DirectX application and/or have other output windows, including the graphics output. In any case, let’s take a look at how to write a windowed DirectX application and how to manipulate the primary surface.

The first thing to know about windowed DirectDraw applications is that the primary surface is the entire screen display, not just your window! Take a look at Figure 7.38 to see this graphically. What this means is that you can’t just write to the screen display blindly, or you’ll end up mangling the client areas of other application windows. Of course, this may be your intent if you’re writing a screen saver or other screen manipulation program. However, in most cases, you’ll only want to write to the client area of your application’s window. That means that somehow you must find the exact coordinates of your client window and then make sure only to draw in that area.

Figure 7.38 In windowed modes, the entire desktop is mapped to the DirectDraw primary surface.

The second problem is with clipping. If you intend to use the `Blt()` function, it has no idea about your client window and will blit over the edges of the client window. This means that somehow you must tell the DirectDraw clipping system that you have a window on the screen so that it will make sure to clip to the window no matter where it’s moved to or how it’s sized.

This brings us to yet another problem—what if the window is moved or resized by the user? True, you can at least force the window to stay the same size, but movement is an absolute must. Otherwise, why even have a windowed application? To handle this, you must track the `WM_SIZE` and `WM_MOVE` messages.

The next problem is 8-bit palettes. If the video mode is 8-bit mode and you want to change the palette, you’re in for trouble. Windows has a Palette Manager that you must appease if you’re going to mess with the 8-bit palette, because you’ll probably make all other Windows applications look ugly. In most cases, you can change the palette, but it’s a good idea to leave about 20 of the palette entries alone (the Windows and system colors) so the Palette Manager can make the other applications look something like what they’re supposed to.

Finally, the most obvious problems are those of bit blitting and pixel manipulation based on varying modes. Hence, you have to write code that handles all color depths if you really want to be robust.

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.