10#include <SDL3/SDL_stdinc.h>
20 SDL_JoystickID *ids = SDL_GetJoysticks(&count);
31 SDL_JoystickID *ids = SDL_GetJoysticks(&count);
32 if (ids ==
nullptr || index < 0 || index >= count) {
39 const SDL_JoystickID openedInstanceId = ids[index];
42 stick = SDL_OpenJoystick(openedInstanceId);
43 if (
stick ==
nullptr) {
49 SDL_SetJoystickEventsEnabled(
true);
54 if (
stick !=
nullptr) {
55 const char *device_name = SDL_GetJoystickName(
stick);
56 if (device_name !=
nullptr) {
60 return "Joystick Not Opened.";
64 if (
stick !=
nullptr) {
65 SDL_CloseJoystick(
stick);
74 if (
stick !=
nullptr) {
81 if (
stick !=
nullptr) {
90 if (
stick ==
nullptr) {
93 return SDL_GetJoystickButton(
stick, button);
97 if (
stick ==
nullptr) {
100 return SDL_GetJoystickHat(
stick, hat);
104 if (
stick ==
nullptr) {
107 return SDL_GetJoystickAxis(
stick, axis);
111 if (
stick ==
nullptr) {
114 const int count = SDL_GetNumJoystickButtons(
stick);
115 return count < 0 ? 0 : count;
119 if (
stick ==
nullptr) {
122 const int count = SDL_GetNumJoystickHats(
stick);
123 return count < 0 ? 0 : count;
127 if (
stick ==
nullptr) {
130 const int count = SDL_GetNumJoystickAxes(
stick);
131 return count < 0 ? 0 : count;
140 SDL_JoystickID *ids = SDL_GetGamepads(&count);
141 if (ids !=
nullptr) {
150 stick = SDL_OpenGamepad(newInstanceId);
151 if (
stick ==
nullptr) {
157 SDL_SetGamepadEventsEnabled(
true);
163 SDL_JoystickID *ids = SDL_GetGamepads(&count);
164 if (ids ==
nullptr || index < 0 || index >= count) {
165 if (ids !=
nullptr) {
171 const SDL_JoystickID openedInstanceId = ids[index];
177 if (
stick !=
nullptr) {
178 const char *device_name = SDL_GetGamepadName(
stick);
179 if (device_name !=
nullptr) {
183 return "Controller Not Opened.";
187 if (
stick !=
nullptr) {
188 SDL_CloseGamepad(
stick);
197 if (
stick !=
nullptr) {
204 if (
stick !=
nullptr) {
213 if (
stick ==
nullptr) {
216 return SDL_GetGamepadButton(
stick, button);
220 if (
stick ==
nullptr) {
224 SDL_Joystick *joystick = SDL_GetGamepadJoystick(
stick);
225 if (joystick ==
nullptr) {
228 return SDL_GetJoystickHat(joystick, hat);
232 if (
stick ==
nullptr) {
235 return SDL_GetGamepadAxis(
stick, axis);
241 if (e.type == SDL_EVENT_GAMEPAD_ADDED) {
245 if (e.type == SDL_EVENT_GAMEPAD_REMOVED) {
std::optional< SDL_Gamepad * > handle() const
Return the underlying SDL_Gamepad handle as an optional.
int controllerIndex() const
Return the device index this controller was opened with.
void close()
Close the controller handle if open.
Sint16 getAxis(SDL_GamepadAxis axis) const
Read the current axis value.
SDL_Gamepad * unwrap() const
Unwrap the SDL_Gamepad pointer, asserting it is open.
SDL_Gamepad * stick
Underlying SDL gamepad handle.
SDL_JoystickID instanceId
Stable SDL gamepad instance identifier.
bool active() const
Check whether the controller is currently usable.
int deviceIndex
Open index in the current gamepad list.
bool openByInstanceId(SDL_JoystickID instanceId, int index_hint=-1)
bool getButton(SDL_GamepadButton button) const
Test whether a mapped button is currently pressed.
VK_Controller()
Default constructor — device not yet opened.
bool connectEvent(SDL_Event &e)
Handle a device-added/removed event to maintain connection state.
Uint8 getHat(int hat) const
Read the position of a POV hat.
static int joysticks()
Return the number of connected joysticks/controllers.
~VK_Controller() noexcept
Destructor — closes the controller if open.
bool open(int index)
Open a gamepad by device index.
std::string name() const
Return the controller's name as reported by SDL.
SDL_JoystickID instanceId
Stable SDL joystick instance identifier.
VK_Joystick()
Default constructor — device not yet opened.
~VK_Joystick() noexcept
Destructor — closes the joystick if open.
SDL_Joystick * unwrap() const
Unwrap the SDL_Joystick pointer, asserting it is open.
bool getButton(int button) const
Test whether a button is currently pressed.
bool open(int index)
Open the joystick at the given device index.
Uint8 getHat(int hat) const
Read the position of a POV hat.
static int joysticks()
Return the number of connected joysticks.
int deviceIndex
Open index in the current joystick list.
int joystickIndex() const
Return the device index this joystick was opened with.
SDL_Joystick * stick
Underlying SDL joystick handle.
std::optional< SDL_Joystick * > handle() const
Return the underlying SDL_Joystick handle as an optional.
std::string name() const
Return the joystick's name as reported by SDL.
Sint16 getAxis(int axis) const
Read the current axis value.
void close()
Close the joystick handle if open.
SDL3 joystick and gamepad RAII wrappers.
Utilities for loading and saving PNG images.