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.

104 lines
3.5 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 (e.jaxis.axis == 0 || e.jaxis.axis == 3) {
  20. *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
  21. *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
  22. if (e.jaxis.value > state->config.axis_threshold) {
  23. *action |= (0x1 << IZ_ACTION_INDEX_RIGHT);
  24. } else if (e.jaxis.value <= -state->config.axis_threshold) {
  25. *action |= (0x1 << IZ_ACTION_INDEX_LEFT);
  26. }
  27. }
  28. if (e.jaxis.axis == 1 || e.jaxis.axis == 4) {
  29. *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
  30. *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
  31. if (e.jaxis.value > -state->config.axis_threshold) {
  32. *action |= (0x1 << IZ_ACTION_INDEX_DOWN);
  33. } else if (e.jaxis.value <= -state->config.axis_threshold) {
  34. *action |= (0x1 << IZ_ACTION_INDEX_UP);
  35. }
  36. }
  37. }
  38. }
  39. void IZ_HandleJoystickHatEvents(SDL_Event e, IZ_Action* action) {
  40. if (e.type == SDL_JOYHATMOTION) {
  41. *action &= ~(0x1 << IZ_ACTION_INDEX_UP);
  42. *action &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
  43. *action &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
  44. *action &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
  45. if (e.jhat.value != 0) {
  46. *action |= e.jhat.value;
  47. }
  48. }
  49. }
  50. void IZ_HandleJoystickButtonEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
  51. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  52. if (e.jbutton.button == state->config.control_mapping[i]) {
  53. const uint16_t bitflag = (0x1 << i);
  54. if (e.type == SDL_JOYBUTTONDOWN) {
  55. *action |= bitflag;
  56. } else if (e.type == SDL_JOYBUTTONUP) {
  57. *action &= ~bitflag;
  58. }
  59. }
  60. }
  61. }
  62. void IZ_HandleJoystickEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) {
  63. IZ_HandleJoystickDeviceEvents(e, state);
  64. IZ_HandleJoystickAxisEvents(e, state, action);
  65. IZ_HandleJoystickHatEvents(e, action);
  66. IZ_HandleJoystickButtonEvents(e, state, action);
  67. }
  68. void IZ_LoadJoystickConfig(IZ_JoystickConfig* config, uint8_t p) {
  69. char config_path[128];
  70. IZ_GetConfigPath(config_path, 128);
  71. char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping";
  72. joystick_control_mapping_section_name[9] = (char) (48 + p);
  73. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  74. config->control_mapping[i] = ini_getl(joystick_control_mapping_section_name, ACTION_NAMES[i], IZ_DEFAULT_JOYSTICK_CONTROLS[p][i], config_path);
  75. }
  76. char joystick_section_name[] = "Joystick.0";
  77. joystick_section_name[9] = (char) (48 + p);
  78. config->axis_threshold = ini_getl(joystick_section_name, "AxisThreshold", 8000, config_path);
  79. }
  80. void IZ_SaveJoystickConfig(IZ_JoystickConfig* config, uint8_t p) {
  81. char config_path[128];
  82. IZ_GetConfigPath(config_path, 128);
  83. char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping";
  84. joystick_control_mapping_section_name[9] = (char) (48 + p);
  85. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  86. ini_putl(joystick_control_mapping_section_name, ACTION_NAMES[i], config->control_mapping[i], config_path);
  87. }
  88. char joystick_section_name[] = "Joystick.0";
  89. joystick_section_name[9] = (char) (48 + p);
  90. ini_putl(joystick_section_name, "AxisThreshold", config->axis_threshold, config_path);
  91. }