Platform SDK: DirectX

Step 1: Initialize the Force-Feedback Device

[C++]

This topic pertains only to application development in Visual Basic. See DirectInput C/C++ Tutorials.

[Visual Basic]

In order to implement force feedback, you must first determine whether an appropriate device is available, and if so, create a DirectInputDevice object for it. Let's presume that your application requires a device that can play effects on the x-axis and y-axis.

The following example starts by creating a DirectInput object, then using that to enumerate the available devices in a DirectInputEnumDevices object. The enumeration is restricted to attached devices of type DIDEVTYPE_JOYSTICK, since all force-feedback devices are of this type.

' dx is the global DirectX object, already initialized
' di is the global DirectInput object
 
Dim diDevInst As DirectInputDeviceInstance
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.GetDIEnumDevices(DIDEVTYPE_JOYSTICK, _
        DIEDFL_ATTACHEDONLY)

The example now iterates through the available devices till 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

Now the example enumerates the axes on the device in a DirectInputEnumDeviceObjects collection:

    Set diDevObjEnum = didev.GetDeviceObjectsEnum(DIDFT_AXIS)
    ForceX = False
    ForceY = False
 

It then goes through the axes looking for the x-axis and y-axis, and checks to see whether they support force-feedback effects:

    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
    Next iAxes
    FoundForce = ForceX And ForceY
    if FoundForce Then Exit For
  End If
Next i

If Not FoundForce Then
  MsgBox "Two force feedback axes required."
End

Next: Step 2: Set Device Properties