Input.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <SDL.h>
  2. #include "Input.h"
  3. #include "Renderer.h"
  4. namespace I {
  5. //=====================================================================================================================================
  6. // VARS =
  7. //=====================================================================================================================================
  8. Vec<short> keys( SDL_NUM_SCANCODES, 0 );
  9. Vec<short> mouseBtns( 8, 0 );
  10. Vec2 mousePosNdc;
  11. Vec2 mousePos;
  12. Vec2 mouseVelocity;
  13. bool warpMouse = false;
  14. bool hideCursor = true;
  15. //=====================================================================================================================================
  16. // reset =
  17. //=====================================================================================================================================
  18. void reset( void )
  19. {
  20. DEBUG_ERR( keys.size() < 1 );
  21. DEBUG_ERR( mouseBtns.size() < 1 );
  22. memset( &keys[0], 0, keys.size()*sizeof(short) );
  23. memset( &mouseBtns[0], 0, mouseBtns.size()*sizeof(short) );
  24. mousePosNdc.setZero();
  25. mouseVelocity.setZero();
  26. }
  27. /*
  28. =======================================================================================================================================
  29. handleEvents =
  30. =======================================================================================================================================
  31. */
  32. void handleEvents()
  33. {
  34. if( hideCursor ) SDL_ShowCursor( SDL_DISABLE );
  35. else SDL_ShowCursor( SDL_ENABLE );
  36. // add the times a key is bying pressed
  37. for( uint x=0; x<keys.size(); x++ )
  38. {
  39. if( keys[x] ) ++keys[x];
  40. }
  41. for( int x=0; x<8; x++ )
  42. {
  43. if( mouseBtns[x] ) ++mouseBtns[x];
  44. }
  45. mouseVelocity.setZero();
  46. SDL_Event event_;
  47. while( SDL_PollEvent(&event_) )
  48. {
  49. switch( event_.type )
  50. {
  51. case SDL_KEYDOWN:
  52. keys[event_.key.keysym.scancode] = 1;
  53. break;
  54. case SDL_KEYUP:
  55. keys[event_.key.keysym.scancode] = 0;
  56. break;
  57. case SDL_MOUSEBUTTONDOWN:
  58. mouseBtns[event_.button.button] = 1;
  59. break;
  60. case SDL_MOUSEBUTTONUP:
  61. mouseBtns[event_.button.button] = 0;
  62. break;
  63. case SDL_MOUSEMOTION:
  64. {
  65. Vec2 prevMousePosNdc( mousePosNdc );
  66. mousePos.x = event_.button.x;
  67. mousePos.y = event_.button.y;
  68. mousePosNdc.x = (2.0 * mousePos.x) / (float)app->getWindowWidth() - 1.0;
  69. mousePosNdc.y = 1.0 - (2.0 * mousePos.y) / (float)app->getWindowHeight();
  70. if( warpMouse )
  71. {
  72. // the SDL_WarpMouse pushes an event in the event queue. This check is so we wont process the event of the...
  73. // ...SDL_WarpMouse function
  74. if( mousePosNdc == Vec2::getZero() ) break;
  75. SDL_WarpMouse( app->getWindowWidth()/2, app->getWindowHeight()/2);
  76. }
  77. mouseVelocity = mousePosNdc - prevMousePosNdc;
  78. break;
  79. }
  80. case SDL_QUIT:
  81. app->quitApp(1);
  82. break;
  83. }
  84. }
  85. //cout << fixed << " velocity: " << mouseVelocity.x() << ' ' << mouseVelocity.y() << endl;
  86. //cout << fixed << mousePosNdc.x() << ' ' << mousePosNdc.y() << endl;
  87. //cout << crnt_keys[SDLK_m] << ' ' << prev_keys[SDLK_m] << " " << keys[SDLK_m] << endl;
  88. //cout << mouseBtns[SDL_BUTTON_LEFT] << endl;
  89. }
  90. } // end namespace