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
| Tip: Most games require a standardized palette such that all graphics are based on the same palette. If you are creating a game that features many different screens, you could create all backgrounds and artwork with individual palettes for each screen. Then, you could use the fade in/out screen transition and change the palette based on the new screen. This would allow you to use multiple palettes in a game, providing for a richer depth of color.
Listing 5-4: Performing screen transitions
    procedure TfrmDXAppMain.FadeEffect(FadeIn: Boolean);

    const

      STEPS = 100;     // we want 100 fade steps

    var

      iCount, iCount2: Integer;

    begin

      {if we are fading in from black...}

      if FadeIn then

      begin

        for iCount := 0 to STEPS do

        begin

          {modify the work’ palette to a percentage of each original palette’s

           color value, based on our current step}

          for iCount2 := 0 to 255 do

          begin

            AnimPalette[iCount2].R := Trunc(OrgPalette[iCount2].R*(iCount/STEPS));

            AnimPalette[iCount2].G := Trunc(OrgPalette[iCount2].G*(iCount/STEPS));

            AnimPalette[iCount2].B := Trunc(OrgPalette[iCount2].B*(iCount/STEPS));

            AnimPalette[iCount2].A := 0;

          end;

          {plug the palette into the hardware, causing the colors to take effect}

          FPalette.SetEntries(0, 0, 256, @AnimPalette);

          {pause for a fraction of a second}

          Sleep(10);

        end;

      end

      else

      {if we are fading out to black...}

      begin

        for iCount := 0 to STEPS do

        begin

          {begin the work’ palette to a percentage of each original palette’s

           color value, based on our current step. notice this is opposite from

           the previous block}

          for iCount2 := 0 to 255 do

          begin

            AnimPalette[iCount2].R := OrgPalette[iCount2].R-

                                      Trunc(OrgPalette[iCount2].R*(iCount/STEPS));

            AnimPalette[iCount2].G := OrgPalette[iCount2].G-

                                      Trunc(OrgPalette[iCount2].G*(iCount/STEPS));

            AnimPalette[iCount2].B := OrgPalette[iCount2].B-

                                      Trunc(OrgPalette[iCount2].B*(iCount/STEPS));

            AnimPalette[iCount2].A := 0;

          end;

          {plug the palette into the hardware, causing the colors to take effect}

          FPalette.SetEntries(0, 0, 256, @AnimPalette);

          {pause for a fraction of a second}

          Sleep(10);

        end;

      end;

    end;
### Summary In this chapter, we discussed how the IDirectDrawPalette object provides an application with hardware palette management functionality within an exclusive, full-screen DirectDraw environment. Keep these points in mind when programmatically manipulating the hardware palette: • The hardware palette is only used under palettized display modes. Thus, you will only be concerned with the palette when creating 256-color games. • Palettes give us access to a large range of colors while keeping memory requirements to a minimum. This also improves application performance as moving pixels between surfaces is very efficient. Under palettized display modes, we also have access to palette animation, which makes certain special effects easy to implement. • The colors in the palette could be defined programmatically, but typically they are initialized from the palette of a bitmap containing graphics used in the game. • By default, only 254 palette entries can be used. DirectDraw predefines palette slot 0 as black and palette slot 255 as white. You can use all 256 entries by specifying the DDPCAPS_ALLOW256 flag in the first parameter of the call to CreatePalette. However, bear in mind that some video cards use palette slot 0 to define the overscan color, the color of the area around the usable display area. • If a surface becomes lost and must be restored, any attached palettes are unaffected, and do not need to be reconnected after a surface is restored. • Palettes do not move when surfaces are flipped, so it is unnecessary to attach palettes to both a primary surface and backbuffer. • Only the palette associated with the primary surface will affect the actual hardware palette registers. Thus, you must attach the newly created palette to the primary surface before the new colors take effect. Palettes can actually be attached to any palettized surface, and the same palette can be attached to more than one surface, but only the palette attached to the primary surface will actually affect the display. If a palette is not attached to the primary surface, the current entries in the hardware palette (left over from the last application) will be used. • When mixing GDI functions with DirectX, GDI functions will use whatever palette is attached to the primary surface at the time the function is called.
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.