31.1 Bitmaps

For a professional-looking program you will often want to use bitmaps. We will talk about two uses for bitmaps. First we'll discuss using bitmaps as background images for your program. And second we'll talk about using small bitmaps as sprites to represent your moving critters. But first we need to say a bit about bitmaps in general.

A bitmap can be something that you draw inside a paint program, a photograph that you scan, or something that you grab off the web. In taking something off the web, however, be sure that it is public domain. If you use copyrighted images in your software, then you're not going to be able to legally distribute your program, and your work is effectively wasted.

Bitmaps come in various kinds of file formats, including *.gif, *.jpg, *.tif, and *.bmp. The Visual Studio, Version 6.0, compiler is only set up to easily deal with bitmaps in the *.bmp format. The MFC Version 7.0 supplied with Visual Studio.NET has a CImage class that does make it possible to handle the other formats. But we aren't presently using this class in the Pop Framework. If you have any kind of high-end image editor on your machine (such as a photograph editor) you can convert from one format to another by loading a bitmap from the existing format and saving it into the *.bmp format. Once you have something in *.bmp format you can, if you like, load it into the Windows Paint Accessory.

As we've discussed before, when you save something into the *.bmp format, you'll notice that you have several options for the kind of *.bmp you save. Typically the options will be labeled 16 Color, 256 Color, 24-bit Color, or True Color (which may use 32 bits per pixel). If your Image Editor doesn't show these options, you can load the *.bmp file inside the Windows Paint Accessory in order to see them.

The 256-color option is usually the best way to go. This gives you a richer palette than the 16-color option, but avoids the problems with 24-bit color and True Color bitmaps. And for a simple icon, 16 Color is okay.

What's wrong with 24-bit Color and True Color bitmaps? The problem is that bitmaps like this make for very large files because you are storing so much information per pixel. A 256-Color bitmap uses eight bits per pixel, rather than 24 bits. A large file can take up a lot of room on your disk, possibly making your *.exe be one or more Meg larger than it would be with 256-Color bitmaps. Another issue is that copying a 24-bit bitmap takes more time than copying a bitmap with a smaller 'color depth.'

Bitmap resources

Once you have a *.bmp file you want to use as a resource, copy it into the \res subdirectory of your code directory. Then use the Resource pane of your Workspace window, right-click on Bitmap, and select Import. In the dialog select the All Files option, find the *.bmp you want and click Import.

Give the bitmap an easy-to-remember ID name like IDB_BACKGROUND. This name will actually stand for some integer, but you don't care what the value of the integer is. If Visual Studio gives it a name you don't like remember that you can change the resource's properties by clicking on it and pressing Alt+Enter.

From now on, when you build your program, the binary code for the new resource bitmap will be bound into your *.exe file. If the bitmap is very large, then your *.exe is going to become very large. The nice thing about the having the bitmap inside your *.exe code is that now you can get at the bitmap by a couple of lines like

CBitmap cBitmap_new; 

To actually do anything with a bitmap, though, you need to load it into a CDC, and, in usual Windows fashion, this is a little trickier than any reasonable person might expect it to be. We've encapsulated the process into the cMemoryDC class as a method called loadResourceBitmap that can be found in the memorydc.cpp file.

If you plan to use 3D graphics, it is extremely important to save your bitmaps so that all of their dimensions are powers of two.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference