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

#### The 101-Key Control Pad

Because setting up one DirectInput device is similar to setting up all other devices, I’m going to really go into detail with the keyboard and then speed things up with the mouse and joystick. So make sure to read this section carefully, because it will be applicable to the other devices as well.

Creating the Keyboard Device

The first step in getting any device up and running is creating it with a call to `IDIRECTINPUT::CreateDevice()`. Remember, this function basically gives you an interface to the particular device that you requested (the keyboard in this first example), which you can then work with. Let’s take a look at the function:

    HRESULT CreateDevice(

     REFGUID rguid, // the GUID of the device to create

     LPDIRECTINPUTDEVICE *lplpDirectInputDevice,  // ptr to the

                                                  // IDIRECTINPUTDEVICE

                                                  // interface to receive ptr

     LPUNKNOWN pUnkOuter); // COM stuff, always NULL

Simple enough, huh, baby? The first parameter, `rguid`, is the `GUID` of the device you want to create. You can either query for the `GUID` of interest or use one of the defaults for the most common devices:

  `GUID_SysKeyboard`—The keyboard.

  > Warning: Danger, Will Robinson! Remember, these are in `DINPUT.H`, so that must be included along with `DINPUT.LIB`. Furthermore, for all this `GUID` stuff, you should also place a `#define INITGUID` at the top of your application before all other includes (but only once), as well as including the header `OBJBASE.H` with your application. You can also include the `.LIB` file `DXGUID.LIB` with your program, but `OBJBASE.H` is preferred. In any case, you can always look at the chapter demos on the CD and see a working example of what to include and what not to include; it’s just one of the stupid details.

  `GUID_SysMouse`—The mouse.

The second parameter is the receiver of the new interface, and of course the last is just `NULL`. The function returns `DI_OK` if successful and something else if not.

All right, based on your new-found knowledge of `CreateDevice()`, let’s see if you can create the keyboard device. The first thing you need is a variable to hold the interface pointer that will be created during the call. All devices are of type `IDIRECTINPUTDEVICE` or `IDIRECTINPUTDEVICE2`, but the keyboard isn’t fancy, so you can get away with version 1.0 of the interface:

    IDIRECTINPUTDEVICE lpdikey = NULL; // ptr to keyboard device

Now, let’s create the device with a call to `CreateDevice()` from the main COM object. Here’s all the code, including the creation of the main COM object and all necessary inclusion/defines:

    // this needs to come first

    #define INITGUID

    // includes

    #include <OBJBASE.H> // need this one for GUIDS

    #include "DINPUT.H"  // need this for directinput and

                               // DINPUT.LIB

    // globals...

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

    IDIRECTINPUTDEVICE lpdikey = NULL; // ptr to keyboard device

    // create the main DirectInput object

    if (FAILED(DirectInputCreate(main_instance,

                                 DIRECTINPUT_VERSION,

                                 &lpdi, NULL))

       { /* error */ }

    // now create the keyboard device

    if (FAILED(lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL)))

       { /* error */ }

    // do all the other stuff....

At this point, `lpdikey` points to the keyboard device, and you can call methods of the interface to set the cooperation level, data format, and so on. Of course, when you’re done with the device, you release it with a call to `Release()`. However, this call will be before you’ve released the main DirectInput object `lpdi`, so put something like this in your shutdown:

    // release all devices

    if (lpdikey)

       lpdikey->Release();

    // .. more device releases, joystick, mouse etc.

    // now release main COM object

    if (lpdi)

       lpdi->Release();

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.