10#include <SDL3/SDL_stdinc.h>
22 SDL_JoystickID *ids = SDL_GetJoysticks(&count);
33 SDL_JoystickID *ids = SDL_GetJoysticks(&count);
34 if (ids ==
nullptr || index < 0 || index >= count) {
41 const SDL_JoystickID openedInstanceId = ids[index];
44 stick = SDL_OpenJoystick(openedInstanceId);
45 if (
stick ==
nullptr) {
51 SDL_SetJoystickEventsEnabled(
true);
56 if (
stick !=
nullptr) {
57 const char *device_name = SDL_GetJoystickName(
stick);
58 if (device_name !=
nullptr) {
62 return "Joystick Not Opened.";
66 if (
stick !=
nullptr) {
67 SDL_CloseJoystick(
stick);
76 if (
stick !=
nullptr) {
83 if (
stick !=
nullptr) {
94 if (
stick ==
nullptr) {
97 return SDL_GetJoystickButton(
stick, button);
101 if (
stick ==
nullptr) {
104 return SDL_GetJoystickHat(
stick, hat);
108 if (
stick ==
nullptr) {
111 return SDL_GetJoystickAxis(
stick, axis);
115 if (
stick ==
nullptr) {
118 const int count = SDL_GetNumJoystickButtons(
stick);
119 return count < 0 ? 0 : count;
123 if (
stick ==
nullptr) {
126 const int count = SDL_GetNumJoystickHats(
stick);
127 return count < 0 ? 0 : count;
131 if (
stick ==
nullptr) {
134 const int count = SDL_GetNumJoystickAxes(
stick);
135 return count < 0 ? 0 : count;
146 SDL_JoystickID *ids = SDL_GetGamepads(&count);
147 if (ids !=
nullptr) {
156 stick = SDL_OpenGamepad(newInstanceId);
157 if (
stick ==
nullptr) {
163 SDL_SetGamepadEventsEnabled(
true);
169 SDL_JoystickID *ids = SDL_GetGamepads(&count);
170 if (ids ==
nullptr || index < 0 || index >= count) {
171 if (ids !=
nullptr) {
177 const SDL_JoystickID openedInstanceId = ids[index];
183 if (
stick !=
nullptr) {
184 const char *device_name = SDL_GetGamepadName(
stick);
185 if (device_name !=
nullptr) {
189 return "Controller Not Opened.";
193 if (
stick !=
nullptr) {
194 SDL_CloseGamepad(
stick);
203 if (
stick !=
nullptr) {
210 if (
stick !=
nullptr) {
221 if (
stick ==
nullptr) {
224 return SDL_GetGamepadButton(
stick, button);
228 if (
stick ==
nullptr) {
232 SDL_Joystick *joystick = SDL_GetGamepadJoystick(
stick);
233 if (joystick ==
nullptr) {
236 return SDL_GetJoystickHat(joystick, hat);
240 if (
stick ==
nullptr) {
243 return SDL_GetGamepadAxis(
stick, axis);
247 return stick !=
nullptr && SDL_GamepadConnected(
stick);
251 if (e.type == SDL_EVENT_GAMEPAD_ADDED) {
255 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.