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-7: Implementing masking techniques
    procedure TForm1.FormPaint(Sender: TObject);

    begin

      {copy the background image to the destination}

      Canvas.Draw(480, 48, imgBackground.Picture.Bitmap);

      {combine the AND mask image with the background image in the destination

       using a boolean AND operation. this carves out an area for the final

       foreground image}

      Canvas.CopyMode := cmSrcAnd;

      Canvas.Draw(480+((imgBackground.Width div 2)-(imgAndMask.Width div 2)),

                  48+((imgBackground.Height div 2)-(imgAndMask.Height div 2)),

                  imgAndMask.Picture.Bitmap);

      {copy the result of step one into the background’ image used for step 2}

      Canvas.CopyMode := cmSrcCopy;

      Canvas.CopyRect(Rect(8, 288, 248, 468), Canvas, Rect(480, 48, 720, 228));

      {copy the background’ image resulting from step 1 into the destination}

      Canvas.CopyRect(Rect(480, 288, 720, 468), Canvas, Rect(8, 288, 248, 468));

      {combine the OR mask image with the result from step 1 in the destination

       using a boolean OR operation.  this copies the foreground image into the

       area carved out by step 1 while preserving the pixels around it, thereby

       creating the illusion of transparency.}

      Canvas.CopyMode := cmSrcPaint;

      Canvas.Draw(480+((imgBackground.Width div 2)-(imgAndMask.Width div 2)),

                  288+((imgBackground.Height div 2)-(imgAndMask.Height div 2)),

                  imgORMask.Picture.Bitmap);

    end;
Figure 3-18: The transparently displayed image ### Palettes As previously discussed, working under palettized video modes provides a number of advantages and disadvantages. Palettized modes can be a real pain at times, requiring careful planning on the part of art production and special considerations on the part of the programmer. However, the advantages of working under a 256-color video mode usually outweigh the disadvantages, primarily because it is a very speedy mode to work under, and palette animation can be used for special effects. The Windows palette manager is a difficult beast to tame. There are many nuances to working in a palettized mode under Windows, making the creation of games using regular GDI functions difficult indeed. As this book’s primary topic is game and graphics programming using DirectX, we will not cover palettes in the depth required for making non-DirectX games. However, it is useful to cover a few of the most basic palette topics in order to prepare the reader for DirectX palettes covered in a later chapter. #### Logical vs. System Palettes There are two different kinds of palettes used by the Windows operating system: the system palette and logical palettes. The system palette is a Windows- maintained global palette containing all of the colors that can be displayed. The first and last ten palette entries are reserved for the system, and contain the 20 static colors that are used for drawing specific user interface elements, such as window title bars, borders, and 3-D user interface elements. The remaining 236 colors can be set in the application. Logical palettes are created by the application and contain the colors that the application would like to use. When the application specifies the palette it wishes to use, the Windows palette manager merges the logical palette with the current system palette. It accomplishes this by examining each entry in the logical palette and mapping it to an existing entry in the system palette, if a particular color exists in both palettes. If no matching entry is found, the color in the logical palette is placed into the first unused entry in the system palette. If an existing entry is not found and there are no more unused palette slots, the Windows palette manager maps the color to the closest approximation in the system palette. This continues until all colors in the logical palette have been merged with the colors in the system palette. A logical palette must be selected into a device context before it can be used. If the specified device context identifies an active window, the logical palette is treated as a foreground palette and the Windows palette manager will mark all non-static entries in the system palette as unused. This allows the logical palette to potentially replace all non-static colors of the system palette. If the device context does not identify an active window, the logical palette is treated as a background palette, and can set unused palette slots only if any remain after the foreground palette has been merged. The palette information stored in a DIB bitmap file can be considered a logical palette. Delphi takes care of most palette-specific concerns automatically, as the TBitmap object will load a bitmap’s palette and put it into use. In the next chapter, we will learn how to load a palette from a bitmap and use it to initialize the DirectX palette.
| Note: The TBitmap object has a property called Palette. This property is the handle to the bitmap’s logical palette, and can be used in any GDI function that manipulates logical palettes.
#### Using a Palette Delphi encapsulates a large portion of the Windows API, making Windows programming easy and fun. Unfortunately, not everything in the Windows API has been conveniently simplified in this manner, and this includes palette functionality. This means that in order to use palettes, one must use low-level Windows API calls throughout most of the process. In order to begin using a custom palette, a logical palette must first be made. First, variables of type PLogPalette and HPALETTE must be declared. The PLogPalette is a pointer to a logical palette structure, which contains a member for the palette version, a member for the number of palette entries, and then an array of type TPaletteEntry. The TPaletteEntry type is a structure that contains fields for the red, green, and blue components of a color, and a flag indicating how the entry is to be used. The PLogPalette variable is initialized to the appropriate values, including the desired colors, and is then passed to the CreatePalette function. This returns a handle to the newly created palette, which is then assigned to the HPALETTE variable. At this point, the palette can be selected into a device context using the SelectPalette function. If using a bitmap, the palette handle can be directly assigned to the TBitmap.Palette property. To actually merge the logical palette with the system palette so the new colors can be used, call the RealizePalette function. When the application no longer needs the logical palette, it should be deleted by calling the DeleteObject function. The following example demonstrates creating a logical palette to draw a red gradient fill. Figure 3-19: The custom palette
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.