sdljoystick.bmx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ' Copyright (c) 2015-2021 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: SDL Joystick driver
  25. End Rem
  26. Module SDL.SDLJoystick
  27. Import SDL.SDL
  28. Import Pub.Joystick
  29. Import BRL.Map
  30. Import "common.bmx"
  31. Private
  32. Global _hatPositions:Float[] = [-1, 0, 0.25, 0.125, 0.5, -1, 0.375, -1, 0.75, 0.875]
  33. Public
  34. Type TSDLJoystickDriver Extends TJoystickDriver
  35. Field joysticks:TIntMap = New TIntMap
  36. Field currentPort:Int = -1
  37. Field currentJoystick:TSDLJoystick
  38. Method GetName:String() Override
  39. Return "SDL Joystick"
  40. End Method
  41. Method JoyCount:Int() Override
  42. Return SDL_NumJoysticks()
  43. End Method
  44. Method JoyName:String(port:Int) Override
  45. Return String.FromUTF8String(SDL_JoystickNameForIndex(port))
  46. End Method
  47. Method JoyButtonCaps:Int(port:Int) Override
  48. SampleJoy port
  49. Return SDL_JoystickNumButtons(currentJoystick.joystickPtr)
  50. End Method
  51. Method JoyAxisCaps:Int(port:Int) Override
  52. SampleJoy port
  53. Return SDL_JoystickNumAxes(currentJoystick.joystickPtr)
  54. End Method
  55. Method JoyDown:Int( button:Int, port:Int=0 ) Override
  56. SampleJoy port
  57. Return SDL_JoystickGetButton(currentJoystick.joystickPtr, button)
  58. End Method
  59. Method JoyHit:Int( button:Int, port:Int=0 ) Override
  60. End Method
  61. Method JoyX#( port:Int=0 ) Override
  62. SampleJoy port
  63. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_X)/32767.0
  64. End Method
  65. Method JoyY#( port:Int=0 ) Override
  66. SampleJoy port
  67. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_Y)/32767.0
  68. End Method
  69. Method JoyZ#( port:Int=0 ) Override
  70. SampleJoy port
  71. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_Z)/32767.0
  72. End Method
  73. Method JoyR#( port:Int=0 ) Override
  74. SampleJoy port
  75. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_R)/32767.0
  76. End Method
  77. Method JoyU#( port:Int=0 ) Override
  78. SampleJoy port
  79. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_U)/32767.0
  80. End Method
  81. Method JoyV#( port:Int=0 ) Override
  82. SampleJoy port
  83. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_V)/32767.0
  84. End Method
  85. Method JoyYaw#( port:Int=0 ) Override
  86. SampleJoy port
  87. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_YAW)/32767.0
  88. End Method
  89. Method JoyPitch#( port:Int=0 ) Override
  90. SampleJoy port
  91. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_PITCH)/32767.0
  92. End Method
  93. Method JoyRoll#( port:Int=0 ) Override
  94. SampleJoy port
  95. Return SDL_JoystickGetAxis(currentJoystick.joystickPtr, JOY_ROLL)/32767.0
  96. End Method
  97. Method JoyHat#( port:Int=0 ) Override
  98. SampleJoy port
  99. Local pos:Int = SDL_JoystickGetHat(currentJoystick.joystickPtr, 0)
  100. Return _hatPositions[pos]
  101. End Method
  102. Method JoyWheel#( port:Int=0 ) Override
  103. End Method
  104. Method JoyType:Int( port:Int=0 ) Override
  105. If port<JoyCount() Return 1
  106. Return 0
  107. End Method
  108. Method JoyXDir:Int( port:Int=0 ) Override
  109. Local t#=JoyX( port )
  110. If t<.333333 Return -1
  111. If t>.333333 Return 1
  112. Return 0
  113. End Method
  114. Method JoyYDir:Int( port:Int=0 ) Override
  115. Local t#=JoyY( port )
  116. If t<.333333 Return -1
  117. If t>.333333 Return 1
  118. Return 0
  119. End Method
  120. Method JoyZDir:Int( port:Int=0 ) Override
  121. Local t#=JoyZ( port )
  122. If t<.333333 Return -1
  123. If t>.333333 Return 1
  124. Return 0
  125. End Method
  126. Method JoyUDir:Int( port:Int=0 ) Override
  127. Local t#=JoyU( port )
  128. If t<.333333 Return -1
  129. If t>.333333 Return 1
  130. Return 0
  131. End Method
  132. Method JoyVDir:Int( port:Int=0 ) Override
  133. Local t#=JoyV( port )
  134. If t<.333333 Return -1
  135. If t>.333333 Return 1
  136. Return 0
  137. End Method
  138. Method FlushJoy( port_mask:Int=~0 ) Override
  139. ' TODO ?
  140. End Method
  141. Method SampleJoy(port:Int)
  142. If currentPort = port Then
  143. Return
  144. End If
  145. Local joystick:TSDLJoystick = TSDLJoystick(joysticks.ValueForKey(port))
  146. If Not joystick Then
  147. joystick = New TSDLJoystick.Create(port)
  148. joysticks.Insert(port, joystick)
  149. End If
  150. currentJoystick = joystick
  151. currentPort = port
  152. End Method
  153. End Type
  154. Rem
  155. bbdoc: An SDL joystick instance.
  156. End Rem
  157. Type TSDLJoystick
  158. Field joystickPtr:Byte Ptr
  159. Method Create:TSDLJoystick(port:Int)
  160. joystickPtr = SDL_JoystickOpen(port)
  161. Return Self
  162. End Method
  163. Rem
  164. bbdoc: Returns whether the joystick has an LED.
  165. returns: #True, or #False if this joystick does not have a modifiable LED.
  166. End Rem
  167. Method HasLED:Int()
  168. Return SDL_JoystickHasLED(joystickPtr)
  169. End Method
  170. Rem
  171. bbdoc: Returns #True if the joystick has haptic features.
  172. End Rem
  173. Method IsHaptic:Int()
  174. Return SDL_JoystickIsHaptic(joystickPtr)
  175. End Method
  176. Rem
  177. bbdoc: Returns the battery level of this joystick.
  178. returns: One of #SDL_JOYSTICK_POWER_UNKNOWN, #SDL_JOYSTICK_POWER_EMPTY, #SDL_JOYSTICK_POWER_LOW, #SDL_JOYSTICK_POWER_MEDIUM, #SDL_JOYSTICK_POWER_FULL, or #SDL_JOYSTICK_POWER_WIRED.
  179. End Rem
  180. Method PowerLevel:Int()
  181. Return SDL_JoystickCurrentPowerLevel(joystickPtr)
  182. End Method
  183. Rem
  184. bbdoc: Starts a rumble effect.
  185. returns: 0, or -1 if rumble isn't supported on this joystick.
  186. about: Each call to this method cancels any previous rumble effect, and calling it with 0 rumble intensity stops any rumbling.
  187. End Rem
  188. Method Rumble:Int(lowFrequencyRumble:Short, highFrequencyRumble:Short, durationMs:UInt)
  189. Return SDL_JoystickRumble(joystickPtr, lowFrequencyRumble, highFrequencyRumble, durationMs)
  190. End Method
  191. Rem
  192. bbdoc: 0, or -1 if trigger rumble isn't supported on this joystick.
  193. End Rem
  194. Method RumbleTriggers:Int(leftRumble:Short, rightRumble:Short, durationMs:UInt)
  195. Return SDL_JoystickRumbleTriggers(joystickPtr, leftRumble, rightRumble, durationMs)
  196. End Method
  197. Rem
  198. bbdoc: Updates the joystick's LED color.
  199. returns: 0, or -1 if this joystick does not have a modifiable LED.
  200. End Rem
  201. Method SetLED:Int(red:Byte, green:Byte, blue:Byte)
  202. Return SDL_JoystickSetLED(joystickPtr, red, green, blue)
  203. End Method
  204. Method Delete()
  205. If joystickPtr Then
  206. SDL_JoystickClose(joystickPtr)
  207. joystickPtr = Null
  208. End If
  209. End Method
  210. End Type
  211. SDL_InitSubSystem(SDL_INIT_JOYSTICK)
  212. ' init driver
  213. New TSDLJoystickDriver
  214. ' make ourself the default
  215. GetJoystickDriver("SDL Joystick")