diff options
Diffstat (limited to 'contrib/SDL-3.2.8/examples/input/02-joystick-events/joystick-events.c')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/input/02-joystick-events/joystick-events.c | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/input/02-joystick-events/joystick-events.c b/contrib/SDL-3.2.8/examples/input/02-joystick-events/joystick-events.c new file mode 100644 index 0000000..cc01d84 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/input/02-joystick-events/joystick-events.c | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | /* | ||
| 2 | * This example code looks for joystick input in the event handler, and | ||
| 3 | * reports any changes as a flood of info. | ||
| 4 | * | ||
| 5 | * This code is public domain. Feel free to use it for any purpose! | ||
| 6 | */ | ||
| 7 | |||
| 8 | /* Joysticks are low-level interfaces: there's something with a bunch of | ||
| 9 | buttons, axes and hats, in no understood order or position. This is | ||
| 10 | a flexible interface, but you'll need to build some sort of configuration | ||
| 11 | UI to let people tell you what button, etc, does what. On top of this | ||
| 12 | interface, SDL offers the "gamepad" API, which works with lots of devices, | ||
| 13 | and knows how to map arbitrary buttons and such to look like an | ||
| 14 | Xbox/PlayStation/etc gamepad. This is easier, and better, for many games, | ||
| 15 | but isn't necessarily a good fit for complex apps and hardware. A flight | ||
| 16 | simulator, a realistic racing game, etc, might want this interface instead | ||
| 17 | of gamepads. */ | ||
| 18 | |||
| 19 | #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ | ||
| 20 | #include <SDL3/SDL.h> | ||
| 21 | #include <SDL3/SDL_main.h> | ||
| 22 | |||
| 23 | /* We will use this renderer to draw into this window every frame. */ | ||
| 24 | static SDL_Window *window = NULL; | ||
| 25 | static SDL_Renderer *renderer = NULL; | ||
| 26 | static SDL_Color colors[64]; | ||
| 27 | |||
| 28 | #define MOTION_EVENT_COOLDOWN 40 | ||
| 29 | |||
| 30 | typedef struct EventMessage | ||
| 31 | { | ||
| 32 | char *str; | ||
| 33 | SDL_Color color; | ||
| 34 | Uint64 start_ticks; | ||
| 35 | struct EventMessage *next; | ||
| 36 | } EventMessage; | ||
| 37 | |||
| 38 | static EventMessage messages; | ||
| 39 | static EventMessage *messages_tail = &messages; | ||
| 40 | |||
| 41 | static const char *hat_state_string(Uint8 state) | ||
| 42 | { | ||
| 43 | switch (state) { | ||
| 44 | case SDL_HAT_CENTERED: return "CENTERED"; | ||
| 45 | case SDL_HAT_UP: return "UP"; | ||
| 46 | case SDL_HAT_RIGHT: return "RIGHT"; | ||
| 47 | case SDL_HAT_DOWN: return "DOWN"; | ||
| 48 | case SDL_HAT_LEFT: return "LEFT"; | ||
| 49 | case SDL_HAT_RIGHTUP: return "RIGHT+UP"; | ||
| 50 | case SDL_HAT_RIGHTDOWN: return "RIGHT+DOWN"; | ||
| 51 | case SDL_HAT_LEFTUP: return "LEFT+UP"; | ||
| 52 | case SDL_HAT_LEFTDOWN: return "LEFT+DOWN"; | ||
| 53 | default: break; | ||
| 54 | } | ||
| 55 | return "UNKNOWN"; | ||
| 56 | } | ||
| 57 | |||
| 58 | static const char *battery_state_string(SDL_PowerState state) | ||
| 59 | { | ||
| 60 | switch (state) { | ||
| 61 | case SDL_POWERSTATE_ERROR: return "ERROR"; | ||
| 62 | case SDL_POWERSTATE_UNKNOWN: return "UNKNOWN"; | ||
| 63 | case SDL_POWERSTATE_ON_BATTERY: return "ON BATTERY"; | ||
| 64 | case SDL_POWERSTATE_NO_BATTERY: return "NO BATTERY"; | ||
| 65 | case SDL_POWERSTATE_CHARGING: return "CHARGING"; | ||
| 66 | case SDL_POWERSTATE_CHARGED: return "CHARGED"; | ||
| 67 | default: break; | ||
| 68 | } | ||
| 69 | return "UNKNOWN"; | ||
| 70 | } | ||
| 71 | |||
| 72 | static void add_message(SDL_JoystickID jid, const char *fmt, ...) | ||
| 73 | { | ||
| 74 | const SDL_Color *color = &colors[((size_t) jid) % SDL_arraysize(colors)]; | ||
| 75 | EventMessage *msg = NULL; | ||
| 76 | char *str = NULL; | ||
| 77 | va_list ap; | ||
| 78 | |||
| 79 | msg = (EventMessage *) SDL_calloc(1, sizeof (*msg)); | ||
| 80 | if (!msg) { | ||
| 81 | return; // oh well. | ||
| 82 | } | ||
| 83 | |||
| 84 | va_start(ap, fmt); | ||
| 85 | SDL_vasprintf(&str, fmt, ap); | ||
| 86 | va_end(ap); | ||
| 87 | if (!str) { | ||
| 88 | SDL_free(msg); | ||
| 89 | return; // oh well. | ||
| 90 | } | ||
| 91 | |||
| 92 | msg->str = str; | ||
| 93 | SDL_copyp(&msg->color, color); | ||
| 94 | msg->start_ticks = SDL_GetTicks(); | ||
| 95 | msg->next = NULL; | ||
| 96 | |||
| 97 | messages_tail->next = msg; | ||
| 98 | messages_tail = msg; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | /* This function runs once at startup. */ | ||
| 103 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 104 | { | ||
| 105 | int i; | ||
| 106 | |||
| 107 | SDL_SetAppMetadata("Example Input Joystick Events", "1.0", "com.example.input-joystick-events"); | ||
| 108 | |||
| 109 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)) { | ||
| 110 | SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | ||
| 111 | return SDL_APP_FAILURE; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (!SDL_CreateWindowAndRenderer("examples/input/joystick-events", 640, 480, 0, &window, &renderer)) { | ||
| 115 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 116 | return SDL_APP_FAILURE; | ||
| 117 | } | ||
| 118 | |||
| 119 | colors[0].r = colors[0].g = colors[0].b = colors[0].a = 255; | ||
| 120 | for (i = 1; i < SDL_arraysize(colors); i++) { | ||
| 121 | colors[i].r = SDL_rand(255); | ||
| 122 | colors[i].g = SDL_rand(255); | ||
| 123 | colors[i].b = SDL_rand(255); | ||
| 124 | colors[i].a = 255; | ||
| 125 | } | ||
| 126 | |||
| 127 | add_message(0, "Please plug in a joystick."); | ||
| 128 | |||
| 129 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 130 | } | ||
| 131 | |||
| 132 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 133 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 134 | { | ||
| 135 | if (event->type == SDL_EVENT_QUIT) { | ||
| 136 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 137 | } else if (event->type == SDL_EVENT_JOYSTICK_ADDED) { | ||
| 138 | /* this event is sent for each hotplugged stick, but also each already-connected joystick during SDL_Init(). */ | ||
| 139 | const SDL_JoystickID which = event->jdevice.which; | ||
| 140 | SDL_Joystick *joystick = SDL_OpenJoystick(which); | ||
| 141 | if (!joystick) { | ||
| 142 | add_message(which, "Joystick #%u add, but not opened: %s", (unsigned int) which, SDL_GetError()); | ||
| 143 | } else { | ||
| 144 | add_message(which, "Joystick #%u ('%s') added", (unsigned int) which, SDL_GetJoystickName(joystick)); | ||
| 145 | } | ||
| 146 | } else if (event->type == SDL_EVENT_JOYSTICK_REMOVED) { | ||
| 147 | const SDL_JoystickID which = event->jdevice.which; | ||
| 148 | SDL_Joystick *joystick = SDL_GetJoystickFromID(which); | ||
| 149 | if (joystick) { | ||
| 150 | SDL_CloseJoystick(joystick); /* the joystick was unplugged. */ | ||
| 151 | } | ||
| 152 | add_message(which, "Joystick #%u removed", (unsigned int) which); | ||
| 153 | } else if (event->type == SDL_EVENT_JOYSTICK_AXIS_MOTION) { | ||
| 154 | static Uint64 axis_motion_cooldown_time = 0; /* these are spammy, only show every X milliseconds. */ | ||
| 155 | const Uint64 now = SDL_GetTicks(); | ||
| 156 | if (now >= axis_motion_cooldown_time) { | ||
| 157 | const SDL_JoystickID which = event->jaxis.which; | ||
| 158 | axis_motion_cooldown_time = now + MOTION_EVENT_COOLDOWN; | ||
| 159 | add_message(which, "Joystick #%u axis %d -> %d", (unsigned int) which, (int) event->jaxis.axis, (int) event->jaxis.value); | ||
| 160 | } | ||
| 161 | } else if (event->type == SDL_EVENT_JOYSTICK_BALL_MOTION) { | ||
| 162 | static Uint64 ball_motion_cooldown_time = 0; /* these are spammy, only show every X milliseconds. */ | ||
| 163 | const Uint64 now = SDL_GetTicks(); | ||
| 164 | if (now >= ball_motion_cooldown_time) { | ||
| 165 | const SDL_JoystickID which = event->jball.which; | ||
| 166 | ball_motion_cooldown_time = now + MOTION_EVENT_COOLDOWN; | ||
| 167 | add_message(which, "Joystick #%u ball %d -> %d, %d", (unsigned int) which, (int) event->jball.ball, (int) event->jball.xrel, (int) event->jball.yrel); | ||
| 168 | } | ||
| 169 | } else if (event->type == SDL_EVENT_JOYSTICK_HAT_MOTION) { | ||
| 170 | const SDL_JoystickID which = event->jhat.which; | ||
| 171 | add_message(which, "Joystick #%u hat %d -> %s", (unsigned int) which, (int) event->jhat.hat, hat_state_string(event->jhat.value)); | ||
| 172 | } else if ((event->type == SDL_EVENT_JOYSTICK_BUTTON_UP) || (event->type == SDL_EVENT_JOYSTICK_BUTTON_DOWN)) { | ||
| 173 | const SDL_JoystickID which = event->jbutton.which; | ||
| 174 | add_message(which, "Joystick #%u button %d -> %s", (unsigned int) which, (int) event->jbutton.button, event->jbutton.down ? "PRESSED" : "RELEASED"); | ||
| 175 | } else if (event->type == SDL_EVENT_JOYSTICK_BATTERY_UPDATED) { | ||
| 176 | const SDL_JoystickID which = event->jbattery.which; | ||
| 177 | add_message(which, "Joystick #%u battery -> %s - %d%%", (unsigned int) which, battery_state_string(event->jbattery.state), event->jbattery.percent); | ||
| 178 | } | ||
| 179 | |||
| 180 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 181 | } | ||
| 182 | |||
| 183 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 184 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 185 | { | ||
| 186 | const Uint64 now = SDL_GetTicks(); | ||
| 187 | const float msg_lifetime = 3500.0f; /* milliseconds a message lives for. */ | ||
| 188 | EventMessage *msg = messages.next; | ||
| 189 | float prev_y = 0.0f; | ||
| 190 | int winw = 640, winh = 480; | ||
| 191 | |||
| 192 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
| 193 | SDL_RenderClear(renderer); | ||
| 194 | SDL_GetWindowSize(window, &winw, &winh); | ||
| 195 | |||
| 196 | while (msg) { | ||
| 197 | float x, y; | ||
| 198 | const float life_percent = ((float) (now - msg->start_ticks)) / msg_lifetime; | ||
| 199 | if (life_percent >= 1.0f) { /* msg is done. */ | ||
| 200 | messages.next = msg->next; | ||
| 201 | if (messages_tail == msg) { | ||
| 202 | messages_tail = &messages; | ||
| 203 | } | ||
| 204 | SDL_free(msg->str); | ||
| 205 | SDL_free(msg); | ||
| 206 | msg = messages.next; | ||
| 207 | continue; | ||
| 208 | } | ||
| 209 | x = (((float) winw) - (SDL_strlen(msg->str) * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE)) / 2.0f; | ||
| 210 | y = ((float) winh) * life_percent; | ||
| 211 | if ((prev_y != 0.0f) && ((prev_y - y) < ((float) SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE))) { | ||
| 212 | msg->start_ticks = now; | ||
| 213 | break; // wait for the previous message to tick up a little. | ||
| 214 | } | ||
| 215 | |||
| 216 | SDL_SetRenderDrawColor(renderer, msg->color.r, msg->color.g, msg->color.b, (Uint8) (((float) msg->color.a) * (1.0f - life_percent))); | ||
| 217 | SDL_RenderDebugText(renderer, x, y, msg->str); | ||
| 218 | |||
| 219 | prev_y = y; | ||
| 220 | msg = msg->next; | ||
| 221 | } | ||
| 222 | |||
| 223 | SDL_RenderPresent(renderer); | ||
| 224 | |||
| 225 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 226 | } | ||
| 227 | |||
| 228 | /* This function runs once at shutdown. */ | ||
| 229 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 230 | { | ||
| 231 | /* SDL will clean up the window/renderer for us. We let the joysticks leak. */ | ||
| 232 | } | ||
