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

## Chapter 9
Uplinking with DirectInput and Force Feedback

> “How’s about I pin you?”

> —Pleasantville

I remember a time when I built joystick interfaces out of TTL chips so that games I wrote on my Atari 800 could support up to four players per 9-pin joystick port. Does that make me a sick person? Anyway, input devices have come a long way, an d so has the DirectX support for them. In this chapter, we’re going to take a look at DirectInput along with some general input algorithms, and I’ll throw in a taste of force feedback. Here’s what you’ll see:

  • Overview of DirectInput
  • The keyboard
  • The mouse
  • The joystick
  • Input merging
  • Force feedback
  • The input library

### The Input Loop Revisited

This is as good as time as any to review the general structure of a game and the relationship of input to the event loop. Referring to Figure 9.1, you see the generic game loop of erase, move, draw, wait, repeat. That’s all there is to a video game. Of course, this is oversimplified. In the world of Win32/DirectX, we already had to add a good amount of setup, termination, and Windows event handling code to get a Windows/DirectX application up and running. But once that’s all taken care of, basically you have erase, move, draw, wait, repeat.

Figure 9.1 A generic input loop.

The question is, where does the input go? Good question. It turns out that you could actually put the input in a number of places—at the beginning of the sequence, in the middle, at the end—but most game programmers like to put it right before the move section. That way, the last input state set by the player is acted on during the next frame.

Figure 9.2 shows a more detailed game loop with input and all the sections broken down into more detail. Remember, because you’re using the game console that you’ve been working on, you have agreed to do everything for each frame within the `Game_Main()` function. In essence, your whole world exists within this single function call.

Figure 9.2 Detailed input loop.

All right, now that I’ve refreshed your memory on where the input should be scanned or read in, let’s see how to do it with DirectInput!

### DirectInput Overture

DirectInput is basically a miracle—just like DirectDraw. Without DirectInput, you would be on the phone with every input device manufacturer in the world, begging them for drivers (one for DOS, Win16, Win32, and so on) and having a really bad day—trust me! DirectInput takes all these problems away. Of course, because it was designed by Microsoft, it creates whole new problems, but at least they are localized at one company!

DirectInput is just like DirectDraw. It’s a hardware-independent virtual input system that allows hardware manufacturers to create conventional and non-conventional input devices that all act as interfaces in the same way. This is good for you because you don’t need a driver for every single input device that your users might own. You talk to DirectInput in a generic way, and the DirectInput driver translates this code into hardware-specific calls, as does DirectDraw.

Take a look at Figure 9.3 to see an illustration of DirectInput as it relates to the hardware drivers and physical input devices.

Figure 9.3 DirectInput system-level schematic.

As you can see, you are always insulated by the HAL (Hardware Abstraction Layer). There’s not much to emulate with the HEL (Hardware Emulation Layer), so it’s not as important as it was with DirectDraw. In any case, that’s the basic idea of DirectInput. Let’s take a look at what it supports:

Every single input device that exists.

That’s pretty much the truth. As long as there is a DirectInput driver for a device, DirectInput can talk to it, and hence you can too. Of course, it’s up to the hardware vendor to write a driver, but that’s their job. With that in mind, you can expect support for the following devices:

  • Keyboards
  • Mice
  • *Joysticks
  • *Paddles
  • *Game pads
  • *Steering wheels
  • *Flight yokes
  • *Head-mounted display trackers
  • *6-DOF (degree of freedom) space balls
  • *Cybersex suits (as soon as they hit the mass market in early 2002)

The devices with asterisks are all considered to be joysticks as far as DirectInput is concerned. There are so many subclasses of joystick-like input devices that DirectInput just decided to call them all devices. Each of these devices can have one or more input objects, which might be axial, rotational, momentary, pressure, and so on. Get it? For example, a joystick with two axes (X and Y) and two momentary switches has four input objects—that’s it.

DirectInput doesn’t really care whether the device in question is a joystick because the device could represent a steering wheel just as easily. However, in reality, DirectInput does subclass a little. Anything that’s not a mouse or keyboard is a joystick-like device, whether you hold it, squeeze it, turn it, or step on it. Coolio?

DirectInput differentiates between all these devices by forcing the manufacturer (and hence the driver) to give each device a unique `GUID` to represent it. This way, at least there is a unique name of sorts for every single device that exists or will exist, and DirectInput can query the system for any device using this name. Once the device is found, however, it’s just a bunch of input objects. I’m belaboring this point because it seems to confuse people. Okay, let’s move on...

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.