gamepadutils.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Gamepad image */
  11. typedef struct GamepadImage GamepadImage;
  12. typedef enum
  13. {
  14. CONTROLLER_MODE_TESTING,
  15. CONTROLLER_MODE_BINDING,
  16. } ControllerDisplayMode;
  17. enum
  18. {
  19. SDL_GAMEPAD_ELEMENT_INVALID = -1,
  20. /* ... SDL_GamepadButton ... */
  21. SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE = SDL_GAMEPAD_BUTTON_COUNT,
  22. SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE,
  23. SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE,
  24. SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE,
  25. SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE,
  26. SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE,
  27. SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE,
  28. SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE,
  29. SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER,
  30. SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER,
  31. SDL_GAMEPAD_ELEMENT_AXIS_MAX,
  32. SDL_GAMEPAD_ELEMENT_NAME = SDL_GAMEPAD_ELEMENT_AXIS_MAX,
  33. SDL_GAMEPAD_ELEMENT_TYPE,
  34. SDL_GAMEPAD_ELEMENT_MAX,
  35. };
  36. #define HIGHLIGHT_COLOR 224, 255, 255, SDL_ALPHA_OPAQUE
  37. #define HIGHLIGHT_TEXTURE_MOD 224, 255, 255
  38. #define PRESSED_COLOR 175, 238, 238, SDL_ALPHA_OPAQUE
  39. #define PRESSED_TEXTURE_MOD 175, 238, 238
  40. #define SELECTED_COLOR 224, 255, 224, SDL_ALPHA_OPAQUE
  41. #define GYRO_COLOR_RED 255, 0, 0, SDL_ALPHA_OPAQUE
  42. #define GYRO_COLOR_GREEN 0, 255, 0, SDL_ALPHA_OPAQUE
  43. #define GYRO_COLOR_BLUE 0, 0, 255, SDL_ALPHA_OPAQUE
  44. #define GYRO_COLOR_ORANGE 255, 128, 0, SDL_ALPHA_OPAQUE
  45. /* Shared layout constants */
  46. #define BUTTON_PADDING 12.0f
  47. #define MINIMUM_BUTTON_WIDTH 96.0f
  48. /* Symbol */
  49. #define DEGREE_UTF8 "\xC2\xB0"
  50. #define SQUARED_UTF8 "\xC2\xB2"
  51. #define MICRO_UTF8 "\xC2\xB5"
  52. /* Gamepad image display */
  53. extern GamepadImage *CreateGamepadImage(SDL_Renderer *renderer);
  54. extern void SetGamepadImagePosition(GamepadImage *ctx, float x, float y);
  55. extern void GetGamepadImageArea(GamepadImage *ctx, SDL_FRect *area);
  56. extern void GetGamepadTouchpadArea(GamepadImage *ctx, SDL_FRect *area);
  57. extern void SetGamepadImageShowingFront(GamepadImage *ctx, bool showing_front);
  58. extern SDL_GamepadType GetGamepadImageType(GamepadImage *ctx);
  59. extern void SetGamepadImageDisplayMode(GamepadImage *ctx, ControllerDisplayMode display_mode);
  60. extern float GetGamepadImageButtonWidth(GamepadImage *ctx);
  61. extern float GetGamepadImageButtonHeight(GamepadImage *ctx);
  62. extern float GetGamepadImageAxisWidth(GamepadImage *ctx);
  63. extern float GetGamepadImageAxisHeight(GamepadImage *ctx);
  64. extern int GetGamepadImageElementAt(GamepadImage *ctx, float x, float y);
  65. extern void ClearGamepadImage(GamepadImage *ctx);
  66. extern void SetGamepadImageElement(GamepadImage *ctx, int element, bool active);
  67. extern void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad);
  68. extern void RenderGamepadImage(GamepadImage *ctx);
  69. extern void DestroyGamepadImage(GamepadImage *ctx);
  70. /* Gamepad element display */
  71. typedef struct GamepadDisplay GamepadDisplay;
  72. extern GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer);
  73. extern void SetGamepadDisplayDisplayMode(GamepadDisplay *ctx, ControllerDisplayMode display_mode);
  74. extern void SetGamepadDisplayArea(GamepadDisplay *ctx, const SDL_FRect *area);
  75. extern void SetGamepadDisplayGyroDriftCorrection(GamepadDisplay *ctx, float *gyro_drift_correction);
  76. extern int GetGamepadDisplayElementAt(GamepadDisplay *ctx, SDL_Gamepad *gamepad, float x, float y);
  77. extern void SetGamepadDisplayHighlight(GamepadDisplay *ctx, int element, bool pressed);
  78. extern void SetGamepadDisplaySelected(GamepadDisplay *ctx, int element);
  79. extern void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad);
  80. extern void DestroyGamepadDisplay(GamepadDisplay *ctx);
  81. /* Gamepad type display */
  82. enum
  83. {
  84. SDL_GAMEPAD_TYPE_UNSELECTED = -1
  85. };
  86. typedef struct GamepadTypeDisplay GamepadTypeDisplay;
  87. extern GamepadTypeDisplay *CreateGamepadTypeDisplay(SDL_Renderer *renderer);
  88. extern void SetGamepadTypeDisplayArea(GamepadTypeDisplay *ctx, const SDL_FRect *area);
  89. extern int GetGamepadTypeDisplayAt(GamepadTypeDisplay *ctx, float x, float y);
  90. extern void SetGamepadTypeDisplayHighlight(GamepadTypeDisplay *ctx, int type, bool pressed);
  91. extern void SetGamepadTypeDisplaySelected(GamepadTypeDisplay *ctx, int type);
  92. extern void SetGamepadTypeDisplayRealType(GamepadTypeDisplay *ctx, SDL_GamepadType type);
  93. extern void RenderGamepadTypeDisplay(GamepadTypeDisplay *ctx);
  94. extern void DestroyGamepadTypeDisplay(GamepadTypeDisplay *ctx);
  95. /* Joystick element display */
  96. typedef struct JoystickDisplay JoystickDisplay;
  97. extern JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer);
  98. extern void SetJoystickDisplayArea(JoystickDisplay *ctx, const SDL_FRect *area);
  99. extern char *GetJoystickDisplayElementAt(JoystickDisplay *ctx, SDL_Joystick *joystick, float x, float y);
  100. extern void SetJoystickDisplayHighlight(JoystickDisplay *ctx, const char *element, bool pressed);
  101. extern void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick);
  102. extern void DestroyJoystickDisplay(JoystickDisplay *ctx);
  103. /* Simple buttons */
  104. typedef struct GamepadButton GamepadButton;
  105. extern GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label);
  106. extern void SetGamepadButtonLabel(GamepadButton *ctx, const char *label);
  107. extern void SetGamepadButtonArea(GamepadButton *ctx, const SDL_FRect *area);
  108. extern void GetGamepadButtonArea(GamepadButton *ctx, SDL_FRect *area);
  109. extern void SetGamepadButtonHighlight(GamepadButton *ctx, bool highlight, bool pressed);
  110. extern float GetGamepadButtonLabelWidth(GamepadButton *ctx);
  111. extern float GetGamepadButtonLabelHeight(GamepadButton *ctx);
  112. extern bool GamepadButtonContains(GamepadButton *ctx, float x, float y);
  113. extern void RenderGamepadButton(GamepadButton *ctx);
  114. extern void DestroyGamepadButton(GamepadButton *ctx);
  115. /* Gyro element Display */
  116. /* This is used as the initial noise tolerance threshold. It's set very close to zero to avoid divide by zero while we're evaluating the noise profile. Each controller may have a very different noise profile.*/
  117. #define ACCELEROMETER_NOISE_THRESHOLD 1e-6f
  118. /* The value below is based on observation of a Dualshock controller. Of all gamepads observed, the Dualshock (PS4) tends to have one of the noisiest accelerometers. Increase this threshold if a controller is failing to pass the noise profiling stage while stationary on a table. */
  119. #define ACCELEROMETER_MAX_NOISE_G 0.075f
  120. #define ACCELEROMETER_MAX_NOISE_G_SQ (ACCELEROMETER_MAX_NOISE_G * ACCELEROMETER_MAX_NOISE_G)
  121. /* Gyro Calibration Phases */
  122. typedef enum
  123. {
  124. GYRO_CALIBRATION_PHASE_OFF, /* Calibration has not yet been evaluated - signal to the user to put the controller on a flat surface before beginning the calibration process */
  125. GYRO_CALIBRATION_PHASE_NOISE_PROFILING, /* Find the max accelerometer noise for a fixed period */
  126. GYRO_CALIBRATION_PHASE_DRIFT_PROFILING, /* Find the drift while the accelerometer is below the accelerometer noise tolerance */
  127. GYRO_CALIBRATION_PHASE_COMPLETE, /* Calibration has finished */
  128. } EGyroCalibrationPhase;
  129. typedef struct Quaternion Quaternion;
  130. typedef struct GyroDisplay GyroDisplay;
  131. extern void InitCirclePoints3D();
  132. extern GyroDisplay *CreateGyroDisplay(SDL_Renderer *renderer);
  133. extern void SetGyroDisplayArea(GyroDisplay *ctx, const SDL_FRect *area);
  134. extern void SetGamepadDisplayIMUValues(GyroDisplay *ctx, float *gyro_drift_solution, float *euler_displacement_angles, Quaternion *gyro_quaternion, int reported_senor_rate_hz, int estimated_sensor_rate_hz, EGyroCalibrationPhase calibration_phase, float drift_calibration_progress_frac, float accelerometer_noise_sq, float accelerometer_noise_tolerance_sq);
  135. extern GamepadButton *GetGyroResetButton(GyroDisplay *ctx);
  136. extern GamepadButton *GetGyroCalibrateButton(GyroDisplay *ctx);
  137. extern void RenderGyroDisplay(GyroDisplay *ctx, GamepadDisplay *gamepadElements, SDL_Gamepad *gamepad);
  138. extern void DestroyGyroDisplay(GyroDisplay *ctx);
  139. /* Working with mappings and bindings */
  140. /* Return whether a mapping has any bindings */
  141. extern bool MappingHasBindings(const char *mapping);
  142. /* Return true if the mapping has a controller name */
  143. extern bool MappingHasName(const char *mapping);
  144. /* Return the name from a mapping, which should be freed using SDL_free(), or NULL if there is no name specified */
  145. extern char *GetMappingName(const char *mapping);
  146. /* Set the name in a mapping, freeing the mapping passed in and returning a new mapping */
  147. extern char *SetMappingName(char *mapping, const char *name);
  148. /* Get the friendly string for an SDL_GamepadType */
  149. extern const char *GetGamepadTypeString(SDL_GamepadType type);
  150. /* Return the type from a mapping, which should be freed using SDL_free(), or NULL if there is no type specified */
  151. extern SDL_GamepadType GetMappingType(const char *mapping);
  152. /* Set the type in a mapping, freeing the mapping passed in and returning a new mapping */
  153. extern char *SetMappingType(char *mapping, SDL_GamepadType type);
  154. /* Return true if a mapping has this element bound */
  155. extern bool MappingHasElement(const char *mapping, int element);
  156. /* Get the binding for an element, which should be freed using SDL_free(), or NULL if the element isn't bound */
  157. extern char *GetElementBinding(const char *mapping, int element);
  158. /* Set the binding for an element, or NULL to clear it, freeing the mapping passed in and returning a new mapping */
  159. extern char *SetElementBinding(char *mapping, int element, const char *binding);
  160. /* Get the element for a binding, or SDL_GAMEPAD_ELEMENT_INVALID if that binding isn't used */
  161. extern int GetElementForBinding(char *mapping, const char *binding);
  162. /* Return true if a mapping contains this binding */
  163. extern bool MappingHasBinding(const char *mapping, const char *binding);
  164. /* Clear any previous binding */
  165. extern char *ClearMappingBinding(char *mapping, const char *binding);