To retrieve buffered data from the joystick, first set the buffer size (see Device Properties) and declare an array of DIDEVICEOBJECTDATA structures. This array can have up to the same number of elements as the buffer size. You do not have to retrieve the entire contents of the buffer with a single call. You can have just one element in the array and retrieve events one at a time until the buffer is empty.
After acquiring the device, you can examine and flush the buffer at any time with the IDirectInputDevice8::GetDeviceData method.(See Buffered and Immediate Data.)
Each element in the DIDEVICEOBJECTDATA array represents a change in state for a single object on the joystick. For instance, if the user presses button 0 and moves the stick diagonally, the array passed to IDirectInputDevice8::GetDeviceData (if it has at least three elements, and pdwInOut is at least 3) will have three elements filled in-an element for button 0 being pressed, an element for the change in the x-axis, and an element for the change in the y-axis-and the value of pdwInOut will be set to 3.
You can determine which object an element in the array refers to by checking the dwOfs member of the DIDEVICEOBJECTDATA structure against the following values:
Each of these values is equivalent to the offset of the data for the object in a DIJOYSTATE structure. For example, DIJOFS_BUTTON0 is equivalent to the offset of rgbButtons[0] in the DIJOYSTATE structure. You can use simple comparisons to determine which device object is associated with an item in the buffer. For example:
DIDEVICEOBJECTDATA *lpdidod; int n; . . . /* JoyBuffer is an array of DIDEVICEOBJECTDATA structures that has been set by a call to GetDeviceData. n is incremented in a loop that examines all filled elements in the array. */ lpdidod = &JoyBuffer[n]; if (( (int) lpdidod->dwOfs == DIJOFS_BUTTON0) && (lpdidod->dwData & 0x80)) { ; // Do something in response to press of primary button. }
If the data format was set with c_dfDIJoystick2, you can use the predefined offsets for all the device objects that exist in DIJOYSTATE, but you must supply your own offsets for device objects represented in the extra members of DIJOYSTATE2.
If you are using Action Mapping, ignore the value in dwOfs and instead retrieve the application-defined data associated with the event from the uAppData member.
The data for the change of state of the device object is located in the dwData member of the DIDEVICEOBJECTDATA structure. For axes, the coordinate value is returned in this member. For button objects, only the low byte of dwData is significant. The high bit of this byte is set if the button is pressed, and it is clear if the button is released.
For the other members, see Time Stamps and Sequence Numbers.