BJOY.CPP 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //
  19. // joy.cpp
  20. //
  21. // History:
  22. // 04/22/95 JMI Started.
  23. //
  24. // 10/10/97 JMI Added temporarily directly to Postal but want to make
  25. // this part of RSPiX soon (need to add event driven
  26. // stuff for rspGetNextInpuEvent() ).
  27. //
  28. //////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Handles all Windows specific joystick stuff.
  31. //
  32. //////////////////////////////////////////////////////////////////////////////
  33. #include "BLUE/win32/win.h"
  34. #include <mmsystem.h>
  35. #include "bjoy.h" // For typedefs and macros.
  36. #include "Blue.h"
  37. //////////////////////////////////////////////////////////////////////////////
  38. // Macros.
  39. //////////////////////////////////////////////////////////////////////////////
  40. #define NUM_JOYSTICKS 2
  41. #define MID_THRESHOLD_PERCENT ((float)20) // In % (e.g., 25 would be 25%).
  42. //////////////////////////////////////////////////////////////////////////////
  43. // Module specific (static) variables.
  44. //////////////////////////////////////////////////////////////////////////////
  45. static JOYCAPS ms_ajoycaps[NUM_JOYSTICKS]; // Capabilities of each joy.
  46. static USHORT ms_ausXmids[NUM_JOYSTICKS]; // Middle positions for x.
  47. static USHORT ms_ausYmids[NUM_JOYSTICKS]; // Middle positions for y.
  48. static USHORT ms_ausZmids[NUM_JOYSTICKS]; // Middle positions for z.
  49. static USHORT ms_ausXThresh[NUM_JOYSTICKS]; // Center thresholds for x.
  50. static USHORT ms_ausYThresh[NUM_JOYSTICKS]; // Center thresholds for y.
  51. static USHORT ms_ausZThresh[NUM_JOYSTICKS]; // Center thresholds for z.
  52. static JOYINFO ms_ajiCurr[NUM_JOYSTICKS]; // Current joystick info.
  53. static JOYINFO ms_ajiPrev[NUM_JOYSTICKS]; // Previous joystick info.
  54. static JOYSTATE ms_ajsCurr[NUM_JOYSTICKS]; // Current joystick state.
  55. static JOYSTATE ms_ajsPrev[NUM_JOYSTICKS]; // Previous joystick state.
  56. //////////////////////////////////////////////////////////////////////////////
  57. // Externally callable functions.
  58. //////////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////////////////////////
  60. //
  61. // To be called by the Blue library itself, only.
  62. // Initializes the joystick module. NOTE: May fail if no joysticks attached.
  63. // Returns nothing in order to remind us that even if the init fails the app
  64. // should still be called.
  65. //
  66. //////////////////////////////////////////////////////////////////////////////
  67. extern void Joy_Init(void)
  68. {
  69. short sNum = (short)joyGetNumDevs();
  70. USHORT usRangeX, usRangeY, usRangeZ;
  71. for (short i = 0; i < sNum && i < NUM_JOYSTICKS; i++)
  72. {
  73. switch (joyGetDevCaps((i == 0 ? JOYSTICKID1 : JOYSTICKID2),
  74. &(ms_ajoycaps[i]),
  75. sizeof(ms_ajoycaps[i])
  76. )
  77. )
  78. {
  79. case JOYERR_NOERROR:
  80. // Calculate ranges.
  81. usRangeX = (ms_ajoycaps[i].wXmax - ms_ajoycaps[i].wXmin);
  82. usRangeY = (ms_ajoycaps[i].wYmax - ms_ajoycaps[i].wYmin);
  83. usRangeZ = (ms_ajoycaps[i].wZmax - ms_ajoycaps[i].wZmin);
  84. // Calculate middle.
  85. ms_ausXmids[i] = ms_ajoycaps[i].wXmin + usRangeX / 2;
  86. ms_ausYmids[i] = ms_ajoycaps[i].wYmin + usRangeY / 2;
  87. ms_ausZmids[i] = ms_ajoycaps[i].wZmin + usRangeZ / 2;
  88. ms_ausXThresh[i] = (float)usRangeX * (float)((float)MID_THRESHOLD_PERCENT / 100.0F);
  89. ms_ausYThresh[i] = (float)usRangeY * (float)((float)MID_THRESHOLD_PERCENT / 100.0F);
  90. ms_ausZThresh[i] = (float)usRangeZ * (float)((float)MID_THRESHOLD_PERCENT / 100.0F);
  91. #if 0
  92. TRACE("wMid:\t0x%04X\n"
  93. "wPid:\t0x%04X\n"
  94. "szPname:\t%s\n"
  95. "wXmin:\t%u\n"
  96. "wXmax:\t%u\n"
  97. "wYmin:\t%u\n"
  98. "wYmax:\t%u\n"
  99. "wZmin:\t%u\n"
  100. "wZmax:\t%u\n"
  101. "wNumButtons:\t%u\n"
  102. "wPeriodMin:\t%u\n"
  103. "wPeriodMax:\t%u\n",
  104. ms_ajoycaps[i].wMid,
  105. ms_ajoycaps[i].wPid,
  106. ms_ajoycaps[i].szPname,
  107. ms_ajoycaps[i].wXmin,
  108. ms_ajoycaps[i].wXmax,
  109. ms_ajoycaps[i].wYmin,
  110. ms_ajoycaps[i].wYmax,
  111. ms_ajoycaps[i].wZmin,
  112. ms_ajoycaps[i].wZmax,
  113. ms_ajoycaps[i].wNumButtons,
  114. ms_ajoycaps[i].wPeriodMin,
  115. ms_ajoycaps[i].wPeriodMax);
  116. #endif
  117. // Success.
  118. break;
  119. case MMSYSERR_NODRIVER:
  120. TRACE("BLUE:Joy_Init(): The joystick driver is not present.\n");
  121. break;
  122. case MMSYSERR_INVALPARAM:
  123. TRACE("BLUE:Joy_Init(): An invalid parameter was passed.\n");
  124. break;
  125. }
  126. }
  127. }
  128. //////////////////////////////////////////////////////////////////////////////
  129. //
  130. // Updates joystick sJoy's current state and makes the current state the
  131. // previous.
  132. // Returns 0 on success.
  133. //
  134. //////////////////////////////////////////////////////////////////////////////
  135. extern short Blu_UpdateJoy(short sJoy)
  136. {
  137. short sRes = 0; // Assume success.
  138. ASSERT(sJoy == 0 || sJoy == 1);
  139. // Get the joystick info. Only update our variables, if successful.
  140. JOYINFO jiTemp;
  141. switch (joyGetPos( (sJoy == 0) ? JOYSTICKID1 : JOYSTICKID2, &jiTemp) )
  142. {
  143. case JOYERR_NOERROR:
  144. // Success. Set values.
  145. // Set previous joyinfo to current.
  146. ms_ajiPrev[sJoy] = ms_ajiCurr[sJoy];
  147. // Set the current to the temp.
  148. ms_ajiCurr[sJoy] = jiTemp;
  149. // Set previous joy state to current.
  150. ms_ajsPrev[sJoy] = ms_ajsCurr[sJoy];
  151. // Fill in new state fields.
  152. ms_ajsCurr[sJoy].button1 = (((jiTemp.wButtons & JOY_BUTTON1) != 0) ? 1 : 0);
  153. ms_ajsCurr[sJoy].button2 = (((jiTemp.wButtons & JOY_BUTTON2) != 0) ? 1 : 0);
  154. ms_ajsCurr[sJoy].button3 = (((jiTemp.wButtons & JOY_BUTTON3) != 0) ? 1 : 0);
  155. ms_ajsCurr[sJoy].button4 = (((jiTemp.wButtons & JOY_BUTTON4) != 0) ? 1 : 0);
  156. ms_ajsCurr[sJoy].left = (jiTemp.wXpos < (USHORT)(ms_ausXmids[sJoy] - ms_ausXThresh[sJoy]));
  157. ms_ajsCurr[sJoy].right = (jiTemp.wXpos > (USHORT)(ms_ausXmids[sJoy] + ms_ausXThresh[sJoy]));
  158. ms_ajsCurr[sJoy].up = (jiTemp.wYpos < (USHORT)(ms_ausYmids[sJoy] - ms_ausYThresh[sJoy]));
  159. ms_ajsCurr[sJoy].down = (jiTemp.wYpos > (USHORT)(ms_ausYmids[sJoy] + ms_ausYThresh[sJoy]));
  160. ms_ajsCurr[sJoy].toward = (jiTemp.wZpos < (USHORT)(ms_ausZmids[sJoy] - ms_ausZThresh[sJoy]));
  161. ms_ajsCurr[sJoy].away = (jiTemp.wZpos > (USHORT)(ms_ausZmids[sJoy] + ms_ausZThresh[sJoy]));
  162. break;
  163. }
  164. return sRes;
  165. }
  166. //////////////////////////////////////////////////////////////////////////////
  167. //
  168. // Puts the coordinates of joystick sJoy's position in your longs.
  169. // Returns nothing.
  170. //
  171. //////////////////////////////////////////////////////////////////////////////
  172. extern void Blu_GetJoyPos(short sJoy, long *px, long *py, long *pz)
  173. {
  174. ASSERT(sJoy == 0 || sJoy == 1);
  175. // Copy the coordinates.
  176. *px = (long)ms_ajiCurr[sJoy].wXpos;
  177. *py = (long)ms_ajiCurr[sJoy].wYpos;
  178. *pz = (long)ms_ajiCurr[sJoy].wZpos;
  179. }
  180. //////////////////////////////////////////////////////////////////////////////
  181. //
  182. // Puts the coordinates of the previous joystick sJoy's position in your longs.
  183. // Returns nothing.
  184. //
  185. //////////////////////////////////////////////////////////////////////////////
  186. extern void Blu_GetJoyPrevPos(short sJoy, long *px, long *py, long *pz)
  187. {
  188. ASSERT(sJoy == 0 || sJoy == 1);
  189. // Copy the coordinates.
  190. *px = (long)ms_ajiPrev[sJoy].wXpos;
  191. *py = (long)ms_ajiPrev[sJoy].wYpos;
  192. *pz = (long)ms_ajiPrev[sJoy].wZpos;
  193. }
  194. //////////////////////////////////////////////////////////////////////////////
  195. //
  196. // Returns the current joystick sJoy's state.
  197. //
  198. //////////////////////////////////////////////////////////////////////////////
  199. extern USHORT Blu_GetJoyState(short sJoy)
  200. {
  201. ASSERT(sJoy == 0 || sJoy == 1);
  202. return ms_ajsCurr[sJoy].us;
  203. }
  204. //////////////////////////////////////////////////////////////////////////////
  205. //
  206. // Returns the previous joystick sJoy's state.
  207. //
  208. //////////////////////////////////////////////////////////////////////////////
  209. extern USHORT Blu_GetJoyPrevState(short sJoy)
  210. {
  211. ASSERT(sJoy == 0 || sJoy == 1);
  212. return ms_ajsPrev[sJoy].us;
  213. }
  214. //////////////////////////////////////////////////////////////////////////////
  215. //
  216. // Places the current joystick sJoy's state.
  217. //
  218. //////////////////////////////////////////////////////////////////////////////
  219. extern void Blu_GetJoyState(short sJoy, PJOYSTATE pjs)
  220. {
  221. ASSERT(sJoy == 0 || sJoy == 1);
  222. *pjs = ms_ajsCurr[sJoy];
  223. }
  224. //////////////////////////////////////////////////////////////////////////////
  225. //
  226. // Places the previous joystick sJoy's state.
  227. //
  228. //////////////////////////////////////////////////////////////////////////////
  229. extern void Blu_GetJoyPrevState(short sJoy, PJOYSTATE pjs)
  230. {
  231. ASSERT(sJoy == 0 || sJoy == 1);
  232. *pjs = ms_ajsPrev[sJoy];
  233. }
  234. //////////////////////////////////////////////////////////////////////////////
  235. // EOF
  236. //////////////////////////////////////////////////////////////////////////////