PC Gaming
  Home arrow PC Gaming arrow Page 5 - 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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 - Revamping the Game Engine for Input


    (Page 5 of 8 )

    Because we've already built a game engine for use in carrying out the various tasks associated with game management, it only makes sense to incorporate user input handling into the game engine. Granted, a certain aspect of handling user input is game-specific and therefore must be handled in the code for each individual game. However, there are some general aspects of keyboard and mouse handling that you can incorporate into the game engine to simplify the work required of specific game code.

    If you recall, the idea behind the game engine is to provide a certain degree of game functionality in a self-contained class and then delegate game-specific tasks to a series of functions that each game is responsible for providing. For example, the GamePaint() function must be provided in a game to draw the game screen. Although the game engine doesn't technically provide code for the GamePaint() function, it does make sure that it gets called whenever the game window needs to be painted; it's up to each game to provide the actual code for GamePaint(), which makes sense because every game draws itself differently. Similar functions need to be added to the game engine to provide games with a means of handling user input.

    The next couple of sections guide you through modifications to the game engine to add support for keyboard and mouse input handling.

    Adding Keyboard Support

    Earlier in this chapter, you found out that the standard Windows approach to keyboard handling with messages simply isn't good enough for games. A much better way to handle keyboard input is to repeatedly check the state of the keyboard for specific key presses and then react accordingly. Using this strategy, much of the burden of keyboard input handling is passed on to the game code, which means that the game engine is primarily responsible only for calling a keyboard handler function to give the game a chance to respond to key presses. The following is what this function looks like:

    void HandleKeys();

    The HandleKeys() function must be provided as part of the game code, and therefore, it isn't included in the game engine. If you don't want your game to support keyboard input, you can just leave the HandleKeys() function blank. Of course, the game engine must make sure that the HandleKeys() function gets called rapidly enough to give your games a responsive feel. This is accomplished in the WinMain() function within the game engine code. The following is the change made to this function:

    if (iTickCount > iTickTrigger)
    {
    iTickTrigger = iTickCount +
    GameEngine::GetEngine()->GetFrameDelay();
    HandleKeys();
    GameCycle();
    }

    The only change to the WinMain() code is the new call to the HandleKeys() function. Notice that this call occurs just before the GameCycle() function, which means that a game gets a chance to respond to keyboard input before every game cycle. Don't forget; the specifics of handling keyboard input are carried out in each specific game when you create your own HandleKeys() function. You find out how to do this later in the chapter.


    Note - Even if you don't plan on using keyboard input in your games, you must provide a HandleKeys() function—just leave it empty if you don't want any keyboard controls. This same rule applies to the mouse input functions you learn about next, as well as all the game engine functions that are called on a game for game-specific event handling.


    Adding Mouse Support

    Although keyboard input in games is admittedly non-standard in terms of deviating from how things are typically handled in Windows programming, mouse input is handled the old-fashioned way—with messages. It's not that mouse messages are more efficient than keyboard messages; it's just harder to notice sluggish mouse input. In other words, mouse messages appear to be fast enough to allow you to create a responsive mouse interface, whereas keyboard messages do not.

    In order to support mouse input, games must support the The following three functions, which are called by the game engine to pass along mouse events:

    void MouseButtonDown(int x, int y, BOOL bLeft);
    void MouseButtonUp(int x, int y, BOOL bLeft);
    void MouseMove(int x, int y);

    In order to connect mouse messages with these mouse handler functions, the game engine must look for the appropriate mouse messages and respond accordingly. The following code includes the new portion of the GameEngine::HandleEvent() method that is responsible for handling mouse messages delivered to the main game window.

    case WM_LBUTTONDOWN:
    // Handle left mouse button press
    MouseButtonDown(LOWORD(lParam), HIWORD(lParam), TRUE);
    return 0;
    case WM_LBUTTONUP:
    // Handle left mouse button release
    MouseButtonUp(LOWORD(lParam), HIWORD(lParam), TRUE);
    return 0;
    case WM_RBUTTONDOWN:
    // Handle right mouse button press
    MouseButtonDown(LOWORD(lParam), HIWORD(lParam), FALSE);
    return 0;
    case WM_RBUTTONUP:
    // Handle right mouse button release
    MouseButtonUp(LOWORD(lParam), HIWORD(lParam), FALSE);
    return 0;
    case WM_MOUSEMOVE:
    // Handle mouse movement
    MouseMove(LOWORD(lParam), HIWORD(lParam));
    return 0;

    This code handles the following mouse messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONDOWN, WM_RBUTTONUP, and WM_MOUSEMOVE. Each piece of message handler code simply calls one of the mouse functions with the appropriate arguments. The first and second arguments to all the mouse functions include the X and Y position of the mouse cursor at the moment the message is delivered. The last argument to the mouse button functions is a Boolean value that identifies whether the left (TRUE) or right (FALSE) mouse button is involved in the event.


    Note - As you can hopefully tell from the code, the idea behind the mouse functions is to enable games to simply provide MouseButtonDown(), MouseButtonUp(), and MouseMove() functions, as opposed to getting involved with message handling. So, to support the mouse in your games, all you have to do is provide code for these three functions.


    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


     

    Recommended by Dev Hardware

    PC GAMING ARTICLES

    - 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?
    - Xbox 360: Before Next Gen






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway