DirectX SDK

Step 3: Set the Joystick Data Format

[Visual Basic]

The information in this topic pertains only to applications written in C++. See DirectInput Visual Basic Tutorials.

[C++]

Now that the application has a pointer to a DirectInput device, it can call the IDirectInputDevice7 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("IDirectInputDevice7::SetDataFormat FAILED\n"); 
      pdev->lpVtbl->Release(pdev); 
      return DIENUM_CONTINUE; 
   } 
 

The pdev variable is a pointer to the device interface created by IDirectInput7::CreateDeviceEx.

The IDirectInputDevice7::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.

Next: Step 4: Set the Joystick Behavior