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

This function is rather general, but you only want to use it to load `.BMP` files from the disk, so you don’t have to worry about all the other stuff it does. Simply set the parameters to the following values to load a `.BMP` from disk:

  `hinst`—This is the instance handle. Set it to `NULL`.
  `lpszName`—This is the name of the `.BMP` file on disk. Send a standard `NULL`-terminated filename like `ANDRE.BMP`, `C:/images/ship.bmp`, and so forth.
  `uType`—This is the type of image to load. Set it to `IMAGE_BITMAP`.
  `cxDesired, cyDesired`—These are the desired width and height of the bitmap. If you set these to any number other than 0, `LoadImage()` will scale the bitmap to fit. Therefore, if you know the size of the image, set them. Otherwise, leave them at 0 and read the size of the image later.
  `fuLoad`—This is a load control flag. Set it to `(LR_LOADFROMFILE | LR_CREATEDIBSECTION)`. This instructs `LoadImage()` to load the data from disk using the name in `lpszName` and to not translate the bitmap data to the current display device’s color characteristics.

The only problem with this function is that it’s so general that getting to the damn data is difficult. You have to use more functions to access the header information, and if there’s a palette, more trouble arises. Instead, I just created my own `Load_Bitmap_File()` function that loads a bitmap from disk in any format (including palettized) and stuffs the information into this structure:

    typedef struct BITMAP_FILE_TAG

            {

            BITMAPFILEHEADER bitmapfileheader;  // this contains the

                                                // bitmapfile header

            BITMAPINFOHEADER bitmapinfoheader;  // this is all the info

                                                // including the palette

            PALETTEENTRY     palette[256];// we will store the palette here

            UCHAR            *buffer;    // this is a pointer to the data

            } BITMAP_FILE, *BITMAP_FILE_PTR;

Notice that I’ve basically put the `BITMAPINFOHEADER` and the exploded `BITMAPINFO` all together in one structure. This is much easier to work with. Now for the `Load_Bitmap_File()` function:

    int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)

    {

    // this function opens a bitmap file and loads the data into bitmap

    int file_handle,  // the file handle

        index;        // looping index

    UCHAR   *temp_buffer = NULL; // used to convert 24 bit images to 16 bit

    OFSTRUCT file_data;          // the file data information

    // open the file if it exists

    if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)

       return(0);

    // now load the bitmap file header

    _lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));

    // test if this is a bitmap file

    if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)

       {

       // close the file

       _lclose(file_handle);

       // return error

       return(0);

       } // end if

    // now we know this is a bitmap, so read in all the sections

    // first the bitmap infoheader

    // now load the bitmap file header

    _lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));

    // now load the color palette if there is one

    if (bitmap->bitmapinfoheader.biBitCount == 8)

       {

       _lread(file_handle, &bitmap->palette,

              MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));

       // now set all the flags in the palette correctly

       // and fix the reversed

       // BGR RGBQUAD data format

       for (index=0; index < MAX_COLORS_PALETTE; index++)

           {

           // reverse the red and green fields

           int temp_color                = bitmap->palette[index].peRed;

           bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;

           bitmap->palette[index].peBlue = temp_color;

           // always set the flags word to this

           bitmap->palette[index].peFlags = PC_NOCOLLAPSE;

           } // end for index

        } // end if

    // finally the image data itself

    _lseek(file_handle,

           -(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);

    // now read in the image

    if (bitmap->bitmapinfoheader.biBitCount==8 ||

        bitmap->bitmapinfoheader.biBitCount==16 ||

        bitmap->bitmapinfoheader.biBitCount==24)

       {

       // delete the last image if there was one

       if (bitmap->buffer)

           free(bitmap->buffer);

       // allocate the memory for the image

       if (!(bitmap->buffer =

           (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))

          {

          // close the file

          _lclose(file_handle);

          // return error

          return(0);

          } // end if

       // now read it in

       _lread(file_handle,bitmap->buffer,

              bitmap->bitmapinfoheader.biSizeImage);

       } // end if

    else

       {

       // serious problem

       return(0);

       } // end else

    // close the file

    _lclose(file_handle);

    // flip the bitmap

    Flip_Bitmap(bitmap->buffer,

                bitmap->bitmapinfoheader.biWidth*

                (bitmap->bitmapinfoheader.biBitCount/8),

                bitmap->bitmapinfoheader.biHeight);

    // return success

    return(1);

    } // end Load_Bitmap_File

> Note: There’s a call to `Flip_Bitmap()` at the end of the function. This simply inverts the image because most `.BMP` files are in bottom-up format. `Flip_Bitmap()` is part of the library I’m building, and it’s copied into the demos that will come shortly so you can review it at any time.

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.