-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
157 lines (140 loc) 路 5.3 KB
/
Copy pathcontroller.cpp
File metadata and controls
157 lines (140 loc) 路 5.3 KB
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
#include "controller.h"
Controller::Controller(SDL_GameController* _controller) {
controller = _controller;
id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
}
bool Controller::isButtonPressed(SDL_GameControllerButton button) {
if (button < 0 || button >= SDL_CONTROLLER_BUTTON_MAX) {
cout << "Invalid button index: " << button << endl;
return false; // Invalid button index
}
return buttonsPressed[button];
}
int16_t Controller::getAxisValue(SDL_GameControllerAxis axis) {
if (axis < 0 || axis >= SDL_CONTROLLER_AXIS_MAX) {
cout << "Invalid axis index: " << axis << endl;
return 0; // Invalid axis index
}
return axisValues[axis];
}
SDL_JoystickID Controller::getID() {
return id;
}
void Controller::setProcessingLevel(int level) {
if (level < 0 || level > 2) {
cout << "Invalid processing level: " << level << endl;
return; // Invalid processing level
}
processingLevel = level;
}
JoystickPosition Controller::getJoystickPosition(SDL_GameControllerAxis axisX, SDL_GameControllerAxis axisY) {
int16_t x = getAxisValue(axisX);
int16_t y = getAxisValue(axisY);
double magnitude = sqrt(x * x + y * y) / 32767.0; // Normalize to [0, 1]
if (processingLevel >= 1) {
magnitude = min(magnitude, 1.0); // Clamp magnitude to [0, 1]
}
double direction = atan2(-y, x); // Convert to radians
if (direction < 0) {
direction += 2 * M_PI; // Ensure direction is in [0, 2*PI)
}
if (processingLevel >= 2) { // Apply deadzone processing
if (magnitude < deadzone) {
magnitude = 0.0;
} else {
magnitude = (magnitude - deadzone) / (1.0 - deadzone); // Scale to [0, 1]
}
}
return {direction, magnitude}; // Clamp magnitude to [0, 1]
}
JoystickPosition Controller::getJoystickPositionLeft() {
return getJoystickPosition(SDL_CONTROLLER_AXIS_LEFTX, SDL_CONTROLLER_AXIS_LEFTY);
}
JoystickPosition Controller::getJoystickPositionRight() {
return getJoystickPosition(SDL_CONTROLLER_AXIS_RIGHTX, SDL_CONTROLLER_AXIS_RIGHTY);
}
// ----------- CONTROLLERS CLASS --------------
Controllers::Controllers() {
// Find all connected controllers at initialization
for (int i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGameController(i)) {
cout << "Controller found: " << i << endl;
controllers.push_back(Controller(SDL_GameControllerOpen(i)));
}
}
}
void Controllers::eventUpdate(SDL_Event event) {
switch (event.type) {
case SDL_CONTROLLERDEVICEADDED: {
bool found = false;
for (const auto& controller : controllers) {
if (controller.id == event.cdevice.which) {
found = true;
break;
}
}
if (!found) {
cout << "Controller added: " << event.cdevice.which << endl;
controllers.push_back(Controller(SDL_GameControllerOpen(event.cdevice.which)));
}
break;
}
case SDL_CONTROLLERDEVICEREMOVED:
cout << "Controller removed: " << event.cdevice.which << endl;
for (auto it = controllers.begin(); it != controllers.end(); ++it) {
if (it->controller && event.cdevice.which == it->id) {
SDL_GameControllerClose(it->controller);
controllers.erase(it);
break;
}
}
break;
case SDL_CONTROLLERDEVICEREMAPPED:
for (auto& controller : controllers) {
if (controller.controller && event.cdevice.which == controller.id) {
SDL_GameControllerClose(controller.controller);
controller.controller = SDL_GameControllerOpen(event.cdevice.which);
break;
}
}
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
if (event.cbutton.button != -1) {
for (auto& controller : controllers) {
if (controller.controller && event.cbutton.which == controller.id) {
controller.buttonsPressed[event.cbutton.button] = (event.type == SDL_CONTROLLERBUTTONDOWN);
break;
}
}
}
break;
case SDL_CONTROLLERAXISMOTION:
for (auto& controller : controllers) {
if (controller.controller && event.caxis.which == controller.id) {
controller.axisValues[event.caxis.axis] = event.caxis.value;
break;
}
}
break;
}
}
vector<SDL_JoystickID> Controllers::getControllerIDs() {
vector<SDL_JoystickID> ids;
for (const auto& controller : controllers) {
ids.push_back(controller.id);
}
return ids;
}
Controller* Controllers::getControllerByID(SDL_JoystickID id) {
for (auto& controller : controllers) {
if (controller.id == id) {
return &controller;
}
}
throw std::runtime_error("Controller with the given ID not found.");
}
Controller* Controllers::getControllerNum(int num) {
if (num < 0 || num >= controllers.size()) return nullptr;
return &controllers[num];
}