home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter11 / JoystickTest / Form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  2004-10-22  |  7.1 KB  |  211 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    AutoRedraw      =   -1  'True
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   3675
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4860
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   245
  14.    ScaleMode       =   3  'Pixel
  15.    ScaleWidth      =   324
  16.    StartUpPosition =   2  'CenterScreen
  17. Attribute VB_Name = "Form1"
  18. Attribute VB_GlobalNameSpace = False
  19. Attribute VB_Creatable = False
  20. Attribute VB_PredeclaredId = True
  21. Attribute VB_Exposed = False
  22. '----------------------------------------------------------------------
  23. ' Visual Basic Game Programming For Teens
  24. ' JoystickTest Program
  25. '----------------------------------------------------------------------
  26. Option Explicit
  27. Option Base 0
  28. Implements DirectXEvent8
  29. 'DirectX objects and structures
  30. Dim dx As New DirectX8
  31. Dim di As DirectInput8
  32. Dim diDev As DirectInputDevice8
  33. Dim diDevEnum As DirectInputEnumDevices8
  34. Dim joyCaps As DIDEVCAPS
  35. 'keep track of analog stick motion
  36. Dim Analog(1 To 2) As D3DVECTOR
  37. 'program variables
  38. Dim EventHandle As Long
  39. Private Sub Form_Load()
  40.     On Local Error Resume Next
  41.     'create the DirectInput object
  42.     Set di = dx.DirectInputCreate()
  43.     If Err.Number <> 0 Then
  44.         MsgBox "Error creating DirectInput object"
  45.         Shutdown
  46.     End If
  47.     'enumerate the game controllers
  48.     Set diDevEnum = di.GetDIDevices(DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY)
  49.     If Err.Number <> 0 Then
  50.         MsgBox "Error enumerating game controllers"
  51.         Shutdown
  52.     End If
  53.     'check for the presence of a joystick
  54.     If diDevEnum.GetCount = 0 Then
  55.         MsgBox "No joystick could be found"
  56.         Shutdown
  57.     End If
  58.     'initialize the joystick
  59.     Joystick_Init
  60.     'main polling loop
  61.     Do While True
  62.         diDev.Poll
  63.         DoEvents
  64.     Loop
  65. End Sub
  66. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  67.     If KeyCode = 27 Then Shutdown
  68. End Sub
  69. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  70.     Shutdown
  71. End Sub
  72. Private Sub Shutdown()
  73.     If EventHandle <> 0 Then
  74.         dx.DestroyEvent EventHandle
  75.     End If
  76.     If Not (diDev Is Nothing) Then diDev.Unacquire
  77.     Set diDev = Nothing
  78.     Set di = Nothing
  79.     Set dx = Nothing
  80.     End
  81. End Sub
  82. Private Sub Joystick_Init()
  83.     On Local Error Resume Next
  84.     'see if joystick was already acquired
  85.     If Not diDev Is Nothing Then
  86.       diDev.Unacquire
  87.     End If
  88.     'create the joystick object
  89.     Set diDev = Nothing
  90.     Set diDev = di.CreateDevice(diDevEnum.GetItem(1).GetGuidInstance)
  91.     diDev.SetCommonDataFormat DIFORMAT_JOYSTICK
  92.     diDev.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
  93.     'create an event handler for the joystick
  94.     EventHandle = dx.CreateEvent(Me)
  95.     'ask for notification of events
  96.     diDev.SetEventNotification EventHandle
  97.     'set the analog response range
  98.     SetAnalogRanges -1000, 1000
  99.     'acquire joystick for exclusive use
  100.     diDev.Acquire
  101.     'manually poll joystick first time
  102.     DirectXEvent8_DXCallback 0
  103.     'retrieve joystick information
  104.     diDev.GetCapabilities joyCaps
  105.     'display information about the joystick
  106.     Debug.Print diDevEnum.GetItem(1).GetInstanceName
  107.     Debug.Print "Number of axes: " & joyCaps.lAxes
  108.     Debug.Print "Number of buttons: " & joyCaps.lButtons
  109.     Debug.Print "Device type: " & joyCaps.lDevType
  110.     Debug.Print "Driver version: " & joyCaps.lDriverVersion
  111.     Debug.Print "Time resolution: " & joyCaps.lFFMinTimeResolution
  112.     Debug.Print "Sample period: " & joyCaps.lFFSamplePeriod
  113.     Debug.Print "Firware revision: " & joyCaps.lFirmwareRevision
  114.     Debug.Print "Hardware revision: " & joyCaps.lHardwareRevision
  115.     Debug.Print "Number of POVs: " & joyCaps.lPOVs
  116. End Sub
  117. Private Sub SetAnalogRanges(ByVal lMin As Long, ByVal lMax As Long)
  118.     Dim DiProp_Dead As DIPROPLONG
  119.     Dim DiProp_Range As DIPROPRANGE
  120.     Dim DiProp_Saturation As DIPROPLONG
  121.     On Local Error Resume Next
  122.     'set range for all axes
  123.     With DiProp_Range
  124.         .lHow = DIPH_DEVICE
  125.         .lMin = lMin
  126.         .lMax = lMax
  127.     End With
  128.     'set the property
  129.     diDev.SetProperty "DIPROP_RANGE", DiProp_Range
  130.     'set deadzone for X and Y axes to 5 percent
  131.     With DiProp_Dead
  132.         .lData = (lMax - lMin) / 5
  133.         .lHow = DIPH_BYOFFSET
  134.         .lObj = DIJOFS_X
  135.         diDev.SetProperty "DIPROP_DEADZONE", DiProp_Dead
  136.         .lObj = DIJOFS_Y
  137.         diDev.SetProperty "DIPROP_DEADZONE", DiProp_Dead
  138.     End With
  139.     'set saturation zone for X and Y axes to 95 percent
  140.     With DiProp_Saturation
  141.         .lData = (lMax - lMin) * 0.95
  142.         .lHow = DIPH_BYOFFSET
  143.         .lObj = DIJOFS_X
  144.          diDev.SetProperty "DIPROP_SATURATION", DiProp_Saturation
  145.         .lObj = DIJOFS_Y
  146.          diDev.SetProperty "DIPROP_SATURATION", DiProp_Saturation
  147.     End With
  148. End Sub
  149. Private Sub Joystick_AnalogMove(ByVal lNum As Long, ByRef vAnalog As D3DVECTOR)
  150.     Debug.Print "Analog stick " & lNum & " = " & _
  151.         vAnalog.x & "," & vAnalog.y & "," & vAnalog.z
  152. End Sub
  153. Private Sub Joystick_SliderMove(ByVal lSlider As Long, ByVal lValue As Long)
  154.     Debug.Print "Slider " & lSlider & " = " & lValue
  155. End Sub
  156. Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  157.     Static Analog1 As D3DVECTOR
  158.     Static Analog2 As D3DVECTOR
  159.     Dim js As DIJOYSTATE
  160.     Dim n As Long
  161.     On Local Error Resume Next
  162.     'retrieve joystick status
  163.     diDev.GetDeviceStateJoystick js
  164.     If Err.Number = DIERR_NOTACQUIRED Or Err.Number = DIERR_INPUTLOST Then
  165.         diDev.Acquire
  166.         Exit Sub
  167.     End If
  168.     'fire off any joystick analog movement events
  169.     For n = 1 To 8
  170.         Select Case n
  171.             Case 1
  172.                 Analog1.x = js.x
  173.                 Joystick_AnalogMove 1, Analog1
  174.             Case 2
  175.                 Analog1.y = js.y
  176.                 Joystick_AnalogMove 1, Analog1
  177.             Case 3
  178.                 Analog1.z = js.z
  179.                 Joystick_AnalogMove 1, Analog1
  180.             Case 4
  181.                 Analog2.x = js.rx
  182.                 Joystick_AnalogMove 2, Analog2
  183.             Case 5
  184.                 Analog2.y = js.ry
  185.                 Joystick_AnalogMove 2, Analog2
  186.             Case 6
  187.                 Analog2.z = js.rz
  188.                 Joystick_AnalogMove 2, Analog2
  189.             Case 7
  190.                 Joystick_SliderMove 1, js.slider(0)
  191.             Case 8
  192.                 Joystick_SliderMove 2, js.slider(1)
  193.         End Select
  194.      Next n
  195.     'fire off any button events
  196.     For n = 0 To joyCaps.lButtons - 1
  197.         If js.Buttons(n) = 0 Then
  198.             Debug.Print "Joystick ButtonUp: " & n
  199.         Else
  200.             Debug.Print "Joystick ButtonDown: " & n
  201.         End If
  202.     Next n
  203.         
  204.     'fire off any direction-pad button events
  205.     If js.POV(0) = -1 Then
  206.         Debug.Print "DPAD: -1"
  207.     Else
  208.         Debug.Print "DPAD: " & js.POV(0) / 4500
  209.     End If
  210. End Sub
  211.