Starter project for SDL2.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.7 KiB

  1. #include "IZ_joystick.h"
  2. void IZ_HandleJoystickDeviceEvents(SDL_Event e, IZ_JoystickState* state) {
  3. if (e.type == SDL_JOYDEVICEADDED) {
  4. if (SDL_NumJoysticks() <= PLAYERS && !state->joystick_instance) {
  5. state->joystick_instance = SDL_JoystickOpen(e.jdevice.which);
  6. }
  7. }
  8. if (e.type == SDL_JOYDEVICEREMOVED) {
  9. if (
  10. state->joystick_instance
  11. && SDL_JoystickInstanceID(state->joystick_instance) == e.jdevice.which
  12. ) {
  13. state->joystick_instance = NULL;
  14. }
  15. }
  16. }
  17. void IZ_HandleJoystickAxisEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
  18. if (e.type == SDL_JOYAXISMOTION) {
  19. if (
  20. e.jaxis.axis == IZ_JOYAXIS_DIRECTION_HORIZONTAL1
  21. || e.jaxis.axis == IZ_JOYAXIS_DIRECTION_HORIZONTAL2
  22. ) {
  23. *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
  24. *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
  25. if (e.jaxis.value > state->config.axis_threshold) {
  26. *action |= (0x1 << IZ_ACTION_INDEX_RIGHT);
  27. return;
  28. }
  29. if (e.jaxis.value <= -state->config.axis_threshold) {
  30. *action |= (0x1 << IZ_ACTION_INDEX_LEFT);
  31. }
  32. return;
  33. }
  34. if (
  35. e.jaxis.axis == IZ_JOYAXIS_DIRECTION_VERTICAL1
  36. || e.jaxis.axis == IZ_JOYAXIS_DIRECTION_VERTICAL2
  37. ) {
  38. *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
  39. *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
  40. if (e.jaxis.value > state->config.axis_threshold) {
  41. *action |= (0x1 << IZ_ACTION_INDEX_DOWN);
  42. return;
  43. }
  44. if (e.jaxis.value <= -state->config.axis_threshold) {
  45. *action |= (0x1 << IZ_ACTION_INDEX_UP);
  46. }
  47. }
  48. }
  49. }
  50. void IZ_HandleJoystickHatEvents(SDL_Event e, IZ_Action* action) {
  51. if (e.type == SDL_JOYHATMOTION) {
  52. *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
  53. *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
  54. *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
  55. *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
  56. if (e.jhat.value != 0) {
  57. *action |= e.jhat.value;
  58. }
  59. }
  60. }
  61. void IZ_HandleJoystickButtonEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
  62. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  63. if (e.jbutton.button == state->config.control_mapping[i]) {
  64. const uint16_t bitflag = (0x1 << i);
  65. if (e.type == SDL_JOYBUTTONDOWN) {
  66. *action |= bitflag;
  67. break;
  68. }
  69. if (e.type == SDL_JOYBUTTONUP) {
  70. *action &= ~bitflag;
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. void IZ_HandleJoystickEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
  77. IZ_HandleJoystickDeviceEvents(e, state);
  78. IZ_HandleJoystickAxisEvents(e, state, action);
  79. IZ_HandleJoystickHatEvents(e, action);
  80. IZ_HandleJoystickButtonEvents(e, state, action);
  81. }
  82. void IZ_LoadJoystickConfig(IZ_JoystickConfig* config, uint8_t p) {
  83. char config_path[128];
  84. IZ_GetConfigPath(config_path, 128);
  85. char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping";
  86. joystick_control_mapping_section_name[9] = (char) (48 + p);
  87. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  88. config->control_mapping[i] = ini_getl(joystick_control_mapping_section_name, ACTION_NAMES[i], IZ_DEFAULT_JOYSTICK_CONTROLS[p][i], config_path);
  89. }
  90. char joystick_section_name[] = "Joystick.0";
  91. joystick_section_name[9] = (char) (48 + p);
  92. config->axis_threshold = ini_getl(joystick_section_name, "AxisThreshold", 8000, config_path);
  93. }
  94. void IZ_SaveJoystickConfig(IZ_JoystickConfig* config, uint8_t p) {
  95. char config_path[128];
  96. IZ_GetConfigPath(config_path, 128);
  97. char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping";
  98. joystick_control_mapping_section_name[9] = (char) (48 + p);
  99. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  100. ini_putl(joystick_control_mapping_section_name, ACTION_NAMES[i], config->control_mapping[i], config_path);
  101. }
  102. char joystick_section_name[] = "Joystick.0";
  103. joystick_section_name[9] = (char) (48 + p);
  104. ini_putl(joystick_section_name, "AxisThreshold", config->axis_threshold, config_path);
  105. }