Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

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

#### Palette Functions

The following functions make up the 256-color palette interface. These functions are only relevant if you have the display set for a 256-color mode—that is, 8-bit color.

Function Prototype:

    int Set_Palette_Entry(

                   int color_index, // color index to change

                   LPPALETTEENTRY color); // the color

Purpose:

`Set_Palette_Entry()` is used to change a single color in the color palette. You simply send the color index 0..255, along with a pointer to `PALETTEENTRY` holding the color, and the update will occur on the next frame. In addition, this function updates the shadow palette. Note: This function is slow; if you need to update the entire palette, use `Set_Palette()`. Returns `TRUE` if successful and `FALSE` otherwise.

Example:

    // set color 0 to black

    PALETTEENTRY black = {0,0,0,PC_NOCOLLAPSE};

    Set_Palette_Entry(0,&black);

Function Prototype:

    int Get_Palette_Entry(

                  int color_index, // color index to retrieve

                  LPPALETTEENTRY color); // storage for color

Purpose:

`Get_Palette_Entry()` retrieves a palette entry from the current palette. However, the function is very fast because it retrieves the data from the RAM-based shadow palette. Hence, you can call this as much as you like because it doesn’t disturb the hardware at all. However, if you make changes to the system palette by using `Set_Palette_Entry()` or `Set_Palette()`, the shadow palette won’t be updated and the data retrieved may not be valid. Returns `TRUE` if successful and `FALSE` otherwise.

Example:

    // let's get palette entry 100

    PALETTEENTRY color;

    Get_Palette_Entry(100,&color);

Function Prototype:

    int Save_Palette_To_File(

                   char *filename,  // filename to save at

                   LPPALETTEENTRY palette); // palette to save

Purpose:

`Save_Palette_To_File()` saves the sent palette data to an ASCII file on disk for later retrieval or processing. This function is very handy if you generate a palette on-the-fly and want to store it on disk. However, the function assumes that the pointer in the palette points to a 256-entry palette, so watch out! Returns `TRUE` if successful and `FALSE` otherwise.

Example:

    PALETTEENTRY my_palette[256]; // assume this is built

    // save the palette we made

    // note file name can be anything, but I like *.pal

    Save_Palette_To_file("/palettes/custom1.pal",my_palette);

Function Prototype:

    int Load_Palette_From_File(

              char *filename,  // file to load from

              LPPALETTEENTRY palette); // storage for palette

Purpose:

`Load_Palette_From_File()` is used to load a previously saved 256-color palette from disk via `Save_Palette_To_File()`. You simply send the filename along with storage for all 256 entries, and the palette is loaded from disk into the data structure. However, the function does not load the entries into the hardware palette; you must do this yourself with `Set_Palette()`. Returns `TRUE` if successful and `FALSE` otherwise.

Example:

    // load the previously saved palette

    PALETTEENTRY disk_palette[256];

    Load_Palette_From_Disk(“/palettes/custom1.pal”,&disk_palette);

Function Prototype:

    int Set_Palette(LPPALETTEENTRY set_palette);

                   // palette to load into hardware

Purpose:

`Set_Palette()` loads the sent palette data into the hardware and updates the shadow palette also. Returns `TRUE` if successful and `FALSE` otherwise.

Example:

    // lets load the palette into the hardware

    Set_Palette(disk_palette);

Function Prototype:

    int Save_Palette(LPPALETTEENTRY sav_palette); // storage for palette

Purpose:

`Save_Palette()` scans the hardware palette out into `sav_palette` so that you can save it to disk or manipulate it. `sav_palette` must have enough storage for all 256 entries.

Example:

    // retrieve the current DirectDraw hardware palette

    PALETTEENTRY hardware_palette[256];

    Save_Palette(hardware_palette);

Function Prototype:

    int Rotate_Colors(int start_index, // starting index 0..255

                      int end_index); // ending index 0..255

Purpose:

`Rotate_Colors()` rotates a bank of colors in a cyclic manner. It manipulates the color palette hardware directly. Returns `TRUE` if successful and `FALSE` otherwise.

Example:

    // rotate the entire palette

    Rotate_Colors(0,255);

Function Prototype:

    int Blink_Colors(int command, // blinker engine command

                     BLINKER_PTR new_light,  // blinker data

                     int id); // id of blinker

Purpose:

`Blink_Colors()` is used to create asynchronous palette animation. The function is too long to explain here, so please refer to Chapter 7, “Advanced DirectDraw and Bitmapped Graphics,” for a more in-depth description.

Example:

None

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.