Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameController.cpp
More file actions
Latest commit
84 lines (72 loc) · 2.46 KB
/
Copy pathGameController.cpp
File metadata and controls
84 lines (72 loc) · 2.46 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
#include"GameController.hpp"
/*static*/ std::mutex GameController::s_joyCntLck;
/*static*/uint64_t GameController::s_joyCnt{0};
voidGameController::Periodic()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {}
/* poll for game controller disconnects */
if (!IsConnected()) {
/* one of the game controllers disconnected, assume it was ours */
Close();
}
/* game controller may have disconnected, make sure it's still valid */
if (!_joy) {
/* no game controller, initialize a new one */
Init();
}
}
voidGameController::ReportMissingGameController()
{
autoconst now = std::chrono::steady_clock::now();
autoconst dtMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastErrorTime).count();
if (dtMs > kErrorTimeMs) {
fprintf(stderr, "Warning: Could not find game controller on port %d\n", _port);
_lastErrorTime = now;
}
}
SDL_GameController *GameController::CreateGameController()
{
/* poll for game controller */
int res = SDL_NumJoysticks();
if (res < 0) {
/* error trying to get joysticks */
fprintf(stderr, "Error getting game controllers: %d\n", res);
ReportMissingGameController();
returnnullptr;
} elseif (res <= _port) {
/* not enough joysticks for given port */
ReportMissingGameController();
returnnullptr;
}
/* joystick found */
if (SDL_IsGameController(_port)) {
/* valid game controller */
returnSDL_GameControllerOpen(_port);
} else {
/* not a valid game controller */
ReportMissingGameController();
returnnullptr;
}
}
voidGameController::PrintGameControllerInfo() const
{
/* Get information about the game controller */
autoconst name = GetName();
autoconst type = GetType();
charconst *typeStr;
switch (type) {
caseSDL_CONTROLLER_TYPE_XBOX360: typeStr = "Xbox 360"; break;
caseSDL_CONTROLLER_TYPE_XBOXONE: typeStr = "Xbox One"; break;
caseSDL_CONTROLLER_TYPE_PS3: typeStr = "PS3"; break;
caseSDL_CONTROLLER_TYPE_PS4: typeStr = "PS4"; break;
caseSDL_CONTROLLER_TYPE_PS5: typeStr = "PS5"; break;
default:
caseSDL_CONTROLLER_TYPE_UNKNOWN: typeStr = "Unknown"; break;
}
/* print information */
printf("Connected to game controller '%s'\n"
" Type: %s\n"
" Port: %d\n",
name.c_str(), typeStr, _port);
}