home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectInput / ActionMapper / ActionMap.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  11.9 KB  |  286 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "DInput Action Mapping"
  5.    ClientHeight    =   3840
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4725
  9.    Icon            =   "ActionMap.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   256
  14.    ScaleMode       =   3  'Pixel
  15.    ScaleWidth      =   315
  16.    ShowInTaskbar   =   0   'False
  17.    StartUpPosition =   3  'Windows Default
  18.    Begin VB.Frame Frame1 
  19.       Height          =   855
  20.       Left            =   0
  21.       TabIndex        =   3
  22.       Top             =   3000
  23.       Width           =   4695
  24.       Begin VB.Label Label2 
  25.          Caption         =   "Pressing 'd' will allow you to view device configurations"
  26.          Height          =   255
  27.          Left            =   120
  28.          TabIndex        =   5
  29.          Top             =   480
  30.          Width           =   4335
  31.       End
  32.       Begin VB.Label Label1 
  33.          Caption         =   "Press escape to exit                                                                              "
  34.          Height          =   255
  35.          Left            =   120
  36.          TabIndex        =   4
  37.          Top             =   240
  38.          Width           =   4335
  39.          WordWrap        =   -1  'True
  40.       End
  41.    End
  42.    Begin VB.Label GameStateList 
  43.       Height          =   255
  44.       Index           =   0
  45.       Left            =   240
  46.       TabIndex        =   2
  47.       Top             =   360
  48.       Visible         =   0   'False
  49.       Width           =   3855
  50.    End
  51.    Begin VB.Label TSHIELD 
  52.       Caption         =   "Label1"
  53.       Height          =   375
  54.       Left            =   3720
  55.       TabIndex        =   1
  56.       Top             =   5160
  57.       Width           =   1215
  58.    End
  59.    Begin VB.Label TFIRE 
  60.       Caption         =   "Label1"
  61.       Height          =   255
  62.       Left            =   1200
  63.       TabIndex        =   0
  64.       Top             =   5160
  65.       Width           =   1095
  66.    End
  67. Attribute VB_Name = "Form1"
  68. Attribute VB_GlobalNameSpace = False
  69. Attribute VB_Creatable = False
  70. Attribute VB_PredeclaredId = True
  71. Attribute VB_Exposed = False
  72. Option Explicit
  73. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  74. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  75. '  File:       ActionMap.frm
  76. '  Content:    Use DirectInput action mapper to interpret input from many devices
  77. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  78. '-----------------------------------------------------------------------------
  79. ' App-defined game actions for the DInput action mapper.
  80. '-----------------------------------------------------------------------------
  81. ' The following constants are custom for each app, depending on what input the
  82. ' app needs. This simple sample is pretending to be a space simulator game, so
  83. ' relevant inputs are for turning left, thrusting, firing weapons, etc.. Also
  84. ' note that some constants are defines for the axes, which are for recieving
  85. ' axis data from joysticks and similiar analog devices.
  86. Const INPUT_LEFTRIGHT_AXIS = 1
  87. Const INPUT_UPDOWN_AXIS = 2
  88. Const INPUT_TURNLEFT = 4
  89. Const INPUT_TURNRIGHT = 5
  90. Const INPUT_FORWARDTHRUST = 6
  91. Const INPUT_REVERSETHRUST = 7
  92. Const INPUT_FIREWEAPONS = 8
  93. Const INPUT_ENABLESHIELD = 9
  94. Const INPUT_DISPLAYGAMEMENU = 10
  95. Const INPUT_QUITGAME = 11
  96. Const kMapGuid = "{20CAA014-60BC-4399-BDD3-84AD65A38A1C}"
  97. Const kUserName = "DInput 8 VB Sample User"
  98. Const KGenre = DIVIRTUAL_SPACESIM
  99. Dim m_mapper As New CInputMapper
  100. Private Sub DeviceList_Click(Index As Integer)
  101. End Sub
  102. Private Sub Form_Load()
  103.     Me.Show
  104.     DefineActions
  105.     Dim i As Long
  106.     For i = 1 To 15
  107.         Load Form1.GameStateList(i)
  108.         Form1.GameStateList(i).Top = i * 20
  109.         Form1.GameStateList(i).Visible = True
  110.                 
  111.     Next
  112.     If Not m_mapper.CreateDevicesFromMAP(Me.hWnd, kUserName, "Semantic Mapper VB Sample", kMapGuid, KGenre) Then
  113.         MsgBox "unable to find any mappable input devices"
  114.         End
  115.     End If
  116.     Do While InputLoop()
  117.         DoEvents
  118.     Loop
  119.     m_mapper.ClearMap
  120.     End
  121. End Sub
  122. ' The following array is the global, app-defined game actions, which map real
  123. ' device input into a semantic. The first column is the app-defined semantics
  124. ' as defined above. These are the constants the game actually sees in its
  125. ' input loop. The second column is the physical action recieved by the device
  126. ' which is to be mapped to the app-defined semantic. For instance, in the
  127. ' array below, if the user hits the "Left" key on the keyboard, the app will
  128. ' recieve an input code equal to INPUT_TURNLEFT. The last column is a text
  129. ' string that DInput uses for displaying a configuration dialog box.
  130. Sub DefineActions()
  131.     m_mapper.ClearMap
  132.     ' Device input (joystick, etc.) that is pre-defined by DInput, according
  133.     ' to genre type. The genre for this app is space simulators.
  134.     With m_mapper
  135.         .AddAction INPUT_LEFTRIGHT_AXIS, DIAXIS_SPACESIM_LATERAL, 0, "Turn"
  136.         .AddAction INPUT_UPDOWN_AXIS, DIAXIS_SPACESIM_MOVE, 0, "Move"
  137.         .AddAction INPUT_FIREWEAPONS, DIBUTTON_SPACESIM_FIRE, 0, "Shoot"
  138.         .AddAction INPUT_ENABLESHIELD, DIBUTTON_SPACESIM_GEAR, 0, "Shield"
  139.         .AddAction INPUT_DISPLAYGAMEMENU, DIBUTTON_SPACESIM_DISPLAY, 0, "Display"
  140.         .AddAction INPUT_QUITGAME, DIBUTTON_SPACESIM_MENU, 0, "Quit Game"
  141.         ' Keyboard input mappings
  142.         .AddAction INPUT_FORWARDTHRUST, DIKEYBOARD_UP, 0, "Forward thrust"
  143.         .AddAction INPUT_REVERSETHRUST, DIKEYBOARD_DOWN, 0, "Reverse thrust"
  144.         .AddAction INPUT_FIREWEAPONS, DIKEYBOARD_F, 0, "Fire weapons"
  145.         .AddAction INPUT_ENABLESHIELD, DIKEYBOARD_S, 0, "Enable shields"
  146.         .AddAction INPUT_DISPLAYGAMEMENU, DIKEYBOARD_D, 0, "Display game menu"
  147.         .AddAction INPUT_QUITGAME, DIKEYBOARD_ESCAPE, 0, "Quit game"
  148.         .AddAction INPUT_TURNRIGHT, DIKEYBOARD_RIGHT, 0, "Right Turn"
  149.         .AddAction INPUT_TURNLEFT, DIKEYBOARD_LEFT, 0, "Left Turn"
  150.         
  151.         ' Mouse input mappings
  152.         .AddAction INPUT_LEFTRIGHT_AXIS, DIMOUSE_XAXIS, 0, "Turn"
  153.         .AddAction INPUT_UPDOWN_AXIS, DIMOUSE_YAXIS, 0, "Move"
  154.         .AddAction INPUT_FIREWEAPONS, DIMOUSE_BUTTON0, 0, "Fire weapons"
  155.         .AddAction INPUT_ENABLESHIELD, DIMOUSE_BUTTON1, 0, "Enable shields"
  156.         
  157.     End With
  158. End Sub
  159. '-----------------------------------------------------------------------------
  160. ' Name: InputLoop()
  161. ' Desc: This is the input loop for the app. Input is gathered from the DInput
  162. '       devices, and output is displayed simply in the app's window.
  163. '-----------------------------------------------------------------------------
  164. Function InputLoop() As Boolean
  165.     Dim didObjData As DIDEVICEOBJECTDATA
  166.     Dim strOut As String
  167.     On Local Error Resume Next
  168.    'Static state of the app's input
  169.     Static bTurningRight       As Boolean
  170.     Static bReverseThrust      As Boolean
  171.     Static bTurningLeft        As Boolean
  172.     Static bForwardThrust      As Boolean
  173.     Static bFiringWeapons      As Boolean
  174.     Static bEnableShields      As Boolean
  175.     Static bDisplayingMenu     As Boolean
  176.     Static dwLRAxisData        As Long
  177.     Static dwUDAxisData       As Long
  178.     Dim nItems As Long
  179.     Dim i As Long, j As Long
  180.     Dim adod(10) As DIDEVICEOBJECTDATA
  181.     Static nItemsSave(20) As Long
  182.     Dim dev As DirectInputDevice8
  183.     For i = 1 To m_mapper.GetNumDevices()
  184.                 
  185.         nItems = 10
  186.         Set dev = m_mapper.GetDevice(i)
  187.         
  188.         ' Need to ensure that the devices are acquired, and pollable devices
  189.         ' are polled.
  190.         dev.Acquire
  191.         If Err.Number <> 0 Then GoTo skipDevice
  192.         
  193.         dev.Poll
  194.         If Err.Number <> 0 Then GoTo skipDevice
  195.         
  196.             
  197.         ' This call gets the data from the i'th device
  198.         nItems = 0
  199.         nItems = dev.GetDeviceData(adod, 0)
  200.         ' Get the sematics codes. The number of input events is stored in
  201.         ' "nItems", and all the events are stored in the "adod" array. Each
  202.         ' event has a type stored in "uAppDate", and actual data stored in
  203.         ' "nData".
  204.         For j = 0 To nItems - 1
  205.         
  206.             If (adod(j).lUserData = INPUT_LEFTRIGHT_AXIS) Then  ' Left-right axis
  207.                 ' Parse the left-right axis data
  208.                 dwLRAxisData = adod(j).lData
  209.                 bTurningRight = False
  210.                 bTurningLeft = False
  211.                 Debug.Print "AXIS"
  212.                 If (dwLRAxisData > 0) Then bTurningRight = True
  213.                 If (dwLRAxisData < -0) Then bTurningLeft = True
  214.             ElseIf (adod(j).lUserData = INPUT_UPDOWN_AXIS) Then   ' Up-down axis
  215.                 ' Parse the up-down axis data
  216.                 dwUDAxisData = adod(j).lData
  217.                 bReverseThrust = False
  218.                 bForwardThrust = False
  219.                 If (dwUDAxisData > 0) Then bReverseThrust = True
  220.                 If (dwUDAxisData < -0) Then bForwardThrust = True
  221.             Else
  222.                 ' Non-axis stuff
  223.             
  224.                 ' Non-axis data is recieved as "button pressed" or "button
  225.                 ' released". Parse input as such.
  226.                 Dim bState As Boolean
  227.                 If (adod(j).lData = &H80) Then bState = True
  228.                 Debug.Print "BUTTON"
  229.                 Select Case adod(j).lUserData
  230.                 
  231.                     Case INPUT_TURNLEFT:
  232.                         bTurningLeft = bState
  233.                     Case INPUT_TURNRIGHT:
  234.                         bTurningRight = bState
  235.                     Case INPUT_FORWARDTHRUST:
  236.                         bForwardThrust = bState
  237.                     Case INPUT_REVERSETHRUST:
  238.                         bReverseThrust = bState
  239.                     Case INPUT_FIREWEAPONS:
  240.                         bFiringWeapons = bState
  241.                     Case INPUT_ENABLESHIELD:
  242.                         bEnableShields = bState
  243.                     Case INPUT_DISPLAYGAMEMENU:
  244.                         bDisplayingMenu = bState
  245.                         
  246.                     Case INPUT_QUITGAME:
  247.                         InputLoop = False
  248.                         Exit Function
  249.                 End Select
  250.             End If
  251.         Next
  252.         
  253. skipDevice:
  254.         Err.Clear
  255.     Next
  256.     ' Remove conflicts (in a game, you couldn't go left and right at the same
  257.     ' time. Actual conflicts depend on the game logic, and not on the DInput
  258.     ' semantic mappings.)
  259.     If (bTurningLeft And bTurningRight) Then
  260.         bTurningLeft = False:        bTurningRight = False
  261.     End If
  262.     If (bForwardThrust And bReverseThrust) Then
  263.         bForwardThrust = False:     bReverseThrust = False
  264.     End If
  265.     If (bFiringWeapons And bEnableShields) Then bFiringWeapons = False
  266.     ' The remainder of the this function is simply to output the results of
  267.     ' gathering the input.
  268.     GameStateList(1).Caption = "Turning Left " + Str(bTurningLeft)
  269.     GameStateList(2).Caption = "Turning Right " + Str(bTurningRight)
  270.     GameStateList(3).Caption = "Forward thrust " + Str(bForwardThrust)
  271.     GameStateList(4).Caption = "Backward thrust " + Str(bReverseThrust)
  272.     GameStateList(5).Caption = "Firing Weapons " + Str(bFiringWeapons)
  273.     GameStateList(6).Caption = "Enable Shields" + Str(bEnableShields)
  274.     GameStateList(7).Caption = "LR Axis " + Str(dwLRAxisData)
  275.     GameStateList(8).Caption = "UD Axis " + Str(dwUDAxisData)
  276.     If (bDisplayingMenu) Then
  277.         
  278.         m_mapper.ConfigureDevices True
  279.         bDisplayingMenu = False
  280.     End If
  281.     InputLoop = True
  282. End Function
  283. Private Sub Form_Unload(Cancel As Integer)
  284.     End
  285. End Sub
  286.