DirectX SDK |
Setting the data format for a device is an essential step before you can acquire and begin using the device. This is true even if you do not intend to retrieve immediate (state) data from the device. DirectInput uses the data format in many methods to identify particular device objects.
The IDirectInputDevice7::SetDataFormat method tells DirectInput what device objects will be used and how the data will be arranged.
The examples in the reference for the DIDATAFORMAT and DIOBJECTDATAFORMAT structures show how to set up custom data formats for nonstandard devices. Fortunately, this step is not necessary for the joystick, keyboard, and mouse. DirectInput provides five global variables, c_dfDIJoystick, c_dfDIJoystick2, c_dfDIKeyboard, c_dfDIMouse, and c_dfDIMouse2, which can be passed in to SetDataFormat to create a standard data format for these devices.
In the following code example, lpdiMouse is an initialized pointer to the mouse DirectInputDevice object:
lpdiMouse->SetDataFormat(&c_dfDIMouse);
Note You cannot change the dwFlags member in the predefined DIDATAFORMAT global variables (for example, to change the property of an axis), because they are const variables. To change properties, use the IDirectInputDevice7::SetProperty method after setting the data format, but before acquiring the device.
The DirectInputDevice.SetCommonDataFormat and DirectInputDevice.SetDataFormat methods tell DirectInput what device objects will be used and how the data will be arranged.
For standard devices—the mouse, keyboard, and any game controller whose input data can be described in a DIJOYSTATE or DIJOYSTATE2 type—you can set the data format by calling the SetCommonDataFormat method, passing in a constant from the CONST_DICOMMONDATAFORMATS enumeration. The common data formats are adequate for most applications.
For specialized devices, you must pass a description of the data format to the SetDataFormat method. The following code example sets the data format for a device with two axes, both of which require a Long for their data, and no buttons:
Dim dx As New DirectX7 Dim di As DirectInput Dim did As DirectInputDevice Dim fD As DIDATAFORMAT Dim fDA(1) As DIOBJECTDATAFORMAT Private Sub Form_Load() Set di = dx.DirectInputCreate() Set did = di.CreateDevice("GUID_SysMouse") fDA(0).lFlags = DIDOI_POLLED fDA(0).lOfs = 0 fDA(0).lType = DIDFT_RELAXIS fDA(0).strGuid = "GUID_XAxis" fDA(1).lFlags = DIDOI_POLLED fDA(1).lOfs = 4 fDA(1).lType = DIDFT_RELAXIS fDA(1).strGuid = "GUID_YAxis" fD.dataSize = 8 fD.lFlags = DIDF_RELAXIS fD.lObjSize = 4 fD.numObjs = 2 did.SetDataFormat fD, fDA() End Sub