Controller.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include "input/Controller.h"
  5. Controller::ButtonState::ButtonState() : downTime(0), justReleased(false) {
  6. }
  7. Controller::Controller() : activeController(GLFW_JOYSTICK_1) {
  8. }
  9. void Controller::tick() {
  10. if(!glfwJoystickPresent(activeController) && findController()) {
  11. std::cout << "cannot find any controller - resetting buttons\n";
  12. reset();
  13. return;
  14. }
  15. int buttonCount = 0;
  16. const u8* buttonMap = glfwGetJoystickButtons(activeController, &buttonCount);
  17. int axisCount = 0;
  18. const float* axisMap = glfwGetJoystickAxes(activeController, &axisCount);
  19. if(buttonCount < 10 || axisCount < 2) {
  20. std::cout << "cannot find any supported controller - resetting buttons\n";
  21. std::cout << "buttons found: " << buttonCount << "\n";
  22. std::cout << "axes found: " << axisCount << "\n";
  23. reset();
  24. return;
  25. }
  26. increment(Button::A, buttonMap, 1);
  27. increment(Button::B, buttonMap, 2);
  28. increment(Button::X, buttonMap, 0);
  29. increment(Button::Y, buttonMap, 3);
  30. increment(Button::L, buttonMap, 4);
  31. increment(Button::R, buttonMap, 5);
  32. increment(Button::SELECT, buttonMap, 8);
  33. increment(Button::START, buttonMap, 9);
  34. increment(Button::LEFT, axisMap[0] < -0.5f);
  35. increment(Button::RIGHT, axisMap[0] > 0.5f);
  36. increment(Button::UP, axisMap[1] < -0.5f);
  37. increment(Button::DOWN, axisMap[1] > 0.5f);
  38. }
  39. bool Controller::findController() {
  40. for(uint i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
  41. if(glfwJoystickPresent(i)) {
  42. activeController = i;
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. void Controller::reset() {
  49. for(ButtonState& b : buttons) {
  50. b.downTime = 0;
  51. }
  52. }
  53. void Controller::increment(uint button, const u8* mapping, uint index) {
  54. increment(button, mapping[index] != GLFW_RELEASE);
  55. }
  56. void Controller::increment(uint button, bool notReleased) {
  57. buttons[button].justReleased = (buttons[button].downTime > 0 && !notReleased && !buttons[button].justReleased);
  58. bool b = notReleased || buttons[button].justReleased;
  59. buttons[button].downTime = buttons[button].downTime * b + b;
  60. }
  61. uint Controller::getButtonAmount() const {
  62. return buttons.getLength();
  63. }
  64. const char* Controller::getName(uint button) const {
  65. static constexpr const char* buttonNames[] = {
  66. "A", "B", "X", "Y", "L", "R", "Select", "Start", "Left", "Right", "Up", "Down"
  67. };
  68. return buttonNames[button];
  69. }
  70. uint Controller::getDownTime(uint button) const {
  71. return buttons[button].downTime;
  72. }
  73. bool Controller::isDown(uint button) const {
  74. return buttons[button].downTime > 0;
  75. }
  76. bool Controller::wasReleased(uint button) const {
  77. return buttons[button].justReleased;
  78. }