25 SDL_JoystickEventState(SDL_ENABLE);
31 return SDL_JoystickName(
stick);
33 return "Joystick Not Opened.\n";
38 SDL_JoystickClose(
stick);
64 if(
stick !=
nullptr && SDL_JoystickGetButton(
stick, button))
72 return SDL_JoystickGetHat(
stick, hat);
79 return SDL_JoystickGetAxis(
stick, axis);
86 return SDL_JoystickNumButtons(
stick);
92 return SDL_JoystickNumHats(
stick);
97 return SDL_JoystickNumAxes(
stick);
115 SDL_GameControllerEventState(SDL_ENABLE);
121 return SDL_GameControllerName(
stick);
123 return "Controller Not Opened.\n";
128 SDL_GameControllerClose(
stick);
145 throw Exception(
"unwrap: Invalid Controller");
154 if(
stick !=
nullptr && SDL_GameControllerGetButton(
stick, button))
161 SDL_Joystick *joystick = SDL_GameControllerGetJoystick(
stick);
163 return SDL_JoystickGetHat(joystick, hat);
171 return SDL_GameControllerGetAxis(
stick, axis);
177 if (e.type == SDL_CONTROLLERDEVICEADDED) {
178 mx::system_out <<
"Controller attached for Player " << e.cdevice.which <<
"\n";
179 if (
open(e.cdevice.which)) {
182 mx::system_out <<
"Failed to open controller: " << SDL_GetError() <<
".\n";
186 if (e.type == SDL_CONTROLLERDEVICEREMOVED) {
std::string name() const
Return the controller's name as reported by SDL.
int controllerIndex() const
Return the device index this controller was opened with.
int index
Device index (-1 if not open).
SDL_GameController * unwrap() const
Unwrap the SDL_GameController pointer, asserting it is open.
bool getButton(SDL_GameControllerButton button)
Test whether a mapped button is currently pressed.
bool connectEvent(SDL_Event &e)
Handle a device-added/removed event to maintain connection state.
Sint16 getAxis(SDL_GameControllerAxis axis)
Read the current axis value.
~Controller()
Destructor — closes the controller if open.
std::optional< SDL_GameController * > handle()
Return the underlying SDL_GameController handle as an optional.
void close()
Close the controller handle if open.
Uint8 getHat(int hat)
Read the position of a POV hat.
Controller()
Default constructor — device not yet opened.
bool open(int index)
Open a game controller by device index.
SDL_GameController * stick
Underlying SDL game controller handle.
General-purpose exception class for the mx library.
~Joystick()
Destructor — closes the joystick if open.
void close()
Close the joystick handle if open.
Joystick()
Default constructor — device not yet opened.
Sint16 getAxis(int axis)
Read the current axis value.
int joystickIndex() const
Return the device index this joystick was opened with.
std::string name() const
Return the joystick's name as reported by SDL.
SDL_Joystick * unwrap() const
Unwrap the SDL_Joystick pointer, asserting it is open.
SDL_Joystick * stick
Underlying SDL joystick handle.
std::optional< SDL_Joystick * > handle()
Return the underlying SDL_Joystick handle as an optional.
bool getButton(int button)
Test whether a button is currently pressed.
bool open(int index)
Open the joystick at the given device index.
int index
Device index (-1 if not open).
Uint8 getHat(int hat)
Read the position of a POV hat.
SDL joystick and game controller RAII wrappers.
TeeStream system_out(std::cout, log_file)
Global stdout + log tee stream.
Tee stream utilities that duplicate output to two sinks simultaneously.