Input.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "Input.h"
  2. // Constructor
  3. Input::Input()
  4. : m_x(0), m_y(0), m_xRel(0), m_yRel(0),
  5. m_finished(false), m_relativeMouse(false), m_window(0), m_windowHalfHeight(0), m_windowHalfWidth(0) {
  6. // Initialisation du tableau m_keys[]
  7. for (int i(0); i < SDL_NUM_SCANCODES; i++)
  8. m_keys[i] = false;
  9. // Initialisation du tableau m_mouseKeys[]
  10. for (int i(0); i < 8; i++)
  11. m_mouseKeys[i] = false;
  12. }
  13. // Destructor
  14. Input::~Input() {}
  15. // Methods
  16. void Input::updateEvents() {
  17. // Reset relative coordinates
  18. m_xRel = 0;
  19. m_yRel = 0;
  20. // Clear instant keys
  21. m_instantKeys.clear();
  22. // Event loop
  23. while (SDL_PollEvent(&m_event)) {
  24. if (catchKeyBoardEvents(m_event))
  25. continue;
  26. else if (catchMouseEvents(m_event))
  27. continue;
  28. else
  29. catchWindowEvents(m_event);
  30. }
  31. // Keeping mouse in window
  32. if (m_relativeMouse)
  33. SDL_WarpMouseInWindow(m_window, m_windowHalfWidth, m_windowHalfHeight);
  34. }
  35. bool Input::catchKeyBoardEvents(const SDL_Event &event) {
  36. switch (event.type) {
  37. case SDL_KEYDOWN:
  38. m_keys[event.key.keysym.scancode] = true;
  39. m_instantKeys.insert(event.key.keysym.scancode);
  40. return true;
  41. case SDL_KEYUP:
  42. m_keys[event.key.keysym.scancode] = false;
  43. return true;
  44. default:
  45. return false;
  46. }
  47. }
  48. bool Input::catchMouseEvents(const SDL_Event &event) {
  49. switch (event.type) {
  50. case SDL_MOUSEBUTTONDOWN:
  51. m_mouseKeys[event.button.button] = true;
  52. return true;
  53. case SDL_MOUSEBUTTONUP:
  54. m_mouseKeys[event.button.button] = false;
  55. return true;
  56. case SDL_MOUSEMOTION:
  57. if (m_relativeMouse) {
  58. m_xRel = event.motion.x - m_windowHalfWidth;
  59. m_yRel = event.motion.y - m_windowHalfHeight;
  60. } else {
  61. m_x = event.motion.x;
  62. m_y = event.motion.y;
  63. m_xRel = event.motion.xrel;
  64. m_yRel = event.motion.yrel;
  65. }
  66. return true;
  67. default:
  68. return false;
  69. }
  70. }
  71. bool Input::catchWindowEvents(const SDL_Event &event) {
  72. switch (event.type) {
  73. case SDL_WINDOWEVENT:
  74. if (event.window.event == SDL_WINDOWEVENT_CLOSE)
  75. m_finished = true;
  76. return true;
  77. default:
  78. return false;
  79. }
  80. }
  81. bool Input::isFinished() const {
  82. return m_finished;
  83. }
  84. void Input::showCursor(bool flag) const {
  85. if (flag)
  86. SDL_ShowCursor(SDL_ENABLE);
  87. else
  88. SDL_ShowCursor(SDL_DISABLE);
  89. }
  90. void Input::capPtr(bool flag) {
  91. m_relativeMouse = flag;
  92. }
  93. // Getters
  94. bool Input::getKey(const SDL_Scancode key) const {
  95. return m_keys[key];
  96. }
  97. bool Input::getInstantKey(const SDL_Scancode key) const {
  98. return m_instantKeys.find(key) != m_instantKeys.end();
  99. }
  100. bool Input::getMouseKey(const Uint8 key) const {
  101. return m_mouseKeys[key];
  102. }
  103. bool Input::isMouseMoving() const {
  104. return !(m_xRel == 0 && m_yRel == 0);
  105. }
  106. // Getters upon cursor position
  107. int Input::getX() const {
  108. return m_x;
  109. }
  110. int Input::getY() const {
  111. return m_y;
  112. }
  113. int Input::getXFromCenter() {
  114. return m_x - m_windowHalfWidth;
  115. }
  116. int Input::getYFromCenter() {
  117. return m_y - m_windowHalfHeight;
  118. }
  119. int Input::getXRel() const {
  120. return m_xRel;
  121. }
  122. int Input::getYRel() const {
  123. return m_yRel;
  124. }
  125. void Input::setWindow(SDL_Window *activWindow) {
  126. // Direct relation
  127. m_window = activWindow;
  128. // Middle computation
  129. SDL_GetWindowSize(activWindow, &m_windowHalfWidth, &m_windowHalfHeight);
  130. m_windowHalfWidth /= 2;
  131. m_windowHalfHeight /= 2;
  132. }