diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/joystick-polling.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/input/01-joystick-polling/joystick-polling.c | 193 |
1 files changed, 193 insertions, 0 deletions
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 | } | ||