The early bird gets the worm, but the second mouse gets the cheese.
– Willie Nelson
There are two ways to communicate with the mouse: absolute mode and relative mode. In absolute mode, the mouse
returns its position relative to the screen coordinates based on where the mouse pointer is. Thus, in a screen
resolution of , the mouse position would be an element of .
In relative mode, the mouse driver sends the position of the mouse relative to its previous position at each clock tick.
Naturally, all mice are relative, it is up to the driver, or the input handler, to transform relative movement to
absolute positions.
The Mouse Cursor
When the user moves the mouse, the system moves a bitmap on the screen called the mouse cursor. The mouse cursor
contains a single-pixel point called the hot spot, a point that the system tracks and recognizes as the position of the
cursor. When a mouse event occurs, the window that contains the hot spot typically receives the mouse message resulting
from the event. The window need not be active or have the keyboard focus to receive a mouse message.
To facilitate things a bit, we will start the game in full-screen mode from now on, which is the most natural mode for
games anyway.
to set the mouse position to be at the center of our game window when our application starts:
To retrieve the position of the cursor, the
GetCursorPos function can be used:
The function returns the position of the mouse cursor (in absolute coordinates) in
a Point structure:
As an example, let us print the position of the cursor to the screen. To do so, we need to create a text format:
Now during the game loop, we simply query for the position of the mouse cursor and create a text format layout with the
x and y-coordinate of the mouse cursor:
Printing the text is effortless now:
Sprite Cursor
The standard cursor doesn’t look great, and thus we will replace it with our own using our animated sprite class.
First, we have to disable the standard cursor using the
ShowCursor function:
The only input to this function is a boolean specifying whether to show the cursor or not:
To use a custom cursor, we simply draw the animated sprite at the cursor position:
Mouse Buttons
To keep track of the state of the mouse buttons, we can use the same routine as for the keyboard. Windows can natively
track five mouse buttons and actually, our keyboard update routine includes those five buttons already.
To test this, let us print the mouse position only while the right mouse button is pressed. To do so, we simply add a
command to the enumeration of game commands, set the default map for that command to the right mouse button being held
down and act upon activation accordingly:
Now let us animate our cursor. We add another game command to the enumeration called leftMouseButtonDown which changes
the mouse cursor animation for as long as the left mouse button is down and, to make things a bit more interesting, it
will allow us to define a rectangle (this might be useful for troop selection in an RTS). To do so, we add events to
look for the left mouse button to just be pressed and released respectively, to save the starting and end point of the
rectangle:
Cleanup
I cleaned the source code and eliminated a few memory leaks. The following key bindings are active on default: