libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
joystick.cpp
Go to the documentation of this file.
1/**
2 * @file joystick.cpp
3 * @brief Implementation of mx::Joystick and mx::Controller SDL joystick wrappers.
4 */
5#include"joystick.hpp"
6#include<optional>
7#include"tee_stream.hpp"
8
9namespace mx {
10
11 Joystick::Joystick() : stick{nullptr}, index{-1} {}
12
14 close();
15 }
16
18 stick = SDL_JoystickOpen(index);
19 if(!stick) {
20 this->index = -1;
21 stick = nullptr;
22 return false;
23 }
24 this->index = index;
25 SDL_JoystickEventState(SDL_ENABLE);
26 return true;
27 }
28
29 std::string Joystick::name() const {
30 if(stick) {
31 return SDL_JoystickName(stick);
32 }
33 return "Joystick Not Opened.\n";
34 }
35
37 if(stick)
38 SDL_JoystickClose(stick);
39
40 stick = nullptr;
41 index = -1;
42 }
43
44 std::optional<SDL_Joystick *> Joystick::handle() {
45 if(stick)
46 return stick;
47
48 return std::nullopt;
49 }
50
51 SDL_Joystick *Joystick::unwrap() const {
52 if(stick) {
53 return stick;
54 }
55 throw mx::Exception("Invalid Joystick");
56 return 0;
57 }
58
60 return index;
61 }
62
63 bool Joystick::getButton(int button) {
64 if(stick != nullptr && SDL_JoystickGetButton(stick, button))
65 return true;
66
67 return false;
68 }
69
70 Uint8 Joystick::getHat(int hat) {
71 if(stick) {
72 return SDL_JoystickGetHat(stick, hat);
73 }
74 return 0;
75 }
76
77 Sint16 Joystick::getAxis(int axis) {
78 if(stick) {
79 return SDL_JoystickGetAxis(stick, axis);
80 }
81 return 0;
82 }
83
85 if(stick)
86 return SDL_JoystickNumButtons(stick);
87 return 0;
88 }
89
91 if(stick)
92 return SDL_JoystickNumHats(stick);
93 return 0;
94 }
96 if(stick)
97 return SDL_JoystickNumAxes(stick);
98 return 0;
99 }
100
101 Controller::Controller() : stick{nullptr}, index{-1} {}
102
106
108 stick = SDL_GameControllerOpen(index);
109 if(!stick) {
110 this->index = -1;
111 stick = nullptr;
112 return false;
113 }
114 this->index = index;
115 SDL_GameControllerEventState(SDL_ENABLE);
116 return true;
117 }
118
119 std::string Controller::name() const {
120 if(stick) {
121 return SDL_GameControllerName(stick);
122 }
123 return "Controller Not Opened.\n";
124 }
125
127 if(stick)
128 SDL_GameControllerClose(stick);
129
130 stick = nullptr;
131 index = -1;
132 }
133
134 std::optional<SDL_GameController *> Controller::handle() {
135 if(stick)
136 return stick;
137
138 return std::nullopt;
139 }
140
141 SDL_GameController *Controller::unwrap() const {
142 if(stick) {
143 return stick;
144 }
145 throw Exception("unwrap: Invalid Controller");
146 return 0;
147 }
148
150 return index;
151 }
152
153 bool Controller::getButton(SDL_GameControllerButton button) {
154 if(stick != nullptr && SDL_GameControllerGetButton(stick, button))
155 return true;
156
157 return false;
158 }
159 Uint8 Controller::getHat(int hat) {
160 if (stick) {
161 SDL_Joystick *joystick = SDL_GameControllerGetJoystick(stick);
162 if (joystick) {
163 return SDL_JoystickGetHat(joystick, hat);
164 }
165 }
166 return 0;
167 }
168
169 Sint16 Controller::getAxis(SDL_GameControllerAxis axis) {
170 if(stick) {
171 return SDL_GameControllerGetAxis(stick, axis);
172 }
173 return 0;
174 }
175
176 bool Controller::connectEvent(SDL_Event &e) {
177 if (e.type == SDL_CONTROLLERDEVICEADDED) {
178 mx::system_out << "Controller attached for Player " << e.cdevice.which << "\n";
179 if (open(e.cdevice.which)) {
180 mx::system_out << "Connected Controller " << name() << "\n";
181 } else {
182 mx::system_out << "Failed to open controller: " << SDL_GetError() << ".\n";
183 }
184 return true;
185 }
186 if (e.type == SDL_CONTROLLERDEVICEREMOVED) {
187 mx::system_out << "Controller disconnected.\n";
188 close();
189 return true;
190 }
191 return false;
192 }
193}
std::string name() const
Return the controller's name as reported by SDL.
Definition joystick.cpp:119
int controllerIndex() const
Return the device index this controller was opened with.
Definition joystick.cpp:149
int index
Device index (-1 if not open).
Definition joystick.hpp:224
SDL_GameController * unwrap() const
Unwrap the SDL_GameController pointer, asserting it is open.
Definition joystick.cpp:141
bool getButton(SDL_GameControllerButton button)
Test whether a mapped button is currently pressed.
Definition joystick.cpp:153
bool connectEvent(SDL_Event &e)
Handle a device-added/removed event to maintain connection state.
Definition joystick.cpp:176
Sint16 getAxis(SDL_GameControllerAxis axis)
Read the current axis value.
Definition joystick.cpp:169
~Controller()
Destructor — closes the controller if open.
Definition joystick.cpp:103
std::optional< SDL_GameController * > handle()
Return the underlying SDL_GameController handle as an optional.
Definition joystick.cpp:134
void close()
Close the controller handle if open.
Definition joystick.cpp:126
Uint8 getHat(int hat)
Read the position of a POV hat.
Definition joystick.cpp:159
Controller()
Default constructor — device not yet opened.
Definition joystick.cpp:101
bool open(int index)
Open a game controller by device index.
Definition joystick.cpp:107
SDL_GameController * stick
Underlying SDL game controller handle.
Definition joystick.hpp:223
General-purpose exception class for the mx library.
Definition exception.hpp:20
~Joystick()
Destructor — closes the joystick if open.
Definition joystick.cpp:13
void close()
Close the joystick handle if open.
Definition joystick.cpp:36
Joystick()
Default constructor — device not yet opened.
Definition joystick.cpp:11
int numAxes()
Definition joystick.cpp:95
int numHats()
Definition joystick.cpp:90
Sint16 getAxis(int axis)
Read the current axis value.
Definition joystick.cpp:77
int numButtons()
Definition joystick.cpp:84
int joystickIndex() const
Return the device index this joystick was opened with.
Definition joystick.cpp:59
std::string name() const
Return the joystick's name as reported by SDL.
Definition joystick.cpp:29
SDL_Joystick * unwrap() const
Unwrap the SDL_Joystick pointer, asserting it is open.
Definition joystick.cpp:51
SDL_Joystick * stick
Underlying SDL joystick handle.
Definition joystick.hpp:116
std::optional< SDL_Joystick * > handle()
Return the underlying SDL_Joystick handle as an optional.
Definition joystick.cpp:44
bool getButton(int button)
Test whether a button is currently pressed.
Definition joystick.cpp:63
bool open(int index)
Open the joystick at the given device index.
Definition joystick.cpp:17
int index
Device index (-1 if not open).
Definition joystick.hpp:117
Uint8 getHat(int hat)
Read the position of a POV hat.
Definition joystick.cpp:70
SDL joystick and game controller RAII wrappers.
Definition cfg.cpp:11
TeeStream system_out(std::cout, log_file)
Global stdout + log tee stream.
Tee stream utilities that duplicate output to two sinks simultaneously.