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

Notice that you missed the last pixel in the source—the 90—altogether. You may or may not like this; maybe you always want to see the top and bottom pixel in the scaling operation for scales 1×2 and greater and would rather throw away some in-between pixels. This is where rounding and biasing the `sample_rate` and `sample_index` come into play—think about it…

Now that you know how to scale an image, let DirectDraw do it for you. `DEMO7_14.CPP|EXE` is a remake of `DEMO7_13.CPP|EXE`, but I’ve added some code to arbitrarily scale the aliens so that they seem to be different sizes. If you have hardware scaling, the demo will run very smoothly, but if you don’t, you may notice some degradation. Again, you’ll see how to use `IDIRECTDRAWSURFACE4::GetCaps()` to detect this later in the chapter.

### Color Effects

The next subject I want to discuss is color animation and tricks. In the past, 256-color palettized modes were the only bit depths available, and a lot of tricks and techniques were invented to take advantage of the instantaneous nature of color changes—that is, a change to one or more of the palette registers is instantly visible on the screen. These days 256-color modes are fading away due to faster hardware and acceleration. However, learning these techniques is still crucial to understanding other related concepts, not to mention that it will be many years until all games are totally RGB. There are a lot of 486 machines and even slower-MHz Pentiums still around that can handle only 256-color modes at any sort of reasonable speed!

#### Color Animation in 256-Color Modes

Color animation basically refers to any operation of modifying or shifting around color palette entries on-the-fly. For example, glowing objects, blinking lights, and many other effects can be created simply by manipulating the entries of the color table on-the-fly. The cool thing is that any object on the screen that has pixels with the values that you’re manipulating in the color table will be affected.

Imagine how hard it would be to do this with bitmaps. For example, let’s say you had a little ship with running lights on it, and you wanted them to blink. You would need a bitmap for each frame of animation. But with color animation, all you need to do is draw a single bitmap, make the lights a specific color index, and then animate the color index. Figure 7.36 illustrates this indirection graphically.

Figure 7.36 Color animation using palette indirection.

Two of my favorite effects are blinking and glowing colors. Let’s begin with a blinking light function. Here’s the functionality you want:

  • Creating up to 256 blinking lights.
  • Each light has an on and off color, in addition to a time delay measured in cycles for the on and off state.
  • Turning on or off any blinking light at any time with an ID, and/or resetting its parameters.
  • Terminating any blinking light and reusing its data storage.

This is a perfect example for showing some persistent data techniques and showing how to update DirectDraw palette entries. My strategy will be to create a single function that’s called to create and destroy the lights as well as to perform the animation. The function will use static data arrays that are local and the function will have a number of operation modes:

  `BLINKER_ADD`—Used to add a blinking color to the database. When called, the function returns an ID number used to reference the blinking light. System holds up to 256 lights.
  `BLINKER_DELETE`—Deletes a blinking light of the sent ID.
  `BLINKER_UPDATE`—Updates the on/off parameters of the sent ID’s light.
  `BLINKER_RUN`—Processes all the lights through one cycle.

The data structure used to hold a single light and also to create one is called `BLINKER` and is shown here:

    // blinking light structure

    typedef struct BLINKER_TYP

                   {

                   // user sets these

                   int color_index;         // index of color to blink

                   PALETTEENTRY on_color;   // RGB value of "on" color

                   PALETTEENTRY off_color;  // RGB value of "off" color

                   int on_time;             // number of frames to keep "on"

                   int off_time;            // number of frames to keep "off"

                   // internal member

                   int counter;             // counter for state transitions

                   int state;               // state of light,

                                            // -1 off, 1 on, 0 dead

                   } BLINKER, *BLINKER_PTR;

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.