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

#### Data Acquisition Modes

Last, but not least, I want to briefly alert you to the existence of immediate and buffered data modes. DirectInput can send you immediate state information or buffer input, time-stamped in a message format. I haven’t had much use for the buffered input model, but it’s there if you want to use it (read the DirectX SDK if you’re interested). We’ll use the immediate mode of data acquisition, which is the default.

#### Creating the Main DirectInput Object

Now let’s see how to create the main DirectInput COM object `IDirectInput`. Then we’ll take a look at how to work with the keyboard, mouse, and joystick.

The interface pointer to the main DirectInput object is defined in `DINPUT.H` as follows:

    LPDIRECTINPUT lpdi; // main directinput interface

To create the main COM interface, use the standalone function `DirectInputCreate()`, shown here:

    HRESULT WINAPI DirectInputCreate(

     HINSTANCE hinst, // the main instance of the app

     DWORD dwVersion, // the version of directinput you want

     LPDIRECTINPUT * lplpDirectInput,  // ptr to storage

                                       // for interface ptr

     LPUNKNOWN punkOuter);  // COM stuff, always NULL

The parameters are as follows:

  `hinst` is the instance handle of your application. This is one of the few function calls that needs this handle. It’s the same handle that is passed into `WinMain()` at the start of your application, so just save it in a global and stuff it in here.
  `dwVersion` is a constant that describes which version of DirectInput you want to be compatible with. If you assume that some of your game will be played on DirectX 3.0 machines, this will be of concern, but just send `DIRECTINPUT_VERSION` for the latest version of DirectInput and that will do.
  `lplpDirectInput` is the address of the interface pointer that will receive the COM interface to DirectInput.
  And lastly, `punkOuter` is for COM aggregation and is not of concern. Set it to `NULL`.

`DirectInputCreate()` returns `DI_OK` (DirectInput OK) if successful, or something else if not. As usual, though, we are going to use the macros `SUCCESS()` and `FAILURE()` rather than test for `DI_OK` because it’s now the preferred method under DirectX to test for a problem. But it’s pretty safe to use `DI_OK` if you want.

Here’s an example of creating the main DirectInput object:

    #include "DINPUT.H" // need this and DINPUT.LIB

    // the rest of your includes, defines etc.

    // globals...

    LPDIRECTINPUT lpdi = NULL; // used to point to com interface

    // create the main DirectInput object

    if (FAILED(DirectInputCreate(main_instance,

                                 DIRECTINPUT_VERSION,

                                 &lpdi, NULL))

> Note: It’s very important that you include `DINPUT.H` and `DINPUT.LIB` in your application, or else the compiler and linker won’t know what to do. Also, if you’ve never read my instructions on compiling, please include the `.LIB` file directly in the application project. Setting a search path in the library search settings is usually not enough.

       { /* error */ }

And that’s that. If the function was successful, at this point you’ll have a pointer to the main DirectInput object that you can then use to create devices.

As with all COM objects, when your application is complete and you are releasing resources, you must make a call to `Release()` to decrement the reference count of the COM object. Here’s how:

    // the shutdown

    lpdi->Release();

And if you want to be technical:

    // the shutdown

    if (lpdi)

      lpdi->Release();

Of course, you would do this after releasing the devices created. Remember always to make your calls to `Release()` in the reverse order you created your objects, like unwinding a stack.

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.