bbinput.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "std.h"
  2. #include "bbsys.h"
  3. gxInput *gx_input;
  4. gxDevice *gx_mouse;
  5. gxDevice *gx_keyboard;
  6. vector<gxDevice*> gx_joysticks;
  7. static int mouse_x,mouse_y,mouse_z;
  8. static const float JLT=-1.0f/3.0f;
  9. static const float JHT=1.0f/3.0f;
  10. bool input_create(){
  11. if( gx_input=gx_runtime->openInput( 0 ) ){
  12. if( gx_keyboard=gx_input->getKeyboard() ){
  13. if( gx_mouse=gx_input->getMouse() ){
  14. gx_joysticks.clear();
  15. for( int k=0;k<gx_input->numJoysticks();++k ){
  16. gx_joysticks.push_back( gx_input->getJoystick(k) );
  17. }
  18. mouse_x=mouse_y=mouse_z=0;
  19. return true;
  20. }
  21. }
  22. gx_runtime->closeInput( gx_input );
  23. gx_input=0;
  24. }
  25. return false;
  26. }
  27. bool input_destroy(){
  28. gx_joysticks.clear();
  29. gx_runtime->closeInput( gx_input );
  30. gx_input=0;
  31. return true;
  32. }
  33. int bbKeyDown( int n ){
  34. return gx_keyboard->keyDown( n );
  35. }
  36. int bbKeyHit( int n ){
  37. return gx_keyboard->keyHit( n );
  38. }
  39. int bbGetKey(){
  40. return gx_input->toAscii( gx_keyboard->getKey() );
  41. }
  42. int bbWaitKey(){
  43. for(;;){
  44. if( !gx_runtime->idle() ) RTEX( 0 );
  45. if( int key=gx_keyboard->getKey( ) ){
  46. if( key=gx_input->toAscii( key ) ) return key;
  47. }
  48. gx_runtime->delay( 20 );
  49. }
  50. }
  51. void bbFlushKeys(){
  52. gx_keyboard->flush();
  53. }
  54. int bbMouseDown( int n ){
  55. return gx_mouse->keyDown( n );
  56. }
  57. int bbMouseHit( int n ){
  58. return gx_mouse->keyHit( n );
  59. }
  60. int bbGetMouse(){
  61. return gx_mouse->getKey();
  62. }
  63. int bbWaitMouse(){
  64. for(;;){
  65. if( !gx_runtime->idle() ) RTEX( 0 );
  66. if( int key=gx_mouse->getKey() ) return key;
  67. gx_runtime->delay( 20 );
  68. }
  69. }
  70. int bbMouseWait(){
  71. return bbWaitMouse();
  72. }
  73. int bbMouseX(){
  74. return gx_mouse->getAxisState( 0 );
  75. }
  76. int bbMouseY(){
  77. return gx_mouse->getAxisState( 1 );
  78. }
  79. int bbMouseZ(){
  80. return gx_mouse->getAxisState( 2 )/120;
  81. }
  82. int bbMouseXSpeed(){
  83. int dx=bbMouseX()-mouse_x;
  84. mouse_x+=dx;
  85. return dx;
  86. }
  87. int bbMouseYSpeed(){
  88. int dy=bbMouseY()-mouse_y;
  89. mouse_y+=dy;
  90. return dy;
  91. }
  92. int bbMouseZSpeed(){
  93. int dz=bbMouseZ()-mouse_z;
  94. mouse_z+=dz;
  95. return dz;
  96. }
  97. void bbFlushMouse(){
  98. gx_mouse->flush();
  99. }
  100. void bbMoveMouse( int x,int y ){
  101. gx_input->moveMouse( mouse_x=x,mouse_y=y );
  102. }
  103. int bbJoyType( int port ){
  104. return gx_input->getJoystickType( port );
  105. }
  106. int bbJoyDown( int n,int port ){
  107. if( port<0 || port>=gx_joysticks.size() ) return 0;
  108. return gx_joysticks[port]->keyDown( n );
  109. }
  110. int bbJoyHit( int n,int port ){
  111. if( port<0 || port>=gx_joysticks.size() ) return 0;
  112. return gx_joysticks[port]->keyHit( n );
  113. }
  114. int bbGetJoy( int port ){
  115. if( port<0 || port>=gx_joysticks.size() ) return 0;
  116. return gx_joysticks[port]->getKey();
  117. }
  118. int bbWaitJoy( int port ){
  119. if( port<0 || port>=gx_joysticks.size() ) return 0;
  120. for(;;){
  121. if( !gx_runtime->idle() ) RTEX( 0 );
  122. if( int key=gx_joysticks[port]->getKey() ) return key;
  123. gx_runtime->delay( 20 );
  124. }
  125. }
  126. float bbJoyX( int port ){
  127. if( port<0 || port>=gx_joysticks.size() ) return 0;
  128. return gx_joysticks[port]->getAxisState(0);
  129. }
  130. float bbJoyY( int port ){
  131. if( port<0 || port>=gx_joysticks.size() ) return 0;
  132. return gx_joysticks[port]->getAxisState(1);
  133. }
  134. float bbJoyZ( int port ){
  135. if( port<0 || port>=gx_joysticks.size() ) return 0;
  136. return gx_joysticks[port]->getAxisState(2);
  137. }
  138. float bbJoyU( int port ){
  139. if( port<0 || port>=gx_joysticks.size() ) return 0;
  140. return gx_joysticks[port]->getAxisState(3);
  141. }
  142. float bbJoyV( int port ){
  143. if( port<0 || port>=gx_joysticks.size() ) return 0;
  144. return gx_joysticks[port]->getAxisState(4);
  145. }
  146. float bbJoyPitch( int port ){
  147. if( port<0 || port>=gx_joysticks.size() ) return 0;
  148. return gx_joysticks[port]->getAxisState(5)*180;
  149. }
  150. float bbJoyYaw( int port ){
  151. if( port<0 || port>=gx_joysticks.size() ) return 0;
  152. return gx_joysticks[port]->getAxisState(6)*180;
  153. }
  154. float bbJoyRoll( int port ){
  155. if( port<0 || port>=gx_joysticks.size() ) return 0;
  156. return gx_joysticks[port]->getAxisState(7)*180;
  157. }
  158. int bbJoyHat( int port ){
  159. if( port<0 || port>=gx_joysticks.size() ) return 0;
  160. return gx_joysticks[port]->getAxisState(8);
  161. }
  162. int bbJoyXDir( int port ){
  163. if( port<0 || port>=gx_joysticks.size() ) return 0;
  164. float t=gx_joysticks[port]->getAxisState(0);
  165. return t<JLT ? -1 : ( t>JHT ? 1 : 0 );
  166. }
  167. int bbJoyYDir( int port ){
  168. if( port<0 || port>=gx_joysticks.size() ) return 0;
  169. float t=gx_joysticks[port]->getAxisState(1);
  170. return t<JLT ? -1 : ( t>JHT ? 1 : 0 );
  171. }
  172. int bbJoyZDir( int port ){
  173. if( port<0 || port>=gx_joysticks.size() ) return 0;
  174. float t=gx_joysticks[port]->getAxisState(2);
  175. return t<JLT ? -1 : ( t>JHT ? 1 : 0 );
  176. }
  177. int bbJoyUDir( int port ){
  178. if( port<0 || port>=gx_joysticks.size() ) return 0;
  179. float t=gx_joysticks[port]->getAxisState(3);
  180. return t<JLT ? -1 : ( t>JHT ? 1 : 0 );
  181. }
  182. int bbJoyVDir( int port ){
  183. if( port<0 || port>=gx_joysticks.size() ) return 0;
  184. float t=gx_joysticks[port]->getAxisState(4);
  185. return t<JLT ? -1 : ( t>JHT ? 1 : 0 );
  186. }
  187. void bbFlushJoy(){
  188. for( int k=0;k<gx_joysticks.size();++k ) gx_joysticks[k]->flush();
  189. }
  190. void bbEnableDirectInput( int enable ){
  191. gx_runtime->enableDirectInput( !!enable );
  192. }
  193. int bbDirectInputEnabled(){
  194. return gx_runtime->directInputEnabled();
  195. }
  196. void input_link( void (*rtSym)( const char *sym,void *pc ) ){
  197. rtSym( "%KeyDown%key",bbKeyDown );
  198. rtSym( "%KeyHit%key",bbKeyHit );
  199. rtSym( "%GetKey",bbGetKey );
  200. rtSym( "%WaitKey",bbWaitKey );
  201. rtSym( "FlushKeys",bbFlushKeys );
  202. rtSym( "%MouseDown%button",bbMouseDown );
  203. rtSym( "%MouseHit%button",bbMouseHit );
  204. rtSym( "%GetMouse",bbGetMouse );
  205. rtSym( "%WaitMouse",bbWaitMouse );
  206. rtSym( "%MouseWait",bbWaitMouse );
  207. rtSym( "%MouseX",bbMouseX );
  208. rtSym( "%MouseY",bbMouseY );
  209. rtSym( "%MouseZ",bbMouseZ );
  210. rtSym( "%MouseXSpeed",bbMouseXSpeed );
  211. rtSym( "%MouseYSpeed",bbMouseYSpeed );
  212. rtSym( "%MouseZSpeed",bbMouseZSpeed );
  213. rtSym( "FlushMouse",bbFlushMouse );
  214. rtSym( "MoveMouse%x%y",bbMoveMouse );
  215. rtSym( "%JoyType%port=0",bbJoyType );
  216. rtSym( "%JoyDown%button%port=0",bbJoyDown );
  217. rtSym( "%JoyHit%button%port=0",bbJoyHit );
  218. rtSym( "%GetJoy%port=0",bbGetJoy );
  219. rtSym( "%WaitJoy%port=0",bbWaitJoy );
  220. rtSym( "%JoyWait%port=0",bbWaitJoy );
  221. rtSym( "#JoyX%port=0",bbJoyX );
  222. rtSym( "#JoyY%port=0",bbJoyY );
  223. rtSym( "#JoyZ%port=0",bbJoyZ );
  224. rtSym( "#JoyU%port=0",bbJoyU );
  225. rtSym( "#JoyV%port=0",bbJoyV );
  226. rtSym( "#JoyPitch%port=0",bbJoyPitch );
  227. rtSym( "#JoyYaw%port=0",bbJoyYaw );
  228. rtSym( "#JoyRoll%port=0",bbJoyRoll );
  229. rtSym( "%JoyHat%port=0",bbJoyHat );
  230. rtSym( "%JoyXDir%port=0",bbJoyXDir );
  231. rtSym( "%JoyYDir%port=0",bbJoyYDir );
  232. rtSym( "%JoyZDir%port=0",bbJoyZDir );
  233. rtSym( "%JoyUDir%port=0",bbJoyUDir );
  234. rtSym( "%JoyVDir%port=0",bbJoyVDir );
  235. rtSym( "FlushJoy",bbFlushJoy );
  236. rtSym( "EnableDirectInput%enable",bbEnableDirectInput );
  237. rtSym( "%DirectInputEnabled",bbDirectInputEnabled );
  238. }