Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
## CHAPTER 5 Palettes This chapter covers the following topics: • The definition of a color palette • The creation and use of the IDirectDrawPalette object • Palette animation Game programmers tend to have mixed feelings about palettized display modes. On one hand, you’ve got special animation effects and superior performance benefiting you from using a palettized display mode. On the other hand, you’ve got graphic resource management nightmares, an extra layer of indirection and complexity, and a limited resource in the form of color. With the increasing speed of today’s hardware, someday the performance boost available with palettized display modes will not outweigh the benefits of using 16-bit or 32-bit color modes. However, at this point, creating games in a 256-color palettized display mode still has many advantages over the higher color depth modes. Since this book discusses techniques for game and graphics programming under 256 video modes, we should examine how DirectX handles color palettes in a little more depth. In this chapter, we will examine how palettes are supported under DirectDraw in full-screen exclusive mode. We covered color depth and palettes back in Chapter 3, Basic Graphics Programming, and palettes still work very much the same way under DirectX. For the sake of completeness (in case you skipped Chapter 3), we’ll briefly examine what a color palette is and how it works. Then we’ll dive right into creating DirectDraw palettes, how they are used, and some special effects you can accomplish with them. If you read the basic graphics programming chapter, skip ahead to the section on the IDirectDrawPalette object. ### What is a Palette? A color palette is a mechanism by which a potentially large range of colors can be displayed without using enormous amounts of memory. Four and eight bits-per-pixel color depths are the only palettized video modes. As 4 bits-per-pixel color depths are no longer used for games, we will discuss only 8 bits-per-pixel color depths. At 8 bits-per-pixel, each pixel can assume a value between 0 and 255. These values act as an index into what can be considered a 256-entry array known as the color palette. Now, each entry in this array contains an 8-bit value for the blue, green, and red intensity of a color. In effect, this gives 8 bits-per-pixel color depths access to 16,777,216 potential colors, but only 256 of them can be shown at one time. When a bitmap is displayed, the video hardware looks at the 8-bit pixel value, and using that value as an index into the 256-entry color table, it draws a pixel using the red, green, and blue intensities stored in the indicated color table entry. Figure 5-1: An 8 bits-per-pixel bitmap’s pixels act as indices into a color palette There are many things to consider when working with 8 bits-per-pixel color modes. A major disadvantage to this mode is that all bitmaps to be displayed simultaneously must be drawn using the same palette. This requires a lot of careful planning on the part of art production, and can limit the creativity of artists and the variety of artwork that can be displayed on screen. Bitmaps that are drawn using one palette but displayed on a device in which a different palette is currently being used will have unpredictable results. A bitmap’s pixels could be mapped to the closest approximation of the appropriate color in the current palette, but this process is slow, usually produces inadequate results, and is not appropriate for fast action games. Different palettes can be used between scenes, however, which can reduce this effect. However, 8 bits-per-pixel video modes give the greatest speed advantage, and a tremendous number of colors can potentially be used while taking up the least amount of RAM. Up to four pixels can be moved simultaneously by using 32-bit memory movement instructions, allowing bitmaps to be moved about at blindingly fast speeds. Another major advantage comes from palette animation. If colors within the palette are moved about, or cycled, every pixel drawn using that color is affected simultaneously and automatically. This can be used for various special effects, such as flowing water or lighting. Palette animation is not available under non-palettized video modes. Since this book is concerned with high-performance games and graphics programming, all examples will use only 8 bits-per-pixel video modes in order to squeeze out as much speed as possible. ### The IDirectDrawPalette Object Using DirectDraw, an application has direct access to the display card’s hardware palette registers. In order to set the palette entries of the display hardware, the developer must use the IDirectDrawPalette object. Of all the DirectX objects, the palette object is perhaps the easiest to manipulate and use. It has only a few methods, and using the object is very straightforward. #### Creating the Palette Object The CreatePalette method of the IDirectDraw object is called to actually create the IDirectDrawPalette object. This simple function takes only a few parameters, and is declared as:
    function CreatePalette (

    dwFlags: DWORD;                          // control flags

    lpColorTable: pointer;                   // a pointer to an array of

                                             // TPaletteEntry structures

    var lplpDDPalette: IDirectDrawPalette;   // a pointer to the new

                                             // IDirectDrawPalette object

    pUnkOuter: IUnknown                      // COM aggregation

    ): HResult;                              // returns an error code
The first parameter takes a series of constants that can be combined using the OR boolean operator to indicate the type of palette being created and some of its capabilities. In particular, 2-, 4-, 16-, or 256-entry palettes can be created by specifying the appropriate flags. Since we’re primarily interested in 256-color display modes, we’ll using the DDPCAPS_8BIT flag to indicate that we want 256 entries in the palette. The third parameter takes a pointer to a variable of type IDirectDrawPalette that will hold the instantiated palette object when this function returns. The final parameter will be used in the future for COM aggregation. For now, we’ll always set this parameter to nil. The second parameter is really the heart of this function. It is a pointer to an array of palette entry structures that contain the colors used to initialize the palette. In our case, this will be an array of 256 entries. These structures are of type TTrueColor, and are defined as:
    TTrueColor = record

      case integer of

        1 : (Data : DWORD);            // the color as a 32-bit value

        2 : (R,G,B,A : byte);          // red, green, blue, and alpha intensities

    end;
This structure contains members for the red, green, and blue components of a color, defining a 24-bit RGB color value. The final member is useful only under windowed DirectX and with Direct3D, so we will not be concerned with it.
| Note Incidentally, the TPaletteEntry data structure type could also be used. It is defined somewhat differently, but it is the same size as this structure, and its members map to the same members of the TTrueColor structure.
Previous | Table of Contents | Next

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 whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.