-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameController.hpp
167 lines (146 loc) · 3.93 KB
/
GameController.hpp
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
#pragma once
#include <SDL2/SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <chrono>
#include <mutex>
/**
* Manages a game controller using the SDL 2 library.
*
* Note: This requires Ubuntu 22.04+ or Debian Bullseye.
*/
class GameController {
private:
SDL_GameController *_joy = nullptr;
int _port;
static std::mutex s_joyCntLck;
static uint64_t s_joyCnt;
/** Initialize this game controller. */
void Init()
{
if (_joy) {
/* close any existing game controller first */
Close();
}
_joy = CreateGameController();
if (_joy) {
/* Print information about the game controller */
PrintGameControllerInfo();
}
}
/** Closes this game controller. */
void Close()
{
/* do nothing if no game controller */
if (!_joy) return;
SDL_GameControllerClose(_joy);
_joy = nullptr;
}
public:
/**
* Creates a game controller on the given port.
*/
GameController(int port) : _port{port}
{
/* first increment game controller count */
{
std::lock_guard lck{s_joyCntLck};
++s_joyCnt;
}
/* then initialize this game controller */
Init();
}
~GameController()
{
/* first close this game controller */
Close();
/* then decrement game controller count */
{
std::lock_guard lck{s_joyCntLck};
if (--s_joyCnt == 0) {
/* this was the last game controller, quit SDL */
SDL_Quit();
}
}
}
/**
* Returns the port of this game controller.
*/
int GetPort() const { return _port; }
/**
* Returns the name of this game controller.
*/
std::string GetName() const
{
if (_joy) {
return SDL_GameControllerName(_joy);
} else {
return "";
}
}
/**
* Returns the SDL type of this game controller.
*/
SDL_GameControllerType GetType() const
{
if (_joy) {
return SDL_GameControllerGetType(_joy);
} else {
return SDL_CONTROLLER_TYPE_UNKNOWN;
}
}
/**
* Returns true if the given SDL button is pressed,
* false otherwise or in the case of an error.
*/
bool GetButton(SDL_GameControllerButton button) const
{
if (_joy) {
return SDL_GameControllerGetButton(_joy, button);
} else {
return false;
}
}
/**
* Returns the value of the given SDL axis from
* -1.0 to 1.0, or 0 in the case of an error.
*/
double GetAxis(SDL_GameControllerAxis axis) const
{
if (_joy) {
return SDL_GameControllerGetAxis(_joy, axis) / 32768.0;
} else {
return 0.0;
}
}
/**
* Returns whether this game controller is currently connected.
*/
bool IsConnected() const;
/**
* Periodically manages this game controller, handling disconnect
* events in the process.
*/
void Periodic()
{
/* 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();
}
}
private:
static constexpr auto kErrorTimeMs = 2000;
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
/** Reports a missing game controller with debouncing. */
void ReportMissingGameController();
/** Creates and returns a game controller. */
SDL_GameController *CreateGameController();
/** Prints out information about this game controller. */
void PrintGameControllerInfo() const;
};