Input.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_terminer(false), m_relativeMouse(false), m_windowHalfHeight(0), m_windowHalfWidth(0)
  6. {
  7. // Initialisation du tableau m_touchesHeld[]
  8. for(int i(0); i < SDLK_LAST; i++)
  9. m_touchesHeld[i] = false;
  10. std::cout << "Il y a "<<SDLK_LAST<<" touches sur le clavier."<<std::endl;
  11. // Initialisation du tableau m_boutonsSourisHeld[]
  12. for(int i(0); i < 8; i++)
  13. m_boutonsSourisHeld[i] = false;
  14. }
  15. Input::~Input()
  16. {}
  17. // Méthodes
  18. void Input::updateEvenements()
  19. {
  20. // Pour éviter des mouvements fictifs de la souris, on réinitialise les coordonnées relatives
  21. m_xRel = 0;
  22. m_yRel = 0;
  23. // Les touches qui viennent d'être appuyées ne le sont plus
  24. m_touchesInstant.clear();
  25. m_boutonsSourisInstant.clear();
  26. // Boucle d'évènements
  27. while(SDL_PollEvent(&m_evenements))
  28. {
  29. // Switch sur le type d'évènement
  30. switch(m_evenements.type)
  31. {
  32. // Cas d'une touche enfoncée
  33. case SDL_KEYDOWN:
  34. m_touchesHeld[m_evenements.key.keysym.sym] = true;
  35. m_touchesInstant.push_back( m_evenements.key.keysym.sym );
  36. break;
  37. // Cas d'une touche relâchée
  38. case SDL_KEYUP:
  39. m_touchesHeld[m_evenements.key.keysym.sym] = false;
  40. break;
  41. // Cas de pression sur un bouton de la souris
  42. case SDL_MOUSEBUTTONDOWN:
  43. m_boutonsSourisHeld[m_evenements.button.button] = true;
  44. m_boutonsSourisInstant.push_back( m_evenements.button.button );
  45. break;
  46. // Cas du relâchement d'un bouton de la souris
  47. case SDL_MOUSEBUTTONUP:
  48. m_boutonsSourisHeld[m_evenements.button.button] = false;
  49. break;
  50. // Cas d'un mouvement de souris
  51. case SDL_MOUSEMOTION:
  52. if (m_relativeMouse)
  53. {
  54. m_xRel = m_evenements.motion.x-m_windowHalfWidth;
  55. m_yRel = m_evenements.motion.y-m_windowHalfHeight;
  56. }
  57. else
  58. {
  59. m_x = m_evenements.motion.x;
  60. m_y = m_evenements.motion.y;
  61. m_xRel = m_evenements.motion.xrel;
  62. m_yRel = m_evenements.motion.yrel;
  63. }
  64. break;
  65. // Cas de la fermeture de la fenêtre
  66. case SDL_QUIT:
  67. m_terminer = true;
  68. break;
  69. // Les autres ne nous interessent pas : on évite de faire râler g++
  70. default:
  71. break;
  72. }
  73. }
  74. // Pour éviter que la souris se barre en mode relative, on la "warp"
  75. if (m_relativeMouse)
  76. SDL_WarpMouse(m_windowHalfWidth, m_windowHalfHeight);
  77. }
  78. bool Input::terminer() const
  79. {
  80. return m_terminer;
  81. }
  82. void Input::afficherPointeur(bool reponse) const
  83. {
  84. if(reponse)
  85. SDL_ShowCursor(SDL_ENABLE);
  86. else
  87. SDL_ShowCursor(SDL_DISABLE);
  88. }
  89. void Input::capturerPointeur(bool reponse)
  90. {
  91. m_relativeMouse = reponse;
  92. }
  93. // Getters
  94. bool Input::getToucheHeld(const int touche) const
  95. {
  96. return m_touchesHeld[touche];
  97. }
  98. bool Input::getBoutonSourisHeld(const Uint8 bouton) const
  99. {
  100. return m_boutonsSourisHeld[bouton];
  101. }
  102. bool Input::getToucheInstant(const int touche) const
  103. {
  104. for ( unsigned int i(0); i < m_touchesInstant.size(); i++ ){
  105. if ( m_touchesInstant[i] == touche )
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool Input::getBoutonSourisInstant(const Uint8 bouton) const
  111. {
  112. for ( unsigned int i(0); i < m_boutonsSourisInstant.size(); i++ ){
  113. if ( m_boutonsSourisInstant[i] == bouton )
  114. return true;
  115. }
  116. return false;
  117. }
  118. bool Input::mouvementSouris() const
  119. {
  120. if(m_xRel == 0 && m_yRel == 0)
  121. return false;
  122. else
  123. return true;
  124. }
  125. // Getters concernant la position du curseur
  126. int Input::getX() const
  127. {
  128. return m_x;
  129. }
  130. int Input::getY() const
  131. {
  132. return m_y;
  133. }
  134. int Input::getXRel() const
  135. {
  136. return m_xRel;
  137. }
  138. int Input::getYRel() const
  139. {
  140. return m_yRel;
  141. }
  142. void Input::placerPtr(SDL_Surface* activWindow)
  143. {
  144. // Détermination de l'endroit de capture du pointeur
  145. m_windowHalfWidth = activWindow->w / 2;
  146. m_windowHalfHeight = activWindow->h / 2;
  147. }
  148. int Input::getWinHalfH()
  149. {
  150. return m_windowHalfHeight;
  151. }
  152. int Input::getWinHalfW()
  153. {
  154. return m_windowHalfWidth;
  155. }