diff options
author | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
commit | 6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch) | |
tree | 34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/examples/input | |
parent | 8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff) |
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/input')
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/README.txt | 2 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/joystick-polling.c | 193 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/onmouseover.webp | bin | 0 -> 47840 bytes | |||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/thumbnail.png | bin | 0 -> 5940 bytes | |||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/02-joystick-events/README.txt | 2 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/02-joystick-events/joystick-events.c | 232 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/02-joystick-events/onmouseover.webp | bin | 0 -> 2302260 bytes | |||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/02-joystick-events/thumbnail.png | bin | 0 -> 30785 bytes |
8 files changed, 429 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/README.txt b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/README.txt new file mode 100644 index 0000000..89e2bee --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/README.txt | |||
@@ -0,0 +1,2 @@ | |||
1 | This example code looks for the current joystick state once per frame, | ||
2 | and draws a visual representation of it. | ||
diff --git a/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/joystick-polling.c b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/joystick-polling.c new file mode 100644 index 0000000..6eb23b8 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/joystick-polling.c | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | * This example code looks for the current joystick state once per frame, | ||
3 | * and draws a visual representation of it. | ||
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 | /* SDL can handle multiple joysticks, but for simplicity, this program only | ||
20 | deals with the first stick it sees. */ | ||
21 | |||
22 | #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ | ||
23 | #include <SDL3/SDL.h> | ||
24 | #include <SDL3/SDL_main.h> | ||
25 | |||
26 | /* We will use this renderer to draw into this window every frame. */ | ||
27 | static SDL_Window *window = NULL; | ||
28 | static SDL_Renderer *renderer = NULL; | ||
29 | static SDL_Joystick *joystick = NULL; | ||
30 | static SDL_Color colors[64]; | ||
31 | |||
32 | /* This function runs once at startup. */ | ||
33 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
34 | { | ||
35 | int i; | ||
36 | |||
37 | SDL_SetAppMetadata("Example Input Joystick Polling", "1.0", "com.example.input-joystick-polling"); | ||
38 | |||
39 | if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK)) { | ||
40 | SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | ||
41 | return SDL_APP_FAILURE; | ||
42 | } | ||
43 | |||
44 | if (!SDL_CreateWindowAndRenderer("examples/input/joystick-polling", 640, 480, 0, &window, &renderer)) { | ||
45 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
46 | return SDL_APP_FAILURE; | ||
47 | } | ||
48 | |||
49 | for (i = 0; i < SDL_arraysize(colors); i++) { | ||
50 | colors[i].r = SDL_rand(255); | ||
51 | colors[i].g = SDL_rand(255); | ||
52 | colors[i].b = SDL_rand(255); | ||
53 | colors[i].a = 255; | ||
54 | } | ||
55 | |||
56 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
57 | } | ||
58 | |||
59 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
60 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
61 | { | ||
62 | if (event->type == SDL_EVENT_QUIT) { | ||
63 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
64 | } else if (event->type == SDL_EVENT_JOYSTICK_ADDED) { | ||
65 | /* this event is sent for each hotplugged stick, but also each already-connected joystick during SDL_Init(). */ | ||
66 | if (joystick == NULL) { /* we don't have a stick yet and one was added, open it! */ | ||
67 | joystick = SDL_OpenJoystick(event->jdevice.which); | ||
68 | if (!joystick) { | ||
69 | SDL_Log("Failed to open joystick ID %u: %s", (unsigned int) event->jdevice.which, SDL_GetError()); | ||
70 | } | ||
71 | } | ||
72 | } else if (event->type == SDL_EVENT_JOYSTICK_REMOVED) { | ||
73 | if (joystick && (SDL_GetJoystickID(joystick) == event->jdevice.which)) { | ||
74 | SDL_CloseJoystick(joystick); /* our joystick was unplugged. */ | ||
75 | joystick = NULL; | ||
76 | } | ||
77 | } | ||
78 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
79 | } | ||
80 | |||
81 | /* This function runs once per frame, and is the heart of the program. */ | ||
82 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
83 | { | ||
84 | int winw = 640, winh = 480; | ||
85 | const char *text = "Plug in a joystick, please."; | ||
86 | float x, y; | ||
87 | int i; | ||
88 | |||
89 | if (joystick) { /* we have a stick opened? */ | ||
90 | text = SDL_GetJoystickName(joystick); | ||
91 | } | ||
92 | |||
93 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
94 | SDL_RenderClear(renderer); | ||
95 | SDL_GetWindowSize(window, &winw, &winh); | ||
96 | |||
97 | /* note that you can get input as events, instead of polling, which is | ||
98 | better since it won't miss button presses if the system is lagging, | ||
99 | but often times checking the current state per-frame is good enough, | ||
100 | and maybe better if you'd rather _drop_ inputs due to lag. */ | ||
101 | |||
102 | if (joystick) { /* we have a stick opened? */ | ||
103 | const float size = 30.0f; | ||
104 | int total; | ||
105 | |||
106 | /* draw axes as bars going across middle of screen. We don't know if it's an X or Y or whatever axis, so we can't do more than this. */ | ||
107 | total = SDL_GetNumJoystickAxes(joystick); | ||
108 | y = (winh - (total * size)) / 2; | ||
109 | x = ((float) winw) / 2.0f; | ||
110 | for (i = 0; i < total; i++) { | ||
111 | const SDL_Color *color = &colors[i % SDL_arraysize(colors)]; | ||
112 | const float val = (((float) SDL_GetJoystickAxis(joystick, i)) / 32767.0f); /* make it -1.0f to 1.0f */ | ||
113 | const float dx = x + (val * x); | ||
114 | const SDL_FRect dst = { dx, y, x - SDL_fabsf(dx), size }; | ||
115 | SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a); | ||
116 | SDL_RenderFillRect(renderer, &dst); | ||
117 | y += size; | ||
118 | } | ||
119 | |||
120 | /* draw buttons as blocks across top of window. We only know the button numbers, but not where they are on the device. */ | ||
121 | total = SDL_GetNumJoystickButtons(joystick); | ||
122 | x = (winw - (total * size)) / 2; | ||
123 | for (i = 0; i < total; i++) { | ||
124 | const SDL_Color *color = &colors[i % SDL_arraysize(colors)]; | ||
125 | const SDL_FRect dst = { x, 0.0f, size, size }; | ||
126 | if (SDL_GetJoystickButton(joystick, i)) { | ||
127 | SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a); | ||
128 | } else { | ||
129 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
130 | } | ||
131 | SDL_RenderFillRect(renderer, &dst); | ||
132 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, color->a); | ||
133 | SDL_RenderRect(renderer, &dst); /* outline it */ | ||
134 | x += size; | ||
135 | } | ||
136 | |||
137 | /* draw hats across the bottom of the screen. */ | ||
138 | total = SDL_GetNumJoystickHats(joystick); | ||
139 | x = ((winw - (total * (size * 2.0f))) / 2.0f) + (size / 2.0f); | ||
140 | y = ((float) winh) - size; | ||
141 | for (i = 0; i < total; i++) { | ||
142 | const SDL_Color *color = &colors[i % SDL_arraysize(colors)]; | ||
143 | const float thirdsize = size / 3.0f; | ||
144 | const SDL_FRect cross[] = { { x, y + thirdsize, size, thirdsize }, { x + thirdsize, y, thirdsize, size } }; | ||
145 | const Uint8 hat = SDL_GetJoystickHat(joystick, i); | ||
146 | |||
147 | SDL_SetRenderDrawColor(renderer, 90, 90, 90, 255); | ||
148 | SDL_RenderFillRects(renderer, cross, SDL_arraysize(cross)); | ||
149 | |||
150 | SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a); | ||
151 | |||
152 | if (hat & SDL_HAT_UP) { | ||
153 | const SDL_FRect dst = { x + thirdsize, y, thirdsize, thirdsize }; | ||
154 | SDL_RenderFillRect(renderer, &dst); | ||
155 | } | ||
156 | |||
157 | if (hat & SDL_HAT_RIGHT) { | ||
158 | const SDL_FRect dst = { x + (thirdsize * 2), y + thirdsize, thirdsize, thirdsize }; | ||
159 | SDL_RenderFillRect(renderer, &dst); | ||
160 | } | ||
161 | |||
162 | if (hat & SDL_HAT_DOWN) { | ||
163 | const SDL_FRect dst = { x + thirdsize, y + (thirdsize * 2), thirdsize, thirdsize }; | ||
164 | SDL_RenderFillRect(renderer, &dst); | ||
165 | } | ||
166 | |||
167 | if (hat & SDL_HAT_LEFT) { | ||
168 | const SDL_FRect dst = { x, y + thirdsize, thirdsize, thirdsize }; | ||
169 | SDL_RenderFillRect(renderer, &dst); | ||
170 | } | ||
171 | |||
172 | x += size * 2; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | x = (((float) winw) - (SDL_strlen(text) * SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE)) / 2.0f; | ||
177 | y = (((float) winh) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2.0f; | ||
178 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
179 | SDL_RenderDebugText(renderer, x, y, text); | ||
180 | SDL_RenderPresent(renderer); | ||
181 | |||
182 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
183 | } | ||
184 | |||
185 | /* This function runs once at shutdown. */ | ||
186 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
187 | { | ||
188 | if (joystick) { | ||
189 | SDL_CloseJoystick(joystick); | ||
190 | } | ||
191 | |||
192 | /* SDL will clean up the window/renderer for us. */ | ||
193 | } | ||
diff --git a/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/onmouseover.webp b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/onmouseover.webp new file mode 100644 index 0000000..484539c --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/onmouseover.webp | |||
Binary files differ | |||
diff --git a/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/thumbnail.png b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/thumbnail.png new file mode 100644 index 0000000..4faebba --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/thumbnail.png | |||
Binary files differ | |||
diff --git a/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/README.txt b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/README.txt new file mode 100644 index 0000000..d87ac0a --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/README.txt | |||
@@ -0,0 +1,2 @@ | |||
1 | This example code looks for joystick input in the event handler, and | ||
2 | reports any changes as a flood of info. | ||
diff --git a/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/joystick-events.c b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/joystick-events.c new file mode 100644 index 0000000..cc01d84 --- /dev/null +++ b/src/contrib/SDL-3.2.20/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 | } | ||
diff --git a/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/onmouseover.webp b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/onmouseover.webp new file mode 100644 index 0000000..05a9b42 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/onmouseover.webp | |||
Binary files differ | |||
diff --git a/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/thumbnail.png b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/thumbnail.png new file mode 100644 index 0000000..07f3ff1 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/input/02-joystick-events/thumbnail.png | |||
Binary files differ | |||