The idea behind this juiced up approach to
The idea behind this juiced up approach to keyboard event handling is that instead of sitting around waiting for Windows to eventually send you a keyboard message, you constantly check the keyboard to see if any of the keys you’re interested in have been pressed. If so, the game instantly springs into motion and efficiently responds to the key press. If no key is pressed, the game engine hums along with no interruptions. The strategy here is to basically take keyboard processing into your own hands and bypass the standard Windows approach, which ultimately results in much more responsive keyboard controls for your games. You learn how to implement this speedy keyboard handler in code a little later in the hour. For now, let’s take a look at how to respond to mouse events. 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
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services