| Microsoft DirectX 8.1 (Visual Basic) |
In order to implement force feedback, you must first determine whether an appropriate device is available, and if so, create a DirectInputDevice8 object for it. This tutorial presumes an application requiring a device that can play effects on both the x-axis and y-axis.
The following example starts, as usual, by creating a DirectInput8 object, then using that object to enumerate the available devices into a DirectInputEnumDevices8 object. The enumeration is restricted to attached devices of type DI8DEVTYPE_JOYSTICK.
Dim dx As DirectX8
Dim di As DirectInput8
Dim diDev As DirectInputDevice8
Dim diEnumDev As DirectInputEnumDevices8
Dim diDevInst As DirectInputDeviceInstance8
Dim diDevObjEnum As DirectInputEnumDeviceObjects
Dim devobj As DirectInputDeviceObjectInstance
Dim devcaps As DIDEVCAPS
Dim ForceX As Boolean, ForceY As Boolean
Dim FoundForce As Boolean
Dim efftype As Long
Dim strObjGuid As String
Dim i As Integer, iAxes As Integer
Set di = dx.DirectInputCreate
Set diEnumDev = di.GetDIDevices(DI8DEVTYPE_JOYSTICK, _
DIEDFL_ATTACHEDONLY)
The example now iterates through the available devices until it finds one that has force-feedback capabilities.
For i = 1 To diEnumDev.GetCount
Set diDevInst = diEnumDev.GetItem(i)
Set diDev = di.CreateDevice(diDevInst.GetGuidInstance)
Call diDev.GetCapabilities(devcaps)
If devcaps.lFlags And DIDC_FORCEFEEDBACK Then
'Process the force-feedback device
Next, because the example requires that both the X and Y axes must support force feedback, all axes on the device are enumerated in a DirectInputEnumDeviceObjects collection.
Set diDevObjEnum = didev.GetDeviceObjectsEnum(DIDFT_AXIS)
The collection of axes present is then inspected, looking for the x-axis and y-axis, and checking to see whether they support force-feedback effects.
ForceX = False
ForceY = False
For iAxes = 1 To diDevObjEnum.GetCount
Set devobj = diDevObjEnum.GetItem(iAxes)
strObjGuid = devobj.GetGuidType
If strObjGuid = "GUID_XAxis" Then
If devobj.GetFlags And DIDOI_FFACTUATOR Then
ForceX = True
End If
ElseIf strObjGuid = "GUID_YAxis" Then
If devobj.GetFlags And DIDOI_FFACTUATOR Then
ForceY = True
End If
End If
FoundForce = ForceX And ForceY
If FoundForce Then Exit For
Next iAxes
Next i 'Next device
If Not FoundForce Then
MsgBox "Two force feedback axes required."
End If
Now that a suitable device is assured, its properties are set in Step 2: Setting Device Properties.