Step 1: Enumerating the Joysticks

After creating the DirectInput system, call the IDirectInput::EnumDevices method to enumerate the joysticks. The following code from Input.c in the Space Donuts source directory accomplishes this.

// LPDIRECTINPUT pdi; // previously initialized

pdi->lpVtbl->EnumDevices(pdi, DIDEVTYPE_JOYSTICK,

InitJoystickInput, pdi, DIEDFL_ATTACHEDONLY);

The method call is in the C form. Note that you could use the IDirectInput_EnumDevices macro to simplify the call. All DirectInput methods have corresponding macros defined in Dinput.h that expand to the appropriate C or C++ syntax.

The DIDEVTYPE_JOYSTICK constant, passed as the second parameter, specifies the type of device to be enumerated.

InitJoystickInput is the address of a callback function to be called each time a joystick is found; this is where the individual devices will be initialized in the following steps of the tutorial.

The fourth parameter can be any 32-bit value that you want to make available to the callback function. In this case it's a pointer to the DirectInput interface, which the callback function needs to know so it can call IDirectInput::CreateDevice.

The last parameter, DIEDFL_ATTACHEDONLY, is a flag that restricts enumeration to devices that are attached to the computer.