Input.cpp 2.8 KB

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