Tricks of the Windows Game Programming Gurus:Uplinking with DirectInput and Force Feedback

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Reading the Data from the Mouse

At this point, you have created the mouse device, set the cooperation level and the data format, and acquired it. Now you’re ready to shake that booty. To make the shake happen, you need to read the data from the mouse with `GetDeviceState()`. However, you must send the correct parameters based on the new data format, `c_dfDIMouse`, and the data structure the data will be placed in, `DIMOUSESTATE`. Here’s how you read the mouse:

    DIMOUSESTATE mousestate; // this holds the mouse data

Notice how smart the function is. Instead of having multiple functions, the function uses a size and `ptr` to work with any data format that exists now or that you might think of later. This is a good programming technique to remember, young Jedi.

    // .. somewhere in your main loop

    // read the mouse state

    if (FAILED(lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE),

             (LPVOID)mousestate)))

           { /* error */ }

Now that you have the mouse data, let’s work with it. Imagine that you want to move an object around based on the motion of the mouse. If the player moves the mouse left, you want the object to move left by the same amount. In addition, if the user presses the left mouse button, it should fire a missile, and the right button should exit the program. Here’s the main code:

    // obviously you need to do all the other steps...

    // defines

    #define MOUSE_LEFT_BUTTON   0

    #define MOUSE_RIGHT_BUTTON  1

    #define MOUSE_MIDDLE_BUTTON 2 // (most of the time)

    // globals

    DIMOUSESTATE mousestate; // this holds the mouse data

    int object_x = SCREEN_CENTER_X, // place object at center

        object_y = SCREEN_CENTER_Y;

    // .. somewhere in your main loop

    // read the mouse state

    if (FAILED(lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE),

             (LPVOID)mousestate)))

           { /* error */ }

    // move object

    object_x += mousestate.lX;

    object_y += mousestate.lY;

    // test for buttons

    if (mousestate.rgbButtons[MOUSE_LEFT_BUTTON] & 0x80)

        { /* fire weapon */ }

    else

    if (mousestate.rgbButtons[MOUSE_RIGHT_BUTTON] & 0x80)

        { /* send exit message */ }

Releasing the Mouse from Service

When you’re done with the mouse, you need to first unacquire it with a call to `Unacquire()` and then release the device as usual. Here’s the code:

    // unacquire mouse

    if (lpdimouse)

       lpdimouse->Unacquire();

    // release the mouse

    if (lpdimouse)

       lpdimouse->Release();

As an example of working with the mouse, I have created a little demo called `DEMO9_2.CPP|EXE`. As before, you need to link in `DDRAW.LIB`, `DINPUT.LIB`, and `WINMM.LIB` (for VC++ users), along with `T3DLIB1.CPP`. Figure 9.8 shows a screen shot of the program in action.

Figure 9.8 `DEMO9_2.EXE` in action.

#### Working the Joystick

The joystick is probably the most complex of all DirectInput devices. The term joystick really encompasses all possible devices other than the mouse and keyboard. However, to keep things manageable, I’m going to primarily focus on devices that look like a joystick or game paddle, such as the Microsoft Sidewinder, Microsoft Gamepad, Gravis Flight Stick, and so forth.

Before we get into this, take a look at Figure 9.9. Here you see a joystick and a control pad. Both devices are considered to be joysticks under DirectInput. The onlyjoystick-like device that has a class of its own is the force feedback device, but I’ll get to that later.

Figure 9.9 DirectInput devices are collections of device objects.

Anyway, the point that I want to make about the joystick and gamepad is that they’re both the same thing as far as DirectInput is concerned. They are both a collection of axes, switches, and sliders. It’s just that the axes on the joystick have many positions (they are continuous), and the gamepad has clamped or extreme positions. The point is that each device is a collection of device objects or device things or input objects, depending on your terminology and what reference you use. They’re all just input devices that happen to be on the same physical piece of hardware. Get it? I hope so. :)

The steps for setting up a joystick-like device are the same as for the keyboard and mouse, except that there are a couple of added steps. Let’s take a look:

  1. Create the joystick device with `CreateDevice()`.
  2. Use `QueryInterface()` to acquire an interface pointer to the `IDIRECTINPUTDEVICE2` interface (which you’ll need for joysticks). This step is new, of course.
  3. Set the cooperation level with `SetCooperativeLevel()`.
  4. Set the data format with `SetDataFormat()`.
  5. Set the joystick range, dead zone, and other properties with `SetProperties()`. This step is new.
  6. Acquire the joystick with `Acquire()`.
  7. Poll the joystick with the `Poll()` function. This step basically makes sure that joysticks without interrupt drivers have valid data when `GetDeviceState()` is called. This step is new.
  8. Read the mouse state with `GetDeviceState()`.
  9. Repeat step 8 until done.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.