Using Bitmaps C++

You will often find it useful to load the data from an image file onto the screen. This way, you can conveniently draw images ahead of time, store them on a disk, and display them as needed. One very common image file is the bitmap file. A bitmap file is a relatively simple image file and is supported by almost every computer. The extension for a bitmap file is .bmp.

You can load a bitmap onto a surface two ways. One way is to create a temporary surface, copy the bitmap to the surface, and then copy the surface to the other surface. The second way is to load the image into an HBITMAP structure and then copy this image onto the surface.

To use either one of these ways, you must include ddutil.h (which is on the CD-ROM at the back of this book). ddutil.h is a file produced by Microsoft that makes using bitmapping much easier. For the first option, you create the surface, call DDLoadBitmap to load the image, and then blit from one surface to the other. DDLoadBitmap takes four parameters: the DirectDraw object, the name of the image file (including the complete directory), the width of the bitmap, and the height of the bitmap. This function returns the newly created surface. Here is an example of calling this function:

The second way to load a bitmap onto the screen is to use the Windows LoadImage function and then call DDCopyBitmap to copy the image to the surface. LoadImage takes six parameters. The first must be NULL, the second is the name and directory of the bitmap, the third must be IMAGE_BITMAP, the forth and fifth are the height and width, and the sixth must be LR_LOADFROMFILE | LR_CREATEDIBSECTION. LoadImage returns an HBITMAP object, which represents the image. Here is an example:

Notice that you have to cast the return value to HBITMAP. This is because LoadImage is a function for every type of image, not just bitmaps. Once you have this HBITMAP object, you can call DDCopyBitmap to copy it onto the surface. DDCopyBitmap takes six parameters. The first is the surface to copy to, the second is the HBITMAP object, the third and fourth specify the starting position on the bitmap to copy (you’ll usually use 0,0), and the fifth and sixth specify the width and height of the bitmap (from the starting position). Here is an example:

That’s really all it takes to load a bitmap onto the screen. With a little practice, you will get the hang of it.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C++ Topics