Adding Mouse Support Although keyboard input in games
program example at the end of this hour 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! 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: bTrans a Boolean value that indicates whether the bitmap should be drawn with transparency crTransColor the transparent color of the bitmap It’s important to try and make changes to the game engine that don’t cause problems with programs that we’ve already written. So, 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 worked. In case you’re curious, 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. 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: 1. Open the project in Visual Studio (Visual C++). 2. Right-click on the project’s folder in Solution Explorer, and click Properties in the pop-up menu. 3. Click the Linker folder in the left pane of the Properties window, and then click Input. 4. Click next to Additional Dependencies in the right pane, and type msimg32.lib. 5. Click the OK button to accept the changes to the project. 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. The source code for the examples in the book is located on the accompanying CD-ROM, and includes Visual C++ project files with the appropriate linker settings already made.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services