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-6: Accessing individual pixels through the ScanLine property
    procedure TForm1.Button1Click(Sender: TObject);

    var

      Bitmap: TBitmap;

      Pixels: PByteArray;

      iBand, iRow, iCol: Integer;

      DrawColor: Byte;

    begin

      {create the bitmap object}

      Bitmap := TBitmap.Create;

      {initialize the appropriate properties so we can access individual pixels

       in a known way}

      Bitmap.Width  := 169;

      Bitmap.Height := 128;

      Bitmap.PixelFormat := pf8Bit;

      {create 8 horizontal bands of alternating colors}

      DrawColor := 19;

      for iBand := 0 to 7 do

      begin

        {flip-flop the color. this is an 8 bit image, so we specify the color

         as an index into the palette.}

        if DrawColor = 16 then

          DrawColor := 19

        else

          DrawColor := 16;

        {each band is comprised of 16 rows of color}

        for iRow := 0 to 15 do

        begin

          {retrieve a pointer to the row of pixels}

          Pixels := Bitmap.ScanLine[(iBand*16)+iRow];

          {set each pixel in the row to the specific color}

          for iCol := 0 to Bitmap.Width-1 do

            Pixels[iCol] := DrawColor;

        end;

      end;

      {draw the bitmap to the screen}

      Canvas.Draw(8, 8, Bitmap);

      {free the bitmap}

      Bitmap.Free;

    end;
Figure 3-13: Individually painted pixels #### Raster Operations In addition to simply copying bitmap pixels from place to place, certain functions can modify exactly how the pixels are drawn onto their destination according to a specified setting. This is known as a raster operation, or ROP, and it determines how the pixels from the source, the destination, and the destination device context’s selected brush are combined. The pixels are combined using boolean operations.
| raster operation (ROP): A Boolean function performed on pixels when they are copied from bitmap to bitmap that determines how the pixels from the source, the destination, and the destination device context’s selected brush are combined.
There are 256 total raster operations, each producing a different output based on the Boolean operator used and the order in which pixels are combined. Of these, only 16 are commonly used in Windows graphics programming. There are several GDI functions, such as BitBlt and StretchBlt, that allow a developer full access to all 256 raster operations. Using the Delphi VCL, the CopyMode property of the TCanvas object specifies the raster operation to be used, and can be set to one of 16 constants that represent the 16 most commonly used raster operations. Raster operations are incredibly useful under certain circumstances. They can be used to easily implement specific special effects which, without raster operations, would be more difficult to implement otherwise. For example, using raster operations, it is very easy to simulate transparent pixels in a bitmap when it is displayed over a background. This effect and others could be implemented using any one of a hundred different techniques, but raster operations make their implementation fast and simple. #### Simulating Transparency Perhaps the most common function performed by the graphical output code of a game is to display one bitmap over another, with the pixels of the background showing through the “transparent” pixels of the displayed bitmap or sprite. Without transparency, every sprite image would have to be rectangular, or the resulting animation would look horrible. Figure 3-14 graphically illustrates the importance of transparency. Figure 3-14: The effect of transparency The easiest method by which sprites can be transparently drawn to the background is known as masking. Masking uses two bitmaps for each sprite image, as each bitmap will be used with a different raster operation when the sprite is drawn.
| masking: The act of using two bitmaps, an AND bitmap and an OR bitmap, along with raster operations to draw one bitmap over another while simulating transparent pixels in the source bitmap.
This method is precisely what happens when an application sets a TBitmap object’s Transparent property to True and then calls TCanvas.Draw. This is a very convenient encapsulation in Delphi. However, the only drawback is that the transparent color is either the color of the pixel in the bottom leftmost pixel of the bitmap or is specified in the TBitmap’s TransparentColor property, depending on the value of its TransparentMode property. The result is that this specified transparent color cannot be used elsewhere in the image, and unless proper care is taken when constructing the image, the result may not be what is expected. Using the direct approach as explained below gives the developer more control over the appearance of the final image. Figure 3-15 displays the difference between using the TransparentColor property and using the technique described below. The two sprite images consist of an AND mask and an OR mask. The OR mask is the actual sprite image as it should appear in the destination. The only requirement for the OR mask is that pixels that should be transparent must be totally black (this must be palette slot zero for 256 color images). The AND mask is a monochrome bitmap consisting of only black and white. The black areas should match those of the actual sprite image in the OR mask, and the white areas represent pixels that should be transparent (opaque, or sprite image pixels, must be palette slot 0, and transparent pixels must be palette slot 255). Figure 3-16 illustrates a sprite image (the OR mask) and its accompanying silhouette (the AND mask). Figure 3-15: Using the TBitmap.Transparent property versus the direct approach Figure 3-16: A sprite AND and OR mask Drawing a sprite image with transparent pixels requires two steps. First, the AND mask is drawn to the destination bitmap using a boolean AND raster operation. The white pixels (representing the transparent areas) have a binary value of 11111111 for 256-color images (a decimal value of 255, the last palette slot). Higher color depths will result in a binary value of all ones. When this binary value is ANDed with the colors of the pixels in the destination image, the result is the color of the pixels in the destination image, thereby preserving the destination image data. The black pixels (representing opaque image data), on the other hand, have a binary value of 00000000. When ANDed with the destination image, the result is black. Effectively, this creates a “cutout” area in the destination image where the actual sprite image will appear. The second step is to draw the OR mask to the destination bitmap using a boolean OR raster operation. The black pixels of the OR mask (representing the transparent areas), when ORed with the destination, will preserve the destination image data, just as the white pixels did in the first step. The actual sprite image data, when ORed with the destination, will be combined with the black “cutout” area produced from the first step. Thus, the sprite image data will be preserved. This results in a perfectly copied sprite with the background image showing through the transparent areas. Figure 3-17 demonstrates the results of each step in the process. Figure 3-17: Using masks with raster operations The result, while perhaps not blindingly fast, should be more than adequate under most circumstances, if there are not a large amount of sprites on the screen. This technique is easy to implement, but it does require double the storage space for sprite images. The following example demonstrates how this technique is implemented.
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.