forked from simple2d/simple2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.c
342 lines (316 loc) · 8.42 KB
/
controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <simple2d.h>
S2D_Window *window;
S2D_Image *controller;
double scale = 80;
double axis_LEFTX = 0;
double axis_LEFTY = 0;
double axis_RIGHTX = 0;
double axis_RIGHTY = 0;
double axis_TRIGGERLEFT = 0;
double axis_TRIGGERRIGHT = 0;
double axis_MAX = 0;
bool btn_A = false;
bool btn_B = false;
bool btn_X = false;
bool btn_Y = false;
bool btn_BACK = false;
bool btn_GUIDE = false;
bool btn_START = false;
bool btn_LEFTSTICK = false;
bool btn_RIGHTSTICK = false;
bool btn_LEFTSHOULDER = false;
bool btn_RIGHTSHOULDER = false;
bool btn_DPAD_UP = false;
bool btn_DPAD_DOWN = false;
bool btn_DPAD_LEFT = false;
bool btn_DPAD_RIGHT = false;
bool btn_MAX = false;
// Normalize axis values to 0.0...1.0
double to_d(int val) {
return val > 0 ? val / 32767.0 : val / 32768.0;
}
void render() {
S2D_DrawImage(controller);
// Axes
S2D_DrawQuad(
156, 130, 0, 1, 0, 1,
156 + axis_LEFTX, 130, 0, 1, 0, 1,
156 + axis_LEFTX, 159, 0, 1, 0, 1,
156, 159, 0, 1, 0, 1
);
S2D_DrawQuad(
142, 145 + axis_LEFTY, 0, 1, 0, 1,
171, 145 + axis_LEFTY, 0, 1, 0, 1,
171, 145, 0, 1, 0, 1,
142, 145, 0, 1, 0, 1
);
S2D_DrawQuad(
374, 215, 0, 1, 0, 1,
374 + axis_RIGHTX, 215, 0, 1, 0, 1,
374 + axis_RIGHTX, 244, 0, 1, 0, 1,
374, 244, 0, 1, 0, 1
);
S2D_DrawQuad(
359, 229 + axis_RIGHTY, 0, 1, 0, 1,
388, 229 + axis_RIGHTY, 0, 1, 0, 1,
388, 229, 0, 1, 0, 1,
359, 229, 0, 1, 0, 1
);
S2D_DrawQuad(
8, 71 - axis_TRIGGERLEFT * .84, 0, 1, 0, 1,
42, 71 - axis_TRIGGERLEFT * .84, 0, 1, 0, 1,
42, 71, 0, 1, 0, 1,
8, 71, 0, 1, 0, 1
);
S2D_DrawQuad(
8 + 550, 71 - axis_TRIGGERRIGHT * .84, 0, 1, 0, 1,
42 + 550, 71 - axis_TRIGGERRIGHT * .84, 0, 1, 0, 1,
42 + 550, 71, 0, 1, 0, 1,
8 + 550, 71, 0, 1, 0, 1
);
// Buttons
if (btn_A) // green
S2D_DrawQuad(
426, 167, 0, 1, 0, 1,
426 + 33, 167, 0, 1, 0, 1,
426 + 33, 167 + 33, 0, 1, 0, 1,
426, 167 + 33, 0, 1, 0, 1
);
if (btn_B) // red
S2D_DrawQuad(
464, 129, 1, 0, 0, 1,
464 + 33, 129, 1, 0, 0, 1,
464 + 33, 129 + 33, 1, 0, 0, 1,
464, 129 + 33, 1, 0, 0, 1
);
if (btn_X) // blue
S2D_DrawQuad(
388, 128, 0, .7, 1, 1,
388 + 33, 128, 0, .7, 1, 1,
388 + 33, 128 + 33, 0, .7, 1, 1,
388, 128 + 33, 0, .7, 1, 1
);
if (btn_Y) // yellow
S2D_DrawQuad(
426, 91, 1, 1, 0, 1,
426 + 33, 91, 1, 1, 0, 1,
426 + 33, 91 + 33, 1, 1, 0, 1,
426, 91 + 33, 1, 1, 0, 1
);
if (btn_BACK)
S2D_DrawQuad(
248, 133, 1, .5, 0, 1,
248 + 23, 133, 1, .5, 0, 1,
248 + 23, 133 + 23, 1, .5, 0, 1,
248, 133 + 23, 1, .5, 0, 1
);
if (btn_GUIDE)
S2D_DrawQuad(
281, 69, .5, 1, .5, 1,
281 + 38, 69, .5, 1, .5, 1,
281 + 38, 69 + 38, .5, 1, .5, 1,
281, 69 + 38, .5, 1, .5, 1
);
if (btn_START)
S2D_DrawQuad(
331, 133, 1, .5, 0, 1,
331 + 23, 133, 1, .5, 0, 1,
331 + 23, 133 + 23, 1, .5, 0, 1,
331, 133 + 23, 1, .5, 0, 1
);
if (btn_LEFTSTICK)
S2D_DrawQuad(
134, 122, 1, 0, 0, 1,
134 + 45, 122, 1, 0, 0, 1,
134 + 45, 122 + 45, 1, 0, 0, 1,
134, 122 + 45, 1, 0, 0, 1
);
if (btn_RIGHTSTICK)
S2D_DrawQuad(
351, 207, 1, 0, 0, 1,
351 + 45, 207, 1, 0, 0, 1,
351 + 45, 207 + 45, 1, 0, 0, 1,
351, 207 + 45, 1, 0, 0, 1
);
if (btn_LEFTSHOULDER)
S2D_DrawQuad(
111, 84, .5, 0, 1, 1,
117, 64, .5, 0, 1, 1,
198, 39, .5, 0, 1, 1,
225, 52, .5, 0, 1, 1
);
if (btn_RIGHTSHOULDER)
S2D_DrawQuad(
494, 85, .5, 0, 1, 1,
484, 64, .5, 0, 1, 1,
401, 39, .5, 0, 1, 1,
378, 51, .5, 0, 1, 1
);
if (btn_DPAD_UP)
S2D_DrawQuad(
216, 194, 1, 0, .5, 1,
216 + 23, 194, 1, 0, .5, 1,
216 + 23, 194 + 28, 1, 0, .5, 1,
216, 194 + 28, 1, 0, .5, 1
);
if (btn_DPAD_DOWN)
S2D_DrawQuad(
216, 243, 1, 0, .5, 1,
216 + 23, 243, 1, 0, .5, 1,
216 + 23, 243 + 27, 1, 0, .5, 1,
216, 243 + 27, 1, 0, .5, 1
);
if (btn_DPAD_LEFT)
S2D_DrawQuad(
189, 221, 1, 0, .5, 1,
189 + 28, 221, 1, 0, .5, 1,
189 + 28, 221 + 22, 1, 0, .5, 1,
189, 221 + 22, 1, 0, .5, 1
);
if (btn_DPAD_RIGHT)
S2D_DrawQuad(
238, 221, 1, 0, .5, 1,
238 + 28, 221, 1, 0, .5, 1,
238 + 28, 221 + 22, 1, 0, .5, 1,
238, 221 + 22, 1, 0, .5, 1
);
}
void on_key(S2D_Event e) {
if (e.type != S2D_KEY_DOWN) return;
if (strcmp(e.key, "Escape") == 0) {
S2D_Close(window);
}
}
void on_controller(S2D_Event e) {
puts("=== Controller Event ===");
printf("Controller #%i\n", e.which);
// Axes
if (e.type == S2D_AXIS) {
printf("Axis movement: #%i ", e.axis);
switch (e.axis) {
case S2D_AXIS_INVALID:
S2D_Error("Controller", "Invalid axis!");
break;
case S2D_AXIS_LEFTX:
puts("(LEFTX)");
axis_LEFTX = to_d(e.value) * scale;
break;
case S2D_AXIS_LEFTY:
puts("(LEFTY)");
axis_LEFTY = to_d(e.value) * scale;
break;
case S2D_AXIS_RIGHTX:
puts("(RIGHTX)");
axis_RIGHTX = to_d(e.value) * scale;
break;
case S2D_AXIS_RIGHTY:
puts("(RIGHTY)");
axis_RIGHTY = to_d(e.value) * scale;
break;
case S2D_AXIS_TRIGGERLEFT:
puts("(TRIGGERLEFT)");
axis_TRIGGERLEFT = to_d(e.value) * scale;
break;
case S2D_AXIS_TRIGGERRIGHT:
puts("(TRIGGERRIGHT)");
axis_TRIGGERRIGHT = to_d(e.value) * scale;
break;
case S2D_AXIS_MAX:
puts("(MAX)");
break;
}
printf("Value: %i\n", e.value);
// Buttons
} else {
switch (e.type) {
case S2D_BUTTON_DOWN:
printf("Button down: #%i ", e.button);
break;
case S2D_BUTTON_UP:
printf("Button up: #%i ", e.button);
break;
}
bool pressed = e.type == S2D_BUTTON_DOWN ? true : false;
switch (e.button) {
case S2D_BUTTON_INVALID:
S2D_Error("Controller", "Invalid button!");
case S2D_BUTTON_A:
puts("(A)");
btn_A = pressed;
break;
case S2D_BUTTON_B:
puts("(B)");
btn_B = pressed;
break;
case S2D_BUTTON_X:
puts("(X)");
btn_X = pressed;
break;
case S2D_BUTTON_Y:
puts("(Y)");
btn_Y = pressed;
break;
case S2D_BUTTON_BACK:
puts("(BACK)");
btn_BACK = pressed;
break;
case S2D_BUTTON_GUIDE:
puts("(GUIDE)");
btn_GUIDE = pressed;
break;
case S2D_BUTTON_START:
puts("(START)");
btn_START = pressed;
break;
case S2D_BUTTON_LEFTSTICK:
puts("(LEFTSTICK)");
btn_LEFTSTICK = pressed;
break;
case S2D_BUTTON_RIGHTSTICK:
puts("(RIGHTSTICK)");
btn_RIGHTSTICK = pressed;
break;
case S2D_BUTTON_LEFTSHOULDER:
puts("(LEFTSHOULDER)");
btn_LEFTSHOULDER = pressed;
break;
case S2D_BUTTON_RIGHTSHOULDER:
puts("(RIGHTSHOULDER)");
btn_RIGHTSHOULDER = pressed;
break;
case S2D_BUTTON_DPAD_UP:
puts("(DPAD_UP)");
btn_DPAD_UP = pressed;
break;
case S2D_BUTTON_DPAD_DOWN:
puts("(DPAD_DOWN)");
btn_DPAD_DOWN = pressed;
break;
case S2D_BUTTON_DPAD_LEFT:
puts("(DPAD_LEFT)");
btn_DPAD_LEFT = pressed;
break;
case S2D_BUTTON_DPAD_RIGHT:
puts("(DPAD_RIGHT)");
btn_DPAD_RIGHT = pressed;
break;
case S2D_BUTTON_MAX:
puts("(MAX)");
btn_MAX = pressed;
break;
}
}
}
int main() {
S2D_Diagnostics(true);
S2D_AddControllerMappingsFromFile("media/controllerdb.txt");
window = S2D_CreateWindow(
"Simple 2D — Controller", 600, 425, NULL, render, 0
);
window->on_key = on_key;
window->on_controller = on_controller;
controller = S2D_CreateImage("media/controller.png");
S2D_Show(window);
S2D_FreeWindow(window);
return 0;
}