Input.cpp 3.0 KB

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