Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
109 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 The Basics
 The Mouse
 Using DirectInput
 Drawing the Mouse
 Stay Tuned...

 Printable version

 


  The Series

 Part I
 Part II
 Part III
 Part IV

 

Initializing DirectInput

Before we go any further with CMouse, let's look at the code to initialize DirectInput. Note that this code doesn't belong in our CMouse::Init() routine; the DirectInput pointer is used by the entire game, not just the mouse, so the code that inits DirectInput should go in your main init function – the same time you init DirectDraw, DirectSound, etc. A DirectInput interface pointer is different than a DirectInput device pointer; you use DirectInput pointers to get DirectInputDevice pointers. Make sure you understand this distinction. Here's the code to initialize the master DirectInput interface pointer:

Popup : Source Listing 2

That code does three important things. First, it gets a valid DirectInput mouse device interface, and puts it in di_mouse. Next, it sets the data format and the cooperative level for the device, basically letting windows know that we want to query the device as a mouse, and that we don't want to take exclusive ownership of it. (Exclusive ownership means that we're the only app that can use the mouse – by specifying DISCL_NONEXCLUSIVE, we've told Windows that we're going to be sharing the mouse with other applications.)

Polling DirectInput for Mouse Status

Now let's flesh out CMouse::Refresh(), the function responsible for updating the CMouse's internal button state and position. Here's the code:

Popup : Source Listing 3

That code's doing a lot of things. First, it queries DirectInput for the new absolute mouse position (there's a while loop in there that will automatically retry the query if we've lost the interface). Next, it squirrels the absolute position data away in m_absposition, then it "applies" the relative position to come up with the new absolute position. The ConstrainPosToScreenSize() makes sure the point is within the bounds of the screen. Finally, it loops through and refreshes all the buttons.


Next : Drawing the Mouse