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.

IZ_joystick.c 3.7 KiB

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