Input.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "Input.h"
  2. // Constructeur et Destructeur
  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. Input::~Input() {}
  14. // Méthodes
  15. void Input::updateEvents() {
  16. // Réinitialise les coordonnées relatives
  17. m_xRel = 0;
  18. m_yRel = 0;
  19. // Boucle d'évènements
  20. while (SDL_PollEvent(&m_events)) {
  21. // Switch sur le type d'évènement
  22. switch (m_events.type) {
  23. // Cas d'une touche enfoncée
  24. case SDL_KEYDOWN:
  25. m_keys[m_events.key.keysym.scancode] = true;
  26. break;
  27. // Cas d'une touche relâchée
  28. case SDL_KEYUP:
  29. m_keys[m_events.key.keysym.scancode] = false;
  30. break;
  31. // Cas de pression sur un bouton de la souris
  32. case SDL_MOUSEBUTTONDOWN:
  33. m_mouseKeys[m_events.button.button] = true;
  34. break;
  35. // Cas du relâchement d'un bouton de la souris
  36. case SDL_MOUSEBUTTONUP:
  37. m_mouseKeys[m_events.button.button] = false;
  38. break;
  39. // Cas d'un mouvement de souris
  40. case SDL_MOUSEMOTION:
  41. if (m_relativeMouse) {
  42. m_xRel = m_events.motion.x - m_windowHalfWidth;
  43. m_yRel = m_events.motion.y - m_windowHalfHeight;
  44. } else {
  45. m_x = m_events.motion.x;
  46. m_y = m_events.motion.y;
  47. m_xRel = m_events.motion.xrel;
  48. m_yRel = m_events.motion.yrel;
  49. }
  50. break;
  51. // Cas de la fermeture de la fenêtre
  52. case SDL_WINDOWEVENT:
  53. if (m_events.window.event == SDL_WINDOWEVENT_CLOSE)
  54. m_finished = true;
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. // Pour éviter que la souris se barre en mode relative, on la "warp"
  61. if (m_relativeMouse)
  62. SDL_WarpMouseInWindow(m_window, m_windowHalfWidth, m_windowHalfHeight);
  63. }
  64. bool Input::isFinished() const {
  65. return m_finished;
  66. }
  67. void Input::showCursor(bool reponse) const {
  68. if (reponse)
  69. SDL_ShowCursor(SDL_ENABLE);
  70. else
  71. SDL_ShowCursor(SDL_DISABLE);
  72. }
  73. void Input::capPtr(bool reponse) {
  74. m_relativeMouse = reponse;
  75. }
  76. // Getters
  77. bool Input::getKey(const SDL_Scancode key) const {
  78. return m_keys[key];
  79. }
  80. bool Input::getMouseKey(const Uint8 key) const {
  81. return m_mouseKeys[key];
  82. }
  83. bool Input::isMouseMoving() const {
  84. return !(m_xRel == 0 && m_yRel == 0);
  85. }
  86. // Getters concernant la position du curseur
  87. int Input::getX() const {
  88. return m_x;
  89. }
  90. int Input::getY() const {
  91. return m_y;
  92. }
  93. int Input::getXRel() const {
  94. return m_xRel;
  95. }
  96. int Input::getYRel() const {
  97. return m_yRel;
  98. }
  99. void Input::setWindow(SDL_Window *activWindow) {
  100. // Attributio directe
  101. m_window = activWindow;
  102. // Détermination de l'endroit de capture du pointeur
  103. SDL_GetWindowSize(activWindow, &m_windowHalfWidth, &m_windowHalfHeight);
  104. m_windowHalfWidth /= 2;
  105. m_windowHalfHeight /= 2;
  106. }
  107. int Input::getMouseRelX() const {
  108. return m_xRel;
  109. }
  110. int Input::getMouseRelY() const {
  111. return m_yRel;
  112. }