-
Notifications
You must be signed in to change notification settings - Fork 6
/
joystick.c
140 lines (125 loc) · 4.19 KB
/
joystick.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
// --------------------------------------------------------------------------
//
// C Kong
// Copyright (C) 2018 Jeff Panici
// All rights reserved.
//
// This software source file is licensed according to the
// MIT License. Refer to the LICENSE file distributed along
// with this source file to learn more.
//
// --------------------------------------------------------------------------
#include <assert.h>
#include <SDL_system.h>
#include <SDL_gamecontroller.h>
#include "joystick.h"
static uint16_t s_button_state = 0;
static const Uint8* s_keyboard_state;
bool joystick_button(
joystick_t* joy,
joystick_button_t button) {
bool button_pressed = false;
if (joy->controller != NULL) {
button_pressed = SDL_GameControllerGetButton(
joy->controller,
(SDL_GameControllerButton) button) != 0;
}
if (!button_pressed) {
switch (button) {
case button_a:
button_pressed = s_keyboard_state[SDL_SCANCODE_LCTRL];
break;
case button_b:
button_pressed = s_keyboard_state[SDL_SCANCODE_RCTRL];
break;
case button_x:
button_pressed = s_keyboard_state[SDL_SCANCODE_X];
break;
case button_y:
button_pressed = s_keyboard_state[SDL_SCANCODE_Y];
break;
case button_back:
button_pressed = s_keyboard_state[SDL_SCANCODE_BACKSPACE];
break;
case button_guide:
button_pressed = s_keyboard_state[SDL_SCANCODE_F1];
break;
case button_start:
button_pressed = s_keyboard_state[SDL_SCANCODE_RETURN];
break;
case button_left_stick:
break;
case button_right_stick:
break;
case button_left_shoulder:
button_pressed = s_keyboard_state[SDL_SCANCODE_LSHIFT];
break;
case button_right_shoulder:
button_pressed = s_keyboard_state[SDL_SCANCODE_RSHIFT];
break;
case button_dpad_up:
button_pressed = s_keyboard_state[SDL_SCANCODE_UP];
break;
case button_dpad_down:
button_pressed = s_keyboard_state[SDL_SCANCODE_DOWN];
break;
case button_dpad_left:
button_pressed = s_keyboard_state[SDL_SCANCODE_LEFT];
break;
case button_dpad_right:
button_pressed = s_keyboard_state[SDL_SCANCODE_RIGHT];
break;
default:
break;
}
}
return button_pressed;
}
bool joystick_button_pressed(
joystick_t* joy,
joystick_button_t button) {
const uint32_t bit_mask = (uint32_t)1 << (uint32_t)button;
for (int8_t i = 0; i < 32; i++) {
bool pressed = joystick_button(joy, button);
if (pressed) {
if ((s_button_state & bit_mask) == 0) {
s_button_state |= bit_mask;
}
} else {
if ((s_button_state & bit_mask) == bit_mask) {
s_button_state &= ~bit_mask;
return true;
}
}
}
return false;
}
joystick_t* joystick_open() {
s_keyboard_state = SDL_GetKeyboardState(NULL);
joystick_t* joy = malloc(sizeof(joystick_t));
joy->index = -1;
joy->name = NULL;
joy->mapping = NULL;
joy->controller = NULL;
int32_t number_of_joysticks = SDL_NumJoysticks();
for (int32_t i = 0; i < number_of_joysticks; ++i) {
if (SDL_IsGameController(i) == SDL_TRUE) {
joy->index = i;
joy->controller = SDL_GameControllerOpen(i);
joy->name = SDL_GameControllerNameForIndex(i);
joy->mapping = SDL_GameControllerMappingForIndex(i);
break;
}
}
return joy;
}
void joystick_close(joystick_t* joy) {
if (joy == NULL)
return;
if (joy->controller != NULL)
SDL_GameControllerClose(joy->controller);
if (joy->mapping != NULL)
SDL_free((void*) joy->mapping);
if (joy->name != NULL)
SDL_free((void*) joy->name);
}