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
This function simply sets the color values in the palette to those specified in the array of TPaletteEntry structures specified in the fourth parameter, starting from the entry specified in the second parameter. When this action is performed on a palette attached to the primary surface, the colors of pixels drawn using these palette entries are immediately changed, and palette animation has occurred.
| Note: When using this method to change a range of color entries, it is faster to change several entries that are adjacent (i.e., entries 5, 6, 7, 8, 9), as opposed to changing non-contiguous color entries (i.e., 13, 57, 73, 109).
It would be easy to demonstrate this technique by filling the screen with a color, say palette slot 5, and then using GetEntries and SetEntries in a loop to continuously change the color in entry 5. Similarly, if displaying a bitmap with a lot of pixels drawn using palette slot 5, this technique could change all of those pixels instantly. This could be useful for creating several variations of a monster using only one bitmap. In level one, the monster could be blue, but in level two, you can change the color table entry to red, and using the same bitmap, display a red version of the monster, perhaps indicating that it is a little tougher. To demonstrate the technique of palette animation, we’re going to do something a little more ambitious. Since the GetEntries and SetEntries methods allow us to change a range of color entries, it would be cool if we could draw a bitmap using a color gradient, and then rotate those color entries so the gradient appears to move. Using this technique is also a good way to implement moving water effects. We start by loading our bitmap in and displaying it. Then, in our main animation loop, we use GetEntries to retrieve the range of color values to be modified. We then save the value in the last entry, slide all the other entries up one, and place the saved entry in the first spot. This rotates the color entries as illustrated in Figure 5-2. Figure 5-2: Rotating color entries We then place the modified entries back into the palette by calling SetEntries. The bitmap used in this example is simply the words “Delphi Rocks” drawn using a red gradient. Rotating the palette entries using this technique makes the gradient appear to move, and results in a very cool text animation effect. Listing 5-3 shows the heart of the example code that performs the actual palette animation. Listing 5-3: Animating a palette
    procedure TfrmDXAppMain.DrawSurfaces;

    var

      {our palette slots to be modified}

      Entries: array[0..29] of TTrueColor;

      {a temporary palette slot holder}

      Temp: TTrueColor;

      {loop variable}

      iCount: Integer;

    begin

      {retrieve the range of palette entries to be animated}

      FPalette.GetEntries(0, 9, 30, @Entries);

      {rotate the color entries up one, moving the last palette

       slot into the first slot}

      Temp := Entries[29];

      for iCount := 29 downto 1 do

        Entries[iCount] := Entries[iCount-1];

      Entries[0] := Temp;

      {place the rotated entries into the palette. this will cause

       the colors in the displayed image to change, creating the illusion

       of movement}

      FPalette.SetEntries(0, 9, 30, @Entries);

    end;
#### Screen Transitions As another use for palette animation, let’s examine a very clean, professional screen transition effect known as the fade. A fade is a cinematic effect where an image slowly appears from a solid color, such as black or white.
| fade: A transition effect where an image slowly appears out of a solid color, such as black or white.
Unlike the previous technique, we’ll be performing palette animation on all 256 entries in the palette so that the entire image is affected (however, you could use only a subset of the entries in the palette to fade only portions of the image, but that’s beyond the scope of this exercise). We must start by retrieving the entire color table into a permanent array that will hold all of the color’s original values. The trick to this technique is in setting the values of a “work” array to a percentage of the values stored in the permanent, or “original colors” array, and then setting the palette’s colors to the work array’s values. We could simply change the red, green, and blue color components by a constant value, but that would make some colors fade in and out faster than others, causing the image to appear to change as it fades in or out. By setting the individual color elements as a percentage of the original value for that entry, we achieve a smooth transition effect. To fade in from black, the work array values are started at zero, and then gradually increased until they reach the original values. Fading out just reverses the procedure. Listing 5-4 demonstrates the heart of this technique.
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.