InputAndJoy.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "InputAndJoy.h"
  2. // Data description
  3. struct GamePadData {
  4. GamePadData() : joystick(nullptr), nbAxes(0), nbButtons(0) {};
  5. SDL_Joystick *joystick;
  6. int nbAxes;
  7. int nbButtons;
  8. std::vector<int> axeValue;
  9. std::vector<bool> buttonValue;
  10. };
  11. // InputAndJoy methods
  12. InputAndJoy::InputAndJoy() {
  13. // JoyStick init
  14. if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
  15. std::cout << "InputAndJoy::InputAndJoy() > " << SDL_GetError() << std::endl;
  16. } else {
  17. // Opening joysticks
  18. SDL_JoystickEventState(SDL_ENABLE);
  19. SDL_Joystick* currentJoystick(NULL);
  20. int i(0);
  21. while (true)
  22. {
  23. // Is a new joystick available
  24. currentJoystick = SDL_JoystickOpen(i);
  25. if (currentJoystick == NULL)
  26. break;
  27. m_pad.push_back(new GamePadData);
  28. // Read data
  29. m_pad[i]->joystick = currentJoystick;
  30. m_pad[i]->nbAxes = SDL_JoystickNumAxes(currentJoystick);
  31. m_pad[i]->nbButtons = SDL_JoystickNumButtons(currentJoystick);
  32. for (int k(0); k < m_pad[i]->nbAxes; k++)
  33. m_pad[i]->axeValue.push_back(0);
  34. for (int k(0); k < m_pad[i]->nbButtons; k++)
  35. m_pad[i]->buttonValue.push_back(false);
  36. // Next one
  37. i++;
  38. }
  39. }
  40. }
  41. InputAndJoy::~InputAndJoy() {
  42. while (!m_pad.empty()) {
  43. SDL_JoystickClose(m_pad.back()->joystick);
  44. delete m_pad.back();
  45. m_pad.back() = nullptr;
  46. m_pad.pop_back();
  47. }
  48. }
  49. void InputAndJoy::updateEvents() {
  50. // Doubling code
  51. m_xRel = 0;
  52. m_yRel = 0;
  53. // Clear instant keys
  54. m_instantKeys.clear();
  55. // Super event loop
  56. while (SDL_PollEvent(&m_event)) {
  57. if (catchKeyBoardEvents(m_event))
  58. continue;
  59. else if (catchMouseEvents(m_event))
  60. continue;
  61. else if (catchWindowEvents(m_event))
  62. continue;
  63. else
  64. catchPadEvents(m_event);
  65. }
  66. // Keeping mouse in window
  67. if (m_relativeMouse)
  68. SDL_WarpMouseInWindow(m_window, m_windowHalfWidth, m_windowHalfHeight);
  69. }
  70. bool InputAndJoy::catchPadEvents(const SDL_Event &event) {
  71. switch (event.type) {
  72. case SDL_JOYAXISMOTION:
  73. m_pad[event.jaxis.which]->axeValue[event.jaxis.axis] = event.jaxis.value;
  74. return true;
  75. case SDL_JOYBUTTONDOWN:
  76. m_pad[event.jaxis.which]->buttonValue[event.jbutton.button] = true;
  77. return true;
  78. case SDL_JOYBUTTONUP:
  79. m_pad[event.jaxis.which]->buttonValue[event.jbutton.button] = false;
  80. return true;
  81. default:
  82. return false;
  83. }
  84. }
  85. int InputAndJoy::getAxeValue(const Uint8 axeID, Sint32 joyID) const {
  86. if (joyID >= m_pad.size()) {
  87. std::cout << "InputAndJoy::getAxeValue() > Game-pad " << joyID << " doesn't exist." << std::endl;
  88. return -1;
  89. }
  90. if (axeID < m_pad[joyID]->nbAxes)
  91. return m_pad[joyID]->axeValue[axeID];
  92. else
  93. std::cout << "InputAndJoy::getAxeValue() > Axe number " << axeID << " doesn't exist on pad ";
  94. std::cout << "number " << joyID << " named " << SDL_JoystickName(m_pad[joyID]->joystick) << "." << std::endl;
  95. return -1;
  96. }
  97. bool InputAndJoy::getButtonPad(const Uint8 button, Sint32 joyID) const {
  98. if (joyID >= m_pad.size()) {
  99. std::cout << "InputAndJoy::getButtonPad() > Game-pad " << joyID << " doesn't exist." << std::endl;
  100. return -1;
  101. }
  102. if (button < m_pad[joyID]->nbButtons)
  103. return m_pad[joyID]->buttonValue[button];
  104. else
  105. std::cout << "InputAndJoy::getButtonPad() > Button number " << button << " doesn't exist on pad ";
  106. std::cout << "number " << joyID << " named " << SDL_JoystickName(m_pad[joyID]->joystick) << "." << std::endl;
  107. return false;
  108. }
  109. unsigned long InputAndJoy::getNbPads() const {
  110. return m_pad.size();
  111. }