Step 3: Setting the Joystick Data Format

Now that the application has a pointer to a DirectInput device, it can call the IDirectInputDevice methods to manipulate that device. The first step, which is an essential one, is to set the data format for the joystick. This step tells DirectInput how to format the input data.

The Space Donuts sample performs this action inside the callback function introduced in the previous step.

if (pdev->lpVtbl->SetDataFormat(pdev, &c_dfDIJoystick) != DI_OK) 
   { 
      OutputDebugString("IDirectInputDevice::SetDataFormat FAILED\n"); 
      pdev->lpVtbl->Release(pdev); 
      return DIENUM_CONTINUE; 
   } 
 

The pdev variable is a pointer to the device interface created by IDirectInput::CreateDevice.

The IDirectInputDevice::SetDataFormat method takes two parameters. The first is a this pointer to the calling instance of the interface and is unnecessary in C++. The second is a pointer to a DIDATAFORMAT structure containing information about how the data for the device is to be formatted. For the joystick, the predefined global variable c_dfDIJoystick can be used here.

As in the previous step, the callback function returns DIENUM_CONTINUE if it fails to initialize the device. This flag instructs DirectInput to keep enumerating as long as there are devices to be enumerated.