Input.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "core/App.h"
  2. #include "core/Logger.h"
  3. #include "core/Globals.h"
  4. #include "Input.h"
  5. #include <SDL/SDL.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()) /
  73. (float)AppSingleton::get().getWindowWidth() - 1.0;
  74. mousePosNdc.y() = 1.0 - (2.0 * mousePos.y()) /
  75. (float)AppSingleton::get().getWindowHeight();
  76. if(warpMouseFlag)
  77. {
  78. // the SDL_WarpMouse pushes an event in the event queue.
  79. // This check is so we wont process the event of the
  80. // SDL_WarpMouse function
  81. if(mousePosNdc == Vec2(0.0))
  82. {
  83. break;
  84. }
  85. uint w = AppSingleton::get().getWindowWidth();
  86. uint h = AppSingleton::get().getWindowHeight();
  87. SDL_WarpMouse(w / 2, h / 2);
  88. }
  89. mouseVelocity = mousePosNdc - prevMousePosNdc;
  90. break;
  91. }
  92. case SDL_QUIT:
  93. AppSingleton::get().quit(1);
  94. break;
  95. }
  96. }
  97. }