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
Listing 3-8: Using custom palettes
    type

      TForm1 = class(TForm)

        procedure FormCreate(Sender: TObject);

        procedure FormPaint(Sender: TObject);

        procedure FormDestroy(Sender: TObject);

      private

        { Private declarations }

        FormPalette: HPALETTE;          // a handle to a logical palette

      public

        { Public declarations }

        function GetPalette: HPALETTE; override;

      end;

    {Whoops! Delphi incorrectly imports the GetSystemPaletteEntries function.

     Here’s the correct declaration that gives all of the functionality available

     with this API function}

    function GetSystemPaletteEntries(DC: HDC; StartIndex, NumEntries: UINT;

                                     PaletteEntries: Pointer): UINT; stdcall;

    var

      Form1: TForm1;

    implementation

    {$R *.DFM}

    function TForm1.GetPalette: HPALETTE;

    begin

      {when something requests the palette of the form

       pass it back the new logical palette}

      Result := FormPalette;

    end;

    {link in the GetSystemPaletteEntries function}

    function GetSystemPaletteEntries; external gdi32

             name GetSystemPaletteEntries’;

    procedure TForm1.FormCreate(Sender: TObject);

    var

      ThePalette: PLogPalette;   // a logical palette definition structure

      iLoop: Integer;            // general loop counter

    begin

      {get enough memory to hold the colors in the first 10 system

       palette slots, plus 32 of our own. this memory is temporary,

       and is no longer needed once the palette is created.}

      GetMem(ThePalette, SizeOf(TLogPalette)+42*SizeOf(TPaletteEntry));

      {initialize the palette version number}

      ThePalette^.palVersion := $300;

      {we will have a total of 42 entries in our palette}

      ThePalette^.palNumEntries := 42;

      {get the first 10 system palette entries}

      GetSystemPaletteEntries(Form1.Canvas.Handle, 0, 10,

                              @(ThePalette^.palPalEntry));

      {we only want 32 new palette entries, and we want them to start

       immediately after the first 10 system palette entries. by retrieving

       the first 10 system palette entries, when we realize our new palette,

       the first 10 logical palette entries will be mapped to the first 10

       system palette entries, and our palette entries will follow}

      for iLoop := 0 to 31 do

      begin

        {create a gradient red palette}

        ThePalette^.palPalEntry[iLoop+10].peRed   := 255-((255 div 32)*iLoop);

        ThePalette^.palPalEntry[iLoop+10].peGreen := 0;

        ThePalette^.palPalEntry[iLoop+10].peBlue  := 0;

        {do not match this palette entry to any other palette entry}

        ThePalette^.palPalEntry[iLoop+10].peFlags := PC_NOCOLLAPSE;

      end;

      {create the palette}

      FormPalette := CreatePalette(ThePalette^);

      {free the temporary memory}

      FreeMem(ThePalette, SizeOf(TLogPalette)+42*SizeOf(TPaletteEntry));

    end;

    procedure TForm1.FormPaint(Sender: TObject);

    var

      OldPalette: HPALETTE;       // a handle to the previous palette

      iLoop: Integer;             // general loop control variable

    begin

      {select our new logical palette into the device context}

      OldPalette := SelectPalette(Canvas.Handle, FormPalette, FALSE);

      {map our logical palette into the system palette}

      RealizePalette(Canvas.Handle);

      {display a red gradient}

      for iLoop := 0 to 31 do

      begin

        Canvas.Brush.Color := $01000000 or iLoop+10;

        Canvas.FillRect(Rect(10, iLoop*10+16, 260, (iLoop*10)+36));

      end;

      {select the previous palette back into the device context}

      SelectPalette(Canvas.Handle, OldPalette, FALSE);

    end;

    procedure TForm1.FormDestroy(Sender: TObject);

    begin

      {we no longer need the logical palette, so delete it}

      DeleteObject(FormPalette);

    end;
### Further Adventures in Windows Programming The rest of this book could be dedicated to Windows GDI programming techniques without ever exhausting the subject. Indeed, several such books have been written that cover Windows graphics programming in much greater detail than that presented here. Our goal with this book is to study DirectX programming techniques under Delphi, so further discussion of GDI programming techniques is beyond our scope. However, if you would like more information concerning graphics programming under Windows, consult the following titles: Black Art of Windows Game Programming (ISBN 1-87873-995-6, Waite Group Press) Delphi 2 Multimedia Adventure Set (ISBN 1-88357-764-0, Coriolis Group) The Tomes of Delphi 3: Win32 Core API (ISBN 1-55622-610-1, Wordware Publishing) Several more books that cover graphics programming, both Windows-specific and general algorithms, are listed in the bibliography at the end of the book. The remainder of this book will be dedicated to creating high-speed games using DirectX techniques.
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.