Tricks of the Windows Game Programming Gurus:Advanced DirectDraw and Bitmapped Graphics

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

The function isn’t really that long or that complex; it’s just a pain to write, that’s all. It opens the bitmap file, loads in the headers, and then loads in the image and palette (if the image is a 256-color bitmap). The function works on 8-, 16-, and 24-bit color images. However, regardless of the image format, the buffer that holds the image `UCHAR` buffer is just a byte pointer, so you must do any casting or pointer arithmetic if the image is 16- or 24-bit. In addition, the function allocates a buffer for the image, so the buffer must be released back to the operating system when you’re done mucking with the image bits. This is accomplished with a call to `Unload_Bitmap_File(),` shown here:

    int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap)

    {

    // this function releases all memory associated with the bitmap

    if (bitmap->buffer)

       {

       // release memory

       free(bitmap->buffer);

       // reset pointer

       bitmap->buffer = NULL;

       } // end if

    // return success

    return(1);

    } // end Unload_Bitmap_File

In a moment, I’ll show you how to load bitmap files into memory and display them, but first I want to describe what you’ll do with these images in the general context of a game.

#### Working with Bitmaps

Most games have a lot of artwork, which consists of 2D sprites, 2D textures, 3D models, and so forth. In most cases, 2D art is loaded a frame at a time as single images (as shown in Figure 7.25), or as templates—that is, as a number of similar images all together in a rectangular matrix (as shown in Figure 7.26). Both methods have their pros and cons. The cool thing about loading single images, or one image per file, is that if you make a change to the image with an image processor, you can use the data immediately. However, there may be hundreds of frames of animation that make up a 2D game character. This means hundreds or thousands of separate image `.BMP` files!

Figure 7.25 A standard set of bitmaps without templating.

Figure 7.26 Bitmap images templated for easy access and extraction.

Templated images, as shown in Figure 7.26, are great because the template holds all the animation for a single character, and hence all the data is in one file. The only downfall is that someone has to template the data! This can be very time-consuming, not to mention that there’s an alignment problem because you must create a template of cells, where each cell is m×n (usually m and n are powers of 2) with a one-pixel border around each cell. Next, you write some software to extract an image from a particular cell, because you now know the size of the cell and so forth. You might use both techniques, depending on the type of game and how much artwork it uses. In any case, later you’re going to have to write software that can extract images from loaded bitmaps in single-image or templated format, and then and load the data into DirectDraw surfaces. This allows you to use the blitter, but I’ll get to that later. For now, just use the `Load_Bitmap_Function()` to load an 8-, 16-, and 24-bit bitmap and display it in the primary buffer to get a feel for the function.

> Note: I created most of the art for these demos myself, using various paint or 3D modeling programs and writing the art out as `.BMP` files (some of the art was done by other artists). This is where a good 2D paint program with support for lots of image formats comes in handy. I usually use Paint Shop Pro, the best all-around 2D paint program as far as price and performance go—it’s on this book’s CD, too!

#### Loading an 8-Bit Bitmap

To load an 8-bit image, this all you need to do:

    BITMAP_FILE bitmap; // this will hold the bitmap

    if (!Load_Bitmap_File(&bitmap,";path:\filename.bmp"))

       { /* error */ }

    // do what you will with the data stored in bitmap.buffer

    // in addition, the palette is stored in bitmap.palette

    // when done, you must release the buffer holding the bitmap

    Unload_Bitmap(&bitmap);

The only interesting thing about loading an 8-bit bitmap is that the palette information in the `BITMAP_FILE` structure is valid. You can use the data to change the DirectDraw palette, save it, or whatever. This brings us to a little detail about writing 8-bit games.

> Warning: You can have only 256 colors on the screen at once, so when you’re generating your artwork, remember to find a 256-color palette that looks good when all the artwork is converted to 256 colors (Debabelizer is a great tool for this). In many cases you’ll need multiple palettes—one for each level of the game—but no matter what, all the bitmaps that are going to be used for the same level and could be visible must be in the same palette!

Now is a good time to bring up something that I haven’t had a good reason to show you yet—changing palette entries after the palette has already been created and attached to an 8-bit DirectDraw surface. As you know, most of the demos that you’ve written for 8-bit mode usually create a random or gradient palette and leave it at that. But this time, you’re loading an image that has its own palette, and you want to update the DirectDraw palette object with the new palette entries. When you copy the image to the primary buffer, it looks right. To do this, all you need is the `IDIRECTDRAWPALETTE:SetEntries()` function, as shown here:

    BITMAP_FILE bitmap; // holds the 8-bit image

    // given that the 8-bit image has been loaded

    if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,

        bitmap.palette)))

    { /* error */ }

That’s so easy it’s sickening!

For an illustration of loading an 8-bit image and displaying it, take a look at `DEMO7_10.CPP|EXE`. It loads an 8-bit image in 640×480 and dumps it to the primary buffer.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

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 in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.