freejoy.bmx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. Rem
  2. bbdoc: User input/Joystick
  3. End Rem
  4. Module Pub.FreeJoy
  5. ModuleInfo "Version: 1.08"
  6. ModuleInfo "Author: Simon Armstrong"
  7. ModuleInfo "License: zlib/libpng"
  8. ModuleInfo "Copyright: Blitz Research Ltd"
  9. ModuleInfo "Modserver: BRL"
  10. ModuleInfo "History: 1.08 Release"
  11. ModuleInfo "History: Added JoyHit samplejoy fix, thanks to HamishTheHystericalHamster"
  12. ModuleInfo "History: 1.07 Release"
  13. ModuleInfo "History: Added MacOSX Rx,Ry,Rz (JoyR,JoyU,JoyV) and Wheel"
  14. ModuleInfo "History: 1.06 Release"
  15. ModuleInfo "History: Enabled Apple Gamepad and MultiAxis HID classes"
  16. ModuleInfo "History: 1.05 Release"
  17. ModuleInfo "History: Fixed Linux C Compiler warnings"
  18. ModuleInfo "History: 1.04 Release"
  19. ModuleInfo "History: Fixed C Compiler warnings"
  20. ?MacOS
  21. Import "freejoy.macosx.c"
  22. Import "-framework IOKit"
  23. ?Win32
  24. Import "freejoy.win32.c"
  25. ?Linux
  26. Import "freejoy.linux.c"
  27. ?
  28. Extern
  29. Rem
  30. bbdoc: Counts the number of joysticks.
  31. returns: The number of joysticks and gamecontrollers connected to the system.
  32. end rem
  33. Function JoyCount()
  34. Function JoyCName:Byte Ptr(port)
  35. Rem
  36. bbdoc: Available buttons (on/off controls) on a joystick.
  37. returns: A bitfield representing which buttons are present.
  38. end rem
  39. Function JoyButtonCaps(port)
  40. Rem
  41. bbdoc: Available axis (proportional controls) on a joystick.
  42. returns: A bitfield representing which axis are available.
  43. about:
  44. The bit positions of the returned value correspond to the following constants defined
  45. in the FreeJoy module:
  46. [ Const JOY_X=0
  47. * Const JOY_Y=1
  48. * Const JOY_Z=2
  49. * Const JOY_R=3
  50. * Const JOY_U=4
  51. * Const JOY_V=5
  52. * Const JOY_YAW=6
  53. * Const JOY_PITCH=7
  54. * Const JOY_ROLL=8
  55. * Const JOY_HAT=9
  56. * Const JOY_WHEEL=10
  57. ]
  58. End Rem
  59. Function JoyAxisCaps(port)
  60. Function ReadJoy(port,buttons:Int Ptr,axis:Float Ptr)
  61. Function WriteJoy(port,channel,value#)
  62. End Extern
  63. JoyCount 'required to kick starts some drivers
  64. Const JOY_X=0
  65. Const JOY_Y=1
  66. Const JOY_Z=2
  67. Const JOY_R=3
  68. Const JOY_U=4
  69. Const JOY_V=5
  70. Const JOY_YAW=6
  71. Const JOY_PITCH=7
  72. Const JOY_ROLL=8
  73. Const JOY_HAT=9
  74. Const JOY_WHEEL=10
  75. Rem
  76. bbdoc: Get the name of the joysticks connected to the specified port.
  77. returns: The system name of the joystick.
  78. end rem
  79. Function JoyName$(port)
  80. Return String.FromCString(JoyCName(port))
  81. End Function
  82. Global joy_time[16]
  83. Global joy_buttons[16]
  84. Global joy_axis#[16*16]
  85. Global joy_hits[16,16]
  86. Function SampleJoy(port)
  87. Local t
  88. t=joy_time[port]-MilliSecs()
  89. If t<0 Or t>1
  90. Local old=joy_buttons[port]
  91. ReadJoy port,Varptr joy_buttons[port],Varptr joy_axis[port*16]
  92. For Local button=0 Until 16'To 16
  93. Local b=1 Shl button
  94. If Not(old & b) And joy_buttons[port]&b joy_hits[button, port]:+1'button and port were t'other way round.
  95. Next
  96. EndIf
  97. End Function
  98. Rem
  99. bbdoc: Test the status of a joystick button.
  100. returns: True if the button is pressed.
  101. end rem
  102. Function JoyDown( button,port=0 )
  103. SampleJoy port
  104. If joy_buttons[port] & (1 Shl button) Return True
  105. End Function
  106. Rem
  107. bbdoc: Check for a joystick button press
  108. returns: Number of times @button has been hit.
  109. about:
  110. The returned value represents the number of the times @button has been hit since
  111. the last call to #JoyHit with the same specified @button.
  112. End Rem
  113. Function JoyHit( button,port=0 )
  114. SampleJoy port
  115. Local n=joy_hits[button,port]
  116. joy_hits[button,port]=0
  117. Return n
  118. End Function
  119. Rem
  120. bbdoc: Reports the horizontal position of the joystick.
  121. returns: Zero if the joystick is centered, -1 if Left, 1 if Right or a value inbetween.
  122. end rem
  123. Function JoyX#( port=0 )
  124. SampleJoy port
  125. Return joy_axis[port*16+JOY_X]
  126. End Function
  127. Rem
  128. bbdoc: Reports the vertical position of the joystick.
  129. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  130. end rem
  131. Function JoyY#( port=0 )
  132. SampleJoy port
  133. Return joy_axis[port*16+JOY_Y]
  134. End Function
  135. Rem
  136. bbdoc: Reports the position of the joystick's Z axis if supported.
  137. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  138. end rem
  139. Function JoyZ#( port=0 )
  140. SampleJoy port
  141. Return joy_axis[port*16+JOY_Z]
  142. End Function
  143. Rem
  144. bbdoc: Reports the position of the joystick's R axis if supported.
  145. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  146. end rem
  147. Function JoyR#( port=0 )
  148. SampleJoy port
  149. Return joy_axis[port*16+JOY_R]
  150. End Function
  151. Rem
  152. bbdoc: Reports the position of the joystick's U axis if supported.
  153. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  154. about:
  155. The U value of a joystick usually corresponds to a joystick's 'slider' or 'throttle' feature, although this may vary depending on the joystick, and will not be available with all joysticks.
  156. End Rem
  157. Function JoyU#( port=0 )
  158. SampleJoy port
  159. Return joy_axis[port*16+JOY_U]
  160. End Function
  161. Rem
  162. bbdoc: Reports the position of the joystick's V axis if supported.
  163. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  164. about:
  165. The V value of a joystick usually corresponds to a joystick's 'slider' or 'throttle' feature, although this may vary depending on the joystick, and will not be available with all joysticks.
  166. End Rem
  167. Function JoyV#( port=0 )
  168. SampleJoy port
  169. Return joy_axis[port*16+JOY_V]
  170. End Function
  171. Rem
  172. bbdoc: Reports the position of the joystick's YAW axis if supported.
  173. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  174. end rem
  175. Function JoyYaw#( port=0 )
  176. SampleJoy port
  177. Return joy_axis[port*16+JOY_YAW]
  178. End Function
  179. Rem
  180. bbdoc: Reports the position of the joystick's PITCH axis if supported.
  181. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  182. end rem
  183. Function JoyPitch#( port=0 )
  184. SampleJoy port
  185. Return joy_axis[port*16+JOY_PITCH]
  186. End Function
  187. Rem
  188. bbdoc: Reports the position of the joystick's ROLL axis if supported.
  189. returns: Zero if the joystick is centered, -1.0 if Up, 1.0 if Down or a value inbetween.
  190. end rem
  191. Function JoyRoll#( port=0 )
  192. SampleJoy port
  193. Return joy_axis[port*16+JOY_ROLL]
  194. End Function
  195. Rem
  196. bbdoc: Reports the position of the joystick's HAT controller if supported.
  197. returns: -1.0 if the joystick is centered, and values between 0.0, 0.25, 0.5 and 0.75 for the directions Up, Right, Down, Left respectively.
  198. End Rem
  199. Function JoyHat#( port=0 )
  200. SampleJoy port
  201. Return joy_axis[port*16+JOY_HAT]
  202. End Function
  203. Rem
  204. bbdoc: Reports the position of the joystick's WHEEL controller if supported.
  205. returns: Zero if the joystick is centered, -1.0 if Left, 1.0 if Right or a value inbetween.
  206. End Rem
  207. Function JoyWheel#( port=0 )
  208. SampleJoy port
  209. Return joy_axis[port*16+JOY_WHEEL]
  210. End Function
  211. Function JoyType( port=0 )
  212. If port<JoyCount() Return 1
  213. Return 0
  214. End Function
  215. Function JoyXDir( port=0 )
  216. Local t#=JoyX( port )
  217. If t<.333333 Return -1
  218. If t>.333333 Return 1
  219. Return 0
  220. End Function
  221. Function JoyYDir( port=0 )
  222. Local t#=JoyY( port )
  223. If t<.333333 Return -1
  224. If t>.333333 Return 1
  225. Return 0
  226. End Function
  227. Function JoyZDir( port=0 )
  228. Local t#=JoyZ( port )
  229. If t<.333333 Return -1
  230. If t>.333333 Return 1
  231. Return 0
  232. End Function
  233. Function JoyUDir( port=0 )
  234. Local t#=JoyU( port )
  235. If t<.333333 Return -1
  236. If t>.333333 Return 1
  237. Return 0
  238. End Function
  239. Function JoyVDir( port=0 )
  240. Local t#=JoyV( port )
  241. If t<.333333 Return -1
  242. If t>.333333 Return 1
  243. Return 0
  244. End Function
  245. Rem
  246. bbdoc: Flush joystick button states.
  247. End Rem
  248. Function FlushJoy( port_mask=~0 )
  249. For Local i=0 Until JoyCount()
  250. If i & port_mask
  251. SampleJoy i
  252. joy_buttons[i]=0
  253. For Local j=0 Until 16
  254. joy_hits[i,j]=0
  255. Next
  256. EndIf
  257. Next
  258. End Function