Atlas - gamepadutils.h

Home / ext / SDL / test Lines: 1 | Size: 10298 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely. 11*/ 12 13/* Gamepad image */ 14 15#ifndef gamepadutils_h_ 16#define gamepadutils_h_ 17 18typedef struct GamepadImage GamepadImage; 19 20typedef enum 21{ 22 CONTROLLER_MODE_TESTING, 23 CONTROLLER_MODE_BINDING, 24} ControllerDisplayMode; 25 26enum 27{ 28 SDL_GAMEPAD_ELEMENT_INVALID = -1, 29 30 /* ... SDL_GamepadButton ... */ 31 32 SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_NEGATIVE = SDL_GAMEPAD_BUTTON_COUNT, 33 SDL_GAMEPAD_ELEMENT_AXIS_LEFTX_POSITIVE, 34 SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_NEGATIVE, 35 SDL_GAMEPAD_ELEMENT_AXIS_LEFTY_POSITIVE, 36 SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_NEGATIVE, 37 SDL_GAMEPAD_ELEMENT_AXIS_RIGHTX_POSITIVE, 38 SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_NEGATIVE, 39 SDL_GAMEPAD_ELEMENT_AXIS_RIGHTY_POSITIVE, 40 SDL_GAMEPAD_ELEMENT_AXIS_LEFT_TRIGGER, 41 SDL_GAMEPAD_ELEMENT_AXIS_RIGHT_TRIGGER, 42 SDL_GAMEPAD_ELEMENT_AXIS_MAX, 43 44 SDL_GAMEPAD_ELEMENT_NAME = SDL_GAMEPAD_ELEMENT_AXIS_MAX, 45 SDL_GAMEPAD_ELEMENT_TYPE, 46 SDL_GAMEPAD_ELEMENT_MAX, 47}; 48 49#define HIGHLIGHT_COLOR 224, 255, 255, SDL_ALPHA_OPAQUE 50#define HIGHLIGHT_TEXTURE_MOD 224, 255, 255 51#define PRESSED_COLOR 175, 238, 238, SDL_ALPHA_OPAQUE 52#define PRESSED_TEXTURE_MOD 175, 238, 238 53#define SELECTED_COLOR 224, 255, 224, SDL_ALPHA_OPAQUE 54#define GYRO_COLOR_RED 255, 0, 0, SDL_ALPHA_OPAQUE 55#define GYRO_COLOR_GREEN 0, 255, 0, SDL_ALPHA_OPAQUE 56#define GYRO_COLOR_BLUE 0, 0, 255, SDL_ALPHA_OPAQUE 57#define GYRO_COLOR_ORANGE 255, 128, 0, SDL_ALPHA_OPAQUE 58 59/* Shared layout constants */ 60#define BUTTON_PADDING 12.0f 61#define MINIMUM_BUTTON_WIDTH 96.0f 62 63/* Symbol */ 64#define DEGREE_UTF8 "\xC2\xB0" 65#define SQUARED_UTF8 "\xC2\xB2" 66#define MICRO_UTF8 "\xC2\xB5" 67/* Gamepad image display */ 68 69extern GamepadImage *CreateGamepadImage(SDL_Renderer *renderer); 70extern void SetGamepadImagePosition(GamepadImage *ctx, float x, float y); 71extern void GetGamepadImageArea(GamepadImage *ctx, SDL_FRect *area); 72extern void GetGamepadTouchpadArea(GamepadImage *ctx, SDL_FRect *area); 73extern void SetGamepadImageShowingFront(GamepadImage *ctx, bool showing_front); 74extern SDL_GamepadType GetGamepadImageType(GamepadImage *ctx); 75extern void SetGamepadImageDisplayMode(GamepadImage *ctx, ControllerDisplayMode display_mode); 76extern float GetGamepadImageButtonWidth(GamepadImage *ctx); 77extern float GetGamepadImageButtonHeight(GamepadImage *ctx); 78extern float GetGamepadImageAxisWidth(GamepadImage *ctx); 79extern float GetGamepadImageAxisHeight(GamepadImage *ctx); 80extern int GetGamepadImageElementAt(GamepadImage *ctx, float x, float y); 81 82extern void ClearGamepadImage(GamepadImage *ctx); 83extern void SetGamepadImageElement(GamepadImage *ctx, int element, bool active); 84 85extern void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad); 86extern void RenderGamepadImage(GamepadImage *ctx); 87extern void DestroyGamepadImage(GamepadImage *ctx); 88 89/* Gamepad element display */ 90 91typedef struct GamepadDisplay GamepadDisplay; 92 93extern GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer); 94extern void SetGamepadDisplayDisplayMode(GamepadDisplay *ctx, ControllerDisplayMode display_mode); 95extern void SetGamepadDisplayArea(GamepadDisplay *ctx, const SDL_FRect *area); 96extern void SetGamepadDisplayGyroDriftCorrection(GamepadDisplay *ctx, float *gyro_drift_correction); 97extern int GetGamepadDisplayElementAt(GamepadDisplay *ctx, SDL_Gamepad *gamepad, float x, float y); 98extern void SetGamepadDisplayHighlight(GamepadDisplay *ctx, int element, bool pressed); 99extern void SetGamepadDisplaySelected(GamepadDisplay *ctx, int element); 100extern void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad); 101extern void DestroyGamepadDisplay(GamepadDisplay *ctx); 102 103/* Gamepad type display */ 104 105enum 106{ 107 SDL_GAMEPAD_TYPE_UNSELECTED = -1 108}; 109 110typedef struct GamepadTypeDisplay GamepadTypeDisplay; 111 112extern GamepadTypeDisplay *CreateGamepadTypeDisplay(SDL_Renderer *renderer); 113extern void SetGamepadTypeDisplayArea(GamepadTypeDisplay *ctx, const SDL_FRect *area); 114extern int GetGamepadTypeDisplayAt(GamepadTypeDisplay *ctx, float x, float y); 115extern void SetGamepadTypeDisplayHighlight(GamepadTypeDisplay *ctx, int type, bool pressed); 116extern void SetGamepadTypeDisplaySelected(GamepadTypeDisplay *ctx, int type); 117extern void SetGamepadTypeDisplayRealType(GamepadTypeDisplay *ctx, SDL_GamepadType type); 118extern void RenderGamepadTypeDisplay(GamepadTypeDisplay *ctx); 119extern void DestroyGamepadTypeDisplay(GamepadTypeDisplay *ctx); 120 121/* Joystick element display */ 122 123typedef struct JoystickDisplay JoystickDisplay; 124 125extern JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer); 126extern void SetJoystickDisplayArea(JoystickDisplay *ctx, const SDL_FRect *area); 127extern char *GetJoystickDisplayElementAt(JoystickDisplay *ctx, SDL_Joystick *joystick, float x, float y); 128extern void SetJoystickDisplayHighlight(JoystickDisplay *ctx, const char *element, bool pressed); 129extern void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick); 130extern void DestroyJoystickDisplay(JoystickDisplay *ctx); 131 132/* Simple buttons */ 133 134typedef struct GamepadButton GamepadButton; 135 136extern GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label); 137extern void SetGamepadButtonLabel(GamepadButton *ctx, const char *label); 138extern void SetGamepadButtonArea(GamepadButton *ctx, const SDL_FRect *area); 139extern void GetGamepadButtonArea(GamepadButton *ctx, SDL_FRect *area); 140extern void SetGamepadButtonHighlight(GamepadButton *ctx, bool highlight, bool pressed); 141extern float GetGamepadButtonLabelWidth(GamepadButton *ctx); 142extern float GetGamepadButtonLabelHeight(GamepadButton *ctx); 143extern bool GamepadButtonContains(GamepadButton *ctx, float x, float y); 144extern void RenderGamepadButton(GamepadButton *ctx); 145extern void DestroyGamepadButton(GamepadButton *ctx); 146 147/* Gyro element Display */ 148 149/* 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.*/ 150#define ACCELEROMETER_NOISE_THRESHOLD 1e-6f 151/* 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. */ 152#define ACCELEROMETER_MAX_NOISE_G 0.075f 153#define ACCELEROMETER_MAX_NOISE_G_SQ (ACCELEROMETER_MAX_NOISE_G * ACCELEROMETER_MAX_NOISE_G) 154 155/* Gyro Calibration Phases */ 156typedef enum 157{ 158 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 */ 159 GYRO_CALIBRATION_PHASE_NOISE_PROFILING, /* Find the max accelerometer noise for a fixed period */ 160 GYRO_CALIBRATION_PHASE_DRIFT_PROFILING, /* Find the drift while the accelerometer is below the accelerometer noise tolerance */ 161 GYRO_CALIBRATION_PHASE_COMPLETE, /* Calibration has finished */ 162} EGyroCalibrationPhase; 163 164typedef struct Quaternion Quaternion; 165typedef struct GyroDisplay GyroDisplay; 166 167extern void InitCirclePoints3D(void); 168extern GyroDisplay *CreateGyroDisplay(SDL_Renderer *renderer); 169extern void SetGyroDisplayArea(GyroDisplay *ctx, const SDL_FRect *area); 170extern 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); 171extern GamepadButton *GetGyroResetButton(GyroDisplay *ctx); 172extern GamepadButton *GetGyroCalibrateButton(GyroDisplay *ctx); 173extern void RenderGyroDisplay(GyroDisplay *ctx, GamepadDisplay *gamepadElements, SDL_Gamepad *gamepad); 174extern void DestroyGyroDisplay(GyroDisplay *ctx); 175 176/* Working with mappings and bindings */ 177 178/* Return whether a mapping has any bindings */ 179extern bool MappingHasBindings(const char *mapping); 180 181/* Return true if the mapping has a controller name */ 182extern bool MappingHasName(const char *mapping); 183 184/* Return the name from a mapping, which should be freed using SDL_free(), or NULL if there is no name specified */ 185extern char *GetMappingName(const char *mapping); 186 187/* Set the name in a mapping, freeing the mapping passed in and returning a new mapping */ 188extern char *SetMappingName(char *mapping, const char *name); 189 190/* Get the friendly string for an SDL_GamepadType */ 191extern const char *GetGamepadTypeString(SDL_GamepadType type); 192 193/* Return the type from a mapping, which should be freed using SDL_free(), or NULL if there is no type specified */ 194extern SDL_GamepadType GetMappingType(const char *mapping); 195 196/* Set the type in a mapping, freeing the mapping passed in and returning a new mapping */ 197extern char *SetMappingType(char *mapping, SDL_GamepadType type); 198 199/* Return true if a mapping has this element bound */ 200extern bool MappingHasElement(const char *mapping, int element); 201 202/* Get the binding for an element, which should be freed using SDL_free(), or NULL if the element isn't bound */ 203extern char *GetElementBinding(const char *mapping, int element); 204 205/* Set the binding for an element, or NULL to clear it, freeing the mapping passed in and returning a new mapping */ 206extern char *SetElementBinding(char *mapping, int element, const char *binding); 207 208/* Get the element for a binding, or SDL_GAMEPAD_ELEMENT_INVALID if that binding isn't used */ 209extern int GetElementForBinding(char *mapping, const char *binding); 210 211/* Return true if a mapping contains this binding */ 212extern bool MappingHasBinding(const char *mapping, const char *binding); 213 214/* Clear any previous binding */ 215extern char *ClearMappingBinding(char *mapping, const char *binding); 216 217#endif /* gamepadutils_h_ */ 218
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.