Tracking the Mouse


When you move the mouse, a series of events is set off that is very similar to those set off by the keyboard. In fact, the Win32 API includes a series of mouse messages that are used to convey mouse events, similar to how keyboard messages convey keyboard events. You learned in the previous section that the Win32 keyboard messages aren't up to the task of providing efficient input for games. Fortunately, the same cannot be said of the mouse messages. It turns out that the built-in Win32 approach to handling mouse events via messages works out just fine for games . Following are the mouse messages used to notify Windows programs of mouse events:

  • WM_MOUSEMOVE ” Any mouse movement

  • WM_LBUTTONDOWN ” Left mouse button pressed

  • WM_LBUTTONUP ” Left mouse button released

  • WM_RBUTTONDOWN ” Right mouse button pressed

  • WM_RBUTTONUP ” Right mouse button released

  • WM_MBUTTONDOWN ” Middle mouse button pressed

  • WM_MBUTTONUP ” Middle mouse button released

The first mouse message, WM_MOUSEMOVE , lets you know whenever the mouse has been moved. The remaining messages relay mouse button clicks for the left, right, and middle buttons , respectively. Keep in mind that a mouse button click consists of a button press followed by a button release; you can implement a mouse dragging feature by keeping track of when a mouse button is pressed and released, and watching for mouse movement in between.

Regardless of whether you're interested in mouse movement or a mouse button click, the important factor regarding the mouse is where the mouse cursor is located. Fortunately, the mouse cursor position is provided with all the previously mentioned mouse messages. It's packed into the lParam argument that gets sent to the GameEngine::HandleEvent() method. Following is the prototype for this method, just in case you forgot :

 LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam,   LPARAM lParam); 

If you recall, the wParam and lParam arguments are sent along with every Windows message, and contain message-specific information. In the case of the mouse messages, lParam contains the XY position of the mouse cursor packed into its low and high words. Following is an example of a code snippet that extracts the mouse position from the lParam argument in a WM_MOUSEMOVE message handler:

 case WM_MOUSEMOVE:   WORD x = LOWORD(lParam);   WORD y = HIWORD(lParam);   return 0; 

The wParam argument for the mouse messages includes information about the mouse button states, as well as some keyboard information. More specifically , wParam lets you know if any of the three mouse buttons are down, as well as whether the Shift or Control keys on the keyboard are being pressed. In case you don't see it, the wParam argument ”used in conjunction with WM_MOUSEMOVE ”provides you with enough information to implement your own Doom strafing feature! Following are the constants used with the mouse messages to interpret the value of the wParam argument:

  • MK_LBUTTON ” Left mouse button is down

  • MK_RBUTTON ” Right mouse button is down

  • MK_MBUTTON ” Middle mouse button is down

  • MK_SHIFT ” Shift key is down

  • MK_CONTROL ” Control key is down

You can check for any of these mouse constants to see if a button or key is being pressed during the mouse move. The constants are actually flags , which means that they can be combined together in the wParam argument. To check for the presence of an individual flag, you must use the bitwise AND operator ( & ) to see if the flag is present. Following is an example of checking wParam to see if the right mouse button is down:

 if (wParam & MK_RBUTTON)   // Yep, the right mouse button is down! 

This code shows how to use the bitwise AND operator ( & ) to check for individual flags. This is a technique you use in Hour 7 to check the state of a joystick.



Sams Teach Yourself Game Programming in 24 Hours
Sams Teach Yourself Game Programming in 24 Hours
ISBN: 067232461X
EAN: 2147483647
Year: 2002
Pages: 271

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net