Initializing the Mouse

Assuming the DirectInput object is already initialized, the next step is to set the data format for the mouse, which instructs DirectInput how to pass the data back to your program.

Setting the Data Format

We’ll use the SetDataFormat function for the mouse as well as for the keyboard. The single parameter to this function specifies the device type, which for the mouse should be the predefined c_dfDIMouse. Here is an example:

HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse);

Note, again, that you do not need to define c_dfDIMouse, because it is defined in dinput.h.

Setting the Cooperative Level

The next step (again, like the keyboard interface) is to set the cooperative level, which determines how much priority over the mouse DirectInput will give your program. To set the cooperative level, you call the SetCooperativeLevel function with the window handle and the mouse priority. Common values are DISCL_EXCLUSIVE and DISCL_FOREGROUND (which has the added benefit of hiding the stock Windows cursor from view). If your game is running full screen, then you may consider gaining exclusive access to the input devices. I often use nonexclusive mode in my code. Here is an example:

HRESULT result = dimouse->SetCooperativeLevel( hwnd,
    DISCL_EXCLUSIVE | DISCL_FOREGROUND);

Acquiring the Device

The last step is to acquire the mouse device using the Acquire function. If the function returns DI_OK, then you have successfully acquired the mouse and you are ready to start checking for movement and button presses. As with the keyboard device, you must unacquire the mouse and release the mouse device after you are finished using it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset