Controlling Games with the Keyboard and Mouse - Sprucing Up the Bitmap Class
(Page 6 of 8 )
You've now made the necessary changes to the game engine to prepare it for keyboard and mouse input. However, there is another minor change you need to make that doesn't technically have anything to do with input. I'm referring to bitmap transparency, which enables bitmaps to not always appear as square graphical objects. Don't get me wrong; bitmaps definitely are square graphical objects, but they don't necessarily have to be drawn that way. The idea behind transparency is that you can identify a color as the transparent color, which is then used to indicate parts of a bitmap that are transparent. When the bitmap is drawn, pixels of the transparent color aren't drawn, and the background shows through.
Note - Why is there a discussion of bitmap transparency in a chapter focused on keyboard and mouse input? The answer has to do with the fact that I want you to view the game engine as a work in progress that is constantly evolving and picking up new features. In this case, the example at the end of this chapter benefits greatly from bitmap transparency, so it only makes sense to add the feature here. You'll continue to make small improvements to the game engine throughout the book, even if they don't tie in directly to the topic at hand. The end result will be a game engine with a lot of slick little features that will make your games all the more fun.
From a graphics creation perspective, you create bitmaps with transparency by selecting a color that isn't used in your graphics, such as "hot purple," which is also known as magenta. You then use magenta to fill areas of your bitmaps that need to appear transparent. It's then up to the revamped game engine to make sure that these transparent regions don't get drawn with the rest of the bitmap. You obviously don't want magenta borders around your images!
Note - The game development community is mixed in its use of transparency colors. In the old days, magenta (RGB: 255, 0, 255) was the "standard" color to denote transparency. It turns out that it is such an obnoxious color that it is rarely used in game graphics, which is exactly what you need in a "transparent color." However, magenta can be problematic when it comes to blending in a graphic with a background because image editors tend to "feather" the edges of an image to smooth it into its background. Most commercial 3D games use pure black (RGB: 0, 0, 0), pure blue (RGB: 0, 0, 255), or medium gray (RGB: 128, 128, 128) as their transparency color, or they use alpha transparency, which is a whole different can of worms. I've stuck with magenta as the transparency color for the examples throughout the book, but as long as you're consistent within the graphics for a particular game, you can select any unused color you want.
The trick to making bitmap transparency work in the game engine is to expand the existing Bitmap::Draw() method so that it supports transparency. This is accomplished by adding two new arguments:
It's important to try to make changes to the game engine that don't cause problems with programs we've already written. Therefore, rather than add these two arguments to the Draw() method and require them of all bitmaps, it's much better to add them and provide default values:
void Draw(HDC hDC, int x, int y, BOOL bTrans = FALSE,
COLORREF crTransColor = RGB(255, 0, 255));
If you notice, the default value of bTrans is FALSE, which means that if you leave off the argument, transparency isn't used. This works great for existing code because it doesn't change the way the Draw() method already works. You'll notice that the default color specified in crTransColor (RGB(255, 0, 255)) is magenta, so if you stick with that color as your transparent color, you won't have to specify a transparent color in the Draw() method.
The only significant changes to the Draw() method involve checking the transparency argument and then drawing the bitmap with transparency using the Win32 TransparentBlt() function if the argument is TRUE. Otherwise, it's business as usual with the BitBlt() function being used to draw bitmaps without transparency.
Note - Although the TransparentBlt() function is part of the Win32 API, it isn't as widely supported as the traditional BitBlt() function. More specifically, the function isn't supported in versions of Windows prior to Windows 98, such as Windows 95.
The TransparentBlt() function is part of the Win32 API, but it requires the inclusion of a special library called msimg32.lib in order for your games to compile properly. This is a standard library that should be included with your compiler, but you'll need to make sure that it is linked in with any programs that use the TransparentBlt() function. If you aren't familiar with altering linker settings for your compiler, just take a look at the compiler documentation and find out how to add additional libraries to a project; it typically involves simply entering the name of the library, msimg32.lib in this case, in a project settings window. Or, if you happen to be using Microsoft Visual Studio, you can follow these steps:
Open the project in Visual Studio (Visual C++).
Right-click on the project's folder in Solution Explorer and click Properties in the pop-up menu.
Click the Linker folder in the left pane of the Properties window and then click Input.
Click next to Additional Dependencies in the right pane and type msimg32.lib.
Click the OK button to accept the changes to the project.
Note - If you're using Microsoft Visual Studio 6, you can accomplish the same task of adding the msimg32.lib file to your project by first right-clicking on the Resource Files project folder. Then, select Add Files to Folder from the pop-up menu and browse to the \LIB directory. Finally, select MSIMG32.LIB and click OK.
After completing these steps, you can safely compile a program and know that the msimg32.lib library is being successfully linked into the executable program file. You'll know if you didn't edit the linker settings properly because a game build will fail with linker errors if it doesn't link in the msimg32.lib library.
Note - The source code for the examples in the book is located on the accompanying CD-ROM and includes Visual C++, Borland C++Builder, and Dev-C++ project files with the appropriate msimg32.lib linker settings already made.
This chapter is from Beginning Game Programming, by Michael Morrison (Sams, ISBN: 0672326590). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Building the UFO Example >>
More PC Gaming Articles
More By Sams Publishing