Input.cpp 3.5 KB

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