PC Gaming
  Home arrow PC Gaming arrow Page 6 - Controlling Games with the Keyboard an...
Dev Hardware Forums 
Computer Cases  
Computer Processors  
Computer Systems  
Digital Cameras  
Flat Panels  
Hardware Guides  
Hardware News  
Input Devices  
Memory  
Mobile Devices  
Motherboards  
Networking Hardware  
Opinions  
PC Cooling  
PC Gaming  
PC Speakers  
Peripherals  
Power Supply Units  
Software  
Sound Cards  
Storage Devices  
Tech Interviews  
User Experiences  
Video Cards  
Mobile Linux 
APP Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PC GAMING

Controlling Games with the Keyboard and Mouse
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 30
    2004-10-11

    Table of Contents:
  • Controlling Games with the Keyboard and Mouse
  • Taking a Look at User Input Devices
  • Mouse and Joystick
  • Tracking the Mouse
  • Revamping the Game Engine for Input
  • Sprucing Up the Bitmap Class
  • Building the UFO Example
  • Testing the Finished Product

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    • 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 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:

    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.


    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.


    SamsThis chapter is from Beginning Game Programming, by Michael Morrison (Sams, ISBN: 0672326590). Check it out at your favorite bookstore today.

    Buy this book now.

    More PC Gaming Articles
    More By Sams Publishing


     

    PC GAMING ARTICLES

    - WoW-ing Online Gamers: What to Expect From t...
    - Age of Conan Review
    - Harnessing Video Game Power for Good
    - Grand Theft Auto IV Review
    - PC Games, a Dying Breed?
    - The Art and Psychology of Gaming
    - Halo 3 Hands On
    - GTA IV: Going Too Far?
    - FEAR Combat Review
    - Prey Review
    - Elder Scrolls IV Oblivion Review
    - PS3: Playing at a Whole New Level
    - F.E.A.R. Video Game Review
    - They Don`t Have to See It to Frag It
    - Do Violent Games Make Violent People?






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT