Debian Codegraph @

src/io/controls.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file controls.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created main.c
10 * - 22nd July 2008: Created util.c from parts of main.c
11 * - 3rd February 2009: Renamed main.c to main.cpp
12 * - 13th July 2009: Created controls.cpp from parts of main.cpp
13 *
14 * @par Licence:
15 * Copyright (c) 2005-2017 AJ Thomson
16 * Copyright (c) 2015-2026 Carsten Teibes
17 *
18 * OpenJazz is distributed under the terms of
19 * the GNU General Public License, version 2.0
20 *
21 * @par Description:
22 * Deals with input.
23 *
24 */
25
26 #ifdef OJ_SDL3
27 #include <SDL3/SDL.h>
28 #else
29 #include <SDL.h>
30 #endif
31
32 #include "controls.h"
33 #include "gfx/video.h"
34 #include "platforms/platforms.h"
35 #include "loop.h"
36
37 #if !OJ_SDL3
38 // Define some stuff to be SDL3 compatible
39
40 #define SDLK_P SDLK_p
41 #define SDLK_Y SDLK_y
42 #define SDLK_N SDLK_n
43
44 #define SDL_EVENT_JOYSTICK_BUTTON_DOWN SDL_JOYBUTTONDOWN
45 #define SDL_EVENT_JOYSTICK_BUTTON_UP SDL_JOYBUTTONUP
46 #define SDL_EVENT_JOYSTICK_BUTTON_UP SDL_JOYBUTTONUP
47 #define SDL_EVENT_JOYSTICK_AXIS_MOTION SDL_JOYAXISMOTION
48 #define SDL_EVENT_JOYSTICK_HAT_MOTION SDL_JOYHATMOTION
49 #define SDL_EVENT_MOUSE_BUTTON_DOWN SDL_MOUSEBUTTONDOWN
50 #define SDL_EVENT_MOUSE_BUTTON_UP SDL_MOUSEBUTTONUP
51 #define SDL_EVENT_MOUSE_MOTION SDL_MOUSEMOTION
52 #define SDL_BUTTON_MASK SDL_BUTTON
53 #endif
54
55 /* Default Keyboard config */
56 #ifndef DEFAULT_KEY_UP
57 #define DEFAULT_KEY_UP (SDLK_UP)
58 #endif
59 #ifndef DEFAULT_KEY_DOWN
60 #define DEFAULT_KEY_DOWN (SDLK_DOWN)
61 #endif
62 #ifndef DEFAULT_KEY_LEFT
63 #define DEFAULT_KEY_LEFT (SDLK_LEFT)
64 #endif
65 #ifndef DEFAULT_KEY_RIGHT
66 #define DEFAULT_KEY_RIGHT (SDLK_RIGHT)
67 #endif
68 #ifndef DEFAULT_KEY_JUMP
69 #define DEFAULT_KEY_JUMP (SDLK_SPACE)
70 #endif
71 #ifndef DEFAULT_KEY_SWIM
72 #define DEFAULT_KEY_SWIM (SDLK_SPACE)
73 #endif
74 #ifndef DEFAULT_KEY_FIRE
75 #define DEFAULT_KEY_FIRE (SDLK_LALT)
76 #endif
77 #ifndef DEFAULT_KEY_CHANGE
78 #define DEFAULT_KEY_CHANGE (SDLK_RCTRL)
79 #endif
80 #ifndef DEFAULT_KEY_ENTER
81 #define DEFAULT_KEY_ENTER (SDLK_RETURN)
82 #endif
83 #ifndef DEFAULT_KEY_ESCAPE
84 #define DEFAULT_KEY_ESCAPE (SDLK_ESCAPE)
85 #endif
86 #ifndef DEFAULT_KEY_STATS
87 #define DEFAULT_KEY_STATS (SDLK_F9)
88 #endif
89 #ifndef DEFAULT_KEY_PAUSE
90 #define DEFAULT_KEY_PAUSE (SDLK_P)
91 #endif
92 #ifndef DEFAULT_KEY_YES
93 #define DEFAULT_KEY_YES (SDLK_Y)
94 #endif
95 #ifndef DEFAULT_KEY_NO
96 #define DEFAULT_KEY_NO (SDLK_N)
97 #endif
98
99 /* These are optional */
100 #ifndef DEFAULT_KEY_BLASTER
101 #define DEFAULT_KEY_BLASTER (SDLK_1)
102 #endif
103 #ifndef DEFAULT_KEY_TOASTER
104 #define DEFAULT_KEY_TOASTER (SDLK_2)
105 #endif
106 #ifndef DEFAULT_KEY_MISSILE
107 #define DEFAULT_KEY_MISSILE (SDLK_3)
108 #endif
109 #ifndef DEFAULT_KEY_BOUNCER
110 #define DEFAULT_KEY_BOUNCER (SDLK_4)
111 #endif
112 #ifndef DEFAULT_KEY_TNT
113 #define DEFAULT_KEY_TNT (SDLK_5)
114 #endif
115
116 /* Arbitrary default button config */
117 #ifndef DEFAULT_BUTTON_UP
118 #define DEFAULT_BUTTON_UP (-1)
119 #endif
120 #ifndef DEFAULT_BUTTON_DOWN
121 #define DEFAULT_BUTTON_DOWN (-1)
122 #endif
123 #ifndef DEFAULT_BUTTON_LEFT
124 #define DEFAULT_BUTTON_LEFT (-1)
125 #endif
126 #ifndef DEFAULT_BUTTON_RIGHT
127 #define DEFAULT_BUTTON_RIGHT (-1)
128 #endif
129 #ifndef DEFAULT_BUTTON_JUMP
130 #define DEFAULT_BUTTON_JUMP (1)
131 #endif
132 #ifndef DEFAULT_BUTTON_SWIM
133 #define DEFAULT_BUTTON_SWIM (1)
134 #endif
135 #ifndef DEFAULT_BUTTON_FIRE
136 #define DEFAULT_BUTTON_FIRE (0)
137 #endif
138 #ifndef DEFAULT_BUTTON_CHANGE
139 #define DEFAULT_BUTTON_CHANGE (3)
140 #endif
141 #ifndef DEFAULT_BUTTON_ENTER
142 #define DEFAULT_BUTTON_ENTER (0)
143 #endif
144 #ifndef DEFAULT_BUTTON_ESCAPE
145 #define DEFAULT_BUTTON_ESCAPE (5)
146 #endif
147 #ifndef DEFAULT_BUTTON_STATS
148 #define DEFAULT_BUTTON_STATS (-1)
149 #endif
150 #ifndef DEFAULT_BUTTON_PAUSE
151 #define DEFAULT_BUTTON_PAUSE (4)
152 #endif
153 #ifndef DEFAULT_BUTTON_YES
154 #define DEFAULT_BUTTON_YES (-1)
155 #endif
156 #ifndef DEFAULT_BUTTON_NO
157 #define DEFAULT_BUTTON_NO (-1)
158 #endif
159
160 /**
161 * Set up the default controls.
162 */
163 Controls::Controls () {
164
165 int count;
166
167 keys[C_UP].key = DEFAULT_KEY_UP;
168 keys[C_DOWN].key = DEFAULT_KEY_DOWN;
169 keys[C_LEFT].key = DEFAULT_KEY_LEFT;
170 keys[C_RIGHT].key = DEFAULT_KEY_RIGHT;
171 keys[C_JUMP].key = DEFAULT_KEY_JUMP;
172 keys[C_SWIM].key = DEFAULT_KEY_SWIM;
173 keys[C_FIRE].key = DEFAULT_KEY_FIRE;
174 keys[C_CHANGE].key = DEFAULT_KEY_CHANGE;
175 keys[C_ENTER].key = DEFAULT_KEY_ENTER;
176 keys[C_ESCAPE].key = DEFAULT_KEY_ESCAPE;
177 keys[C_BLASTER].key = DEFAULT_KEY_BLASTER;
178 keys[C_TOASTER].key = DEFAULT_KEY_TOASTER;
179 keys[C_MISSILE].key = DEFAULT_KEY_MISSILE;
180 keys[C_BOUNCER].key = DEFAULT_KEY_BOUNCER;
181 keys[C_TNT].key = DEFAULT_KEY_TNT;
182 keys[C_STATS].key = DEFAULT_KEY_STATS;
183 keys[C_PAUSE].key = DEFAULT_KEY_PAUSE;
184 keys[C_YES].key = DEFAULT_KEY_YES;
185 keys[C_NO].key = DEFAULT_KEY_NO;
186
187
188 buttons[C_UP].button = DEFAULT_BUTTON_UP;
189 buttons[C_DOWN].button = DEFAULT_BUTTON_DOWN;
190 buttons[C_LEFT].button = DEFAULT_BUTTON_LEFT;
191 buttons[C_RIGHT].button = DEFAULT_BUTTON_RIGHT;
192 buttons[C_JUMP].button = DEFAULT_BUTTON_JUMP;
193 buttons[C_SWIM].button = DEFAULT_BUTTON_SWIM;
194 buttons[C_FIRE].button = DEFAULT_BUTTON_FIRE;
195 buttons[C_CHANGE].button = DEFAULT_BUTTON_CHANGE;
196 buttons[C_ENTER].button = DEFAULT_BUTTON_ENTER;
197 buttons[C_ESCAPE].button = DEFAULT_BUTTON_ESCAPE;
198 buttons[C_BLASTER].button = -1;
199 buttons[C_TOASTER].button = -1;
200 buttons[C_MISSILE].button = -1;
201 buttons[C_BOUNCER].button = -1;
202 buttons[C_TNT].button = -1;
203 buttons[C_STATS].button = DEFAULT_BUTTON_STATS;
204 buttons[C_PAUSE].button = DEFAULT_BUTTON_PAUSE;
205 buttons[C_YES].button = DEFAULT_BUTTON_YES;
206 buttons[C_NO].button = DEFAULT_BUTTON_NO;
207
208
209 axes[C_UP].axis = 1;
210 axes[C_UP].direction = false;
211 axes[C_DOWN].axis = 1;
212 axes[C_DOWN].direction = true;
213 axes[C_LEFT].axis = 0;
214 axes[C_LEFT].direction = false;
215 axes[C_RIGHT].axis = 0;
216 axes[C_RIGHT].direction = true;
217 axes[C_JUMP].axis = -1;
218 axes[C_SWIM].axis = -1;
219 axes[C_FIRE].axis = -1;
220 axes[C_CHANGE].axis = -1;
221 axes[C_ENTER].axis = -1;
222 axes[C_ESCAPE].axis = -1;
223 axes[C_BLASTER].axis = -1;
224 axes[C_TOASTER].axis = -1;
225 axes[C_MISSILE].axis = -1;
226 axes[C_BOUNCER].axis = -1;
227 axes[C_TNT].axis = -1;
228 axes[C_STATS].axis = -1;
229 axes[C_PAUSE].axis = -1;
230 axes[C_YES].axis = -1;
231 axes[C_NO].axis = -1;
232
233
234 hats[C_UP].hat = 0;
235 hats[C_UP].direction = SDL_HAT_UP;
236 hats[C_DOWN].hat = 0;
237 hats[C_DOWN].direction = SDL_HAT_DOWN;
238 hats[C_LEFT].hat = 0;
239 hats[C_LEFT].direction = SDL_HAT_LEFT;
240 hats[C_RIGHT].hat = 0;
241 hats[C_RIGHT].direction = SDL_HAT_RIGHT;
242 hats[C_JUMP].hat = -1;
243 hats[C_SWIM].hat = -1;
244 hats[C_FIRE].hat = -1;
245 hats[C_CHANGE].hat = -1;
246 hats[C_ENTER].hat = -1;
247 hats[C_ESCAPE].hat = -1;
248 hats[C_BLASTER].hat = -1;
249 hats[C_TOASTER].hat = -1;
250 hats[C_MISSILE].hat = -1;
251 hats[C_BOUNCER].hat = -1;
252 hats[C_TNT].hat = -1;
253 hats[C_STATS].hat = -1;
254 hats[C_PAUSE].hat = -1;
255 hats[C_YES].hat = -1;
256 hats[C_NO].hat = -1;
257
258
259 for (count = 0; count < CONTROLS; count++) {
260
261 keys[count].pressed = false;
262 buttons[count].pressed = false;
263 axes[count].pressed = false;
264 hats[count].pressed = false;
265
266 controlstates[count].time = 0;
267 controlstates[count].state = false;
268
269 }
270
271 cursorX = cursorY = 0;
272 cursorPressed = cursorReleased = false;
273 wheelUp = wheelDown = 0;
274
275 }
276
277 void Controls::init() {
278 #if OJ_SDL3
279 if(SDL_HasJoystick()) SDL_OpenJoystick(0);
280 #else
281 if (SDL_NumJoysticks() > 0) SDL_JoystickOpen(0);
282 #endif
283 }
284
285 void Controls::deinit() {
286 #if OJ_SDL3
287 if(SDL_HasJoystick()) SDL_CloseJoystick(0);
288 #else
289 if (SDL_NumJoysticks() > 0) SDL_JoystickClose(0);
290 #endif
291 }
292
293 /**
294 * Set the key to use for the specified control.
295 *
296 * @param control The control
297 * @param key The key to use
298 */
299 void Controls::setKey (int control, int key) {
300
301 keys[control].key = key;
302 keys[control].pressed = false;
303
304 }
305
306
307 /**
308 * Set the button to use for the specified control.
309 *
310 * @param control The control
311 * @param button The button to use
312 */
313 void Controls::setButton (int control, int button) {
314
315 buttons[control].button = button;
316 buttons[control].pressed = false;
317
318 }
319
320
321 /**
322 * Set the axis and direction to use for the specified control.
323 *
324 * @param control The control
325 * @param axis The axis to use
326 * @param direction Whether or not to use positive axis values
327 */
328 void Controls::setAxis (int control, int axis, bool direction) {
329
330 axes[control].axis = axis;
331 axes[control].direction = direction;
332 axes[control].pressed = false;
333
334 }
335
336
337 /**
338 * Set the hat and direction to use for the specified control.
339 *
340 * @param control The control
341 * @param hat The hat to use
342 * @param direction The direction to use
343 */
344 void Controls::setHat (int control, int hat, int direction) {
345
346 hats[control].hat = hat;
347 hats[control].direction = direction;
348 hats[control].pressed = false;
349
350 }
351
352
353 /**
354 * Get the key being used for the specified control.
355 *
356 * @param control The control
357 *
358 * @return The key being used
359 */
360 int Controls::getKey (int control) {
361
362 return keys[control].key;
363
364 }
365
366
367 /**
368 * Get the button being used for the specified control.
369 *
370 * @param control The control
371 *
372 * @return The button being used
373 */
374 int Controls::getButton (int control) {
375
376 return buttons[control].button;
377
378 }
379
380
381 /**
382 * Get the axis being used for the specified control.
383 *
384 * @param control The control
385 *
386 * @return The axis being used
387 */
388 int Controls::getAxis (int control) {
389
390 return axes[control].axis;
391
392 }
393
394
395 /**
396 * Get the direction of the axis being used for the specified control.
397 *
398 * @param control The control
399 *
400 * @return True if positive values of the axis are being used
401 */
402 int Controls::getAxisDirection (int control) {
403
404 return axes[control].direction;
405
406 }
407
408
409 /**
410 * Get the hat being used for the specified control.
411 *
412 * @param control The control
413 *
414 * @return The hat being used
415 */
416 int Controls::getHat (int control) {
417
418 return hats[control].hat;
419
420 }
421
422
423 /**
424 * Get the direction of the hat being used for the specified control.
425 *
426 * @param control The control
427 *
428 * @return hat direction
429 */
430 int Controls::getHatDirection (int control) {
431
432 return hats[control].direction;
433
434 }
435
436
437 /**
438 * Set the position and state of the cursor.
439 *
440 * @param x The x-coordinate of the cursor
441 * @param y The y-coordinate of the cursor
442 * @param pressed The state of the cursor
443 */
444 void Controls::setCursor(int x, int y, bool pressed) {
445
446 cursorX = x;
447 cursorY = y;
448 cursorPressed = pressed;
449 cursorReleased = !pressed;
450
451 }
452
453
454 /**
455 * Update controls based on a system event.
456 *
457 * @param event The system event. Non-input events will be ignored
458 * @param type Type of loop. Normal, typing, or input configuration
459 *
460 * @return Error code
461 */
462 int Controls::update (SDL_Event *event, LoopType type) {
463
464 int count;
465
466 count = CONTROLS;
467
468 switch (event->type) {
469 #if OJ_SDL3
470 case SDL_EVENT_KEY_DOWN:
471 if (type == SET_KEY_LOOP) return event->key.key;
472
473 for (count = 0; count < CONTROLS; count++)
474 if (event->key.key == (unsigned int)keys[count].key)
475 keys[count].pressed = true;
476
477 if (type == TYPING_LOOP) return event->key.key;
478 #else
479 case SDL_KEYDOWN:
480 if (type == SET_KEY_LOOP) return event->key.keysym.sym;
481
482 for (count = 0; count < CONTROLS; count++)
483 if (event->key.keysym.sym == keys[count].key)
484 keys[count].pressed = true;
485
486 if (type == TYPING_LOOP) return event->key.keysym.sym;
487 #endif
488
489 break;
490
491 #if OJ_SDL3
492 case SDL_EVENT_KEY_UP:
493 for (count = 0; count < CONTROLS; count++)
494 if (event->key.key == (unsigned int)keys[count].key)
495 keys[count].pressed = false;
496 #else
497 case SDL_KEYUP:
498 for (count = 0; count < CONTROLS; count++)
499 if (event->key.keysym.sym == keys[count].key)
500 keys[count].pressed = false;
501 #endif
502
503 break;
504
505 case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
506 if (type == SET_JOYSTICK_LOOP) return JOYSTICKB | event->jbutton.button;
507
508 for (count = 0; count < CONTROLS; count++)
509 if (event->jbutton.button == buttons[count].button)
510 buttons[count].pressed = true;
511
512 break;
513
514 case SDL_EVENT_JOYSTICK_BUTTON_UP:
515 for (count = 0; count < CONTROLS; count++)
516 if (event->jbutton.button == buttons[count].button)
517 buttons[count].pressed = false;
518
519 break;
520
521 case SDL_EVENT_JOYSTICK_AXIS_MOTION:
522 if (type == SET_JOYSTICK_LOOP) {
523
524 if (event->jaxis.value < -16384)
525 return JOYSTICKANEG | event->jaxis.axis;
526 else if (event->jaxis.value > 16384)
527 return JOYSTICKAPOS | event->jaxis.axis;
528
529 }
530
531 for (count = 0; count < CONTROLS; count++)
532 if (event->jaxis.axis == axes[count].axis) {
533
534 if (!axes[count].direction && (event->jaxis.value < -16384))
535 axes[count].pressed = true;
536 else if (axes[count].direction && (event->jaxis.value > 16384))
537 axes[count].pressed = true;
538 else
539 axes[count].pressed = false;
540
541 }
542
543 break;
544
545 case SDL_EVENT_JOYSTICK_HAT_MOTION:
546 if (type == SET_JOYSTICK_LOOP) {
547
548 switch(event->jhat.value) {
549 case SDL_HAT_UP:
550 return JOYSTICKHUP | event->jhat.hat;
551 case SDL_HAT_LEFT:
552 return JOYSTICKHLFT | event->jhat.hat;
553 case SDL_HAT_RIGHT:
554 return JOYSTICKHRHT | event->jhat.hat;
555 case SDL_HAT_DOWN:
556 return JOYSTICKHDWN | event->jhat.hat;
557 }
558 }
559
560 for(count = 0; count < CONTROLS; count++)
561 if (event->jhat.hat == hats[count].hat) {
562
563 if (hats[count].direction & event->jhat.value)
564 hats[count].pressed = true;
565 else
566 hats[count].pressed = false;
567
568 }
569
570 break;
571
572 case SDL_EVENT_MOUSE_MOTION:
573 if(event->motion.state & SDL_BUTTON_MASK(1))
574 setCursor(event->motion.x, event->motion.y, true);
575
576 break;
577
578 case SDL_EVENT_MOUSE_BUTTON_DOWN:
579 if (event->button.button == SDL_BUTTON_LEFT) {
580
581 setCursor(event->button.x, event->button.y, true);
582
583 } else if (event->button.button == 4) {
584
585 wheelUp++;
586
587 } else if (event->button.button == 5) {
588
589 wheelDown++;
590
591 }
592
593 break;
594
595 case SDL_EVENT_MOUSE_BUTTON_UP:
596 if (event->button.button == SDL_BUTTON_LEFT) {
597
598 setCursor(event->button.x, event->button.y, false);
599
600 }
601
602 break;
603
604 }
605
606 return E_NONE;
607
608 }
609
610
611 /**
612 * Process input iteration.
613 *
614 * Called once per game iteration. Updates input.
615 */
616 void Controls::loop () {
617
618 int count;
619
620 // Apply controls to universal control tracking
621 for (count = 0; count < CONTROLS; count++)
622 controlstates[count].state = (controlstates[count].time < globalTicks) &&
623 (keys[count].pressed || buttons[count].pressed ||
624 axes[count].pressed || hats[count].pressed);
625
626 if (wheelUp) {
627
628 controlstates[C_UP].state = true;
629 wheelUp--;
630
631 }
632
633 if (wheelDown) {
634
635 controlstates[C_DOWN].state = true;
636 wheelDown--;
637
638 }
639
640 }
641
642
643 /**
644 * Determine whether or not the specified control is being used.
645 *
646 * @param control The control
647 *
648 * @return True if the control is being used
649 */
650 bool Controls::getState (int control) {
651
652 return controlstates[control].state;
653
654 }
655
656
657 /**
658 * If it's being used, release the specified control.
659 *
660 * @param control The control
661 *
662 * @return True if the control was being used
663 */
664 bool Controls::release (int control) {
665
666 if (!controlstates[control].state) return false;
667
668 controlstates[control].time = globalTicks + T_KEY;
669 controlstates[control].state = false;
670
671 return true;
672
673 }
674
675
676 /**
677 * Get the position of the cursor, and determine whether or not it's being used.
678 *
679 * @param x Is set to the x-coordinate of the cursor
680 * @param y Is set to the y-coordinate of the cursor
681 *
682 * @return True if the cursor was being used
683 */
684 bool Controls::getCursor (int& x, int& y) {
685 int scaleFactor = video.getScaleFactor();
686
687 if (scaleFactor > MIN_SCALE) {
688 x = cursorX / scaleFactor;
689 y = cursorY / scaleFactor;
690 } else {
691 x = cursorX;
692 y = cursorY;
693 }
694
695 return cursorPressed || cursorReleased;
696
697 }
698
699
700 /**
701 * Determine whether or not the cursor has been released.
702 *
703 * @return True if the cursor has been released
704 */
705 bool Controls::wasCursorReleased () {
706
707 if (cursorReleased) {
708
709 cursorReleased = false;
710
711 return true;
712
713 }
714
715 return false;
716
717 }