MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk_controller.hpp
Go to the documentation of this file.
1/**
2 * @file mxvk_controller.hpp
3 * @brief SDL3 joystick and gamepad RAII wrappers.
4 */
5#pragma once
6
7#include <SDL3/SDL.h>
8
9#include <optional>
10#include <string>
11
12namespace mxvk {
13
14 /**
15 * @class VK_Joystick
16 * @brief RAII wrapper for a raw SDL_Joystick device.
17 *
18 * Opens an SDL joystick by index and provides axis, hat, and button
19 * readback. The handle is automatically closed on destruction.
20 */
22 public:
23 /** @brief Default constructor — device not yet opened. */
25 /** @brief Destructor — closes the joystick if open. */
26 ~VK_Joystick() noexcept;
27
28 VK_Joystick(const VK_Joystick &) = delete;
29 VK_Joystick &operator=(const VK_Joystick &) = delete;
31 VK_Joystick &operator=(VK_Joystick &&) = delete;
32
33 /**
34 * @brief Open the joystick at the given device index.
35 * @param index SDL joystick index (0-based) within the current device list.
36 * @return @c true on success.
37 */
38 bool open(int index);
39
40 /**
41 * @brief Return the joystick's name as reported by SDL.
42 * @return Device name string.
43 */
44 std::string name() const;
45
46 /** @brief Close the joystick handle if open. */
47 void close();
48
49 /**
50 * @brief Return the underlying SDL_Joystick handle as an optional.
51 * @return std::optional containing the handle, or std::nullopt.
52 */
53 [[nodiscard]] std::optional<SDL_Joystick *> handle() const;
54
55 /**
56 * @brief Unwrap the SDL_Joystick pointer, asserting it is open.
57 * @return Raw SDL_Joystick pointer.
58 */
59 SDL_Joystick *unwrap() const;
60
61 /**
62 * @brief Return the device index this joystick was opened with.
63 * @return Device index.
64 */
65 int joystickIndex() const;
66
67 /**
68 * @brief Return the number of connected joysticks.
69 * @return SDL_NumJoysticks() result.
70 */
71 static int joysticks();
72
73 /** @return Number of buttons on this joystick. */
74 [[nodiscard]] int numButtons() const;
75 /** @return Number of hats on this joystick. */
76 [[nodiscard]] int numHats() const;
77 /** @return Number of axes on this joystick. */
78 [[nodiscard]] int numAxes() const;
79
80 /**
81 * @brief Test whether a button is currently pressed.
82 * @param button Button index.
83 * @return @c true if pressed.
84 */
85 [[nodiscard]] bool getButton(int button) const;
86
87 /**
88 * @brief Read the position of a POV hat.
89 * @param hat Hat index.
90 * @return SDL hat position bitmask.
91 */
92 [[nodiscard]] Uint8 getHat(int hat) const;
93
94 /**
95 * @brief Read the current axis value.
96 * @param axis Axis index.
97 * @return Axis value in the range [-32768, 32767].
98 */
99 [[nodiscard]] Sint16 getAxis(int axis) const;
100
101 protected:
102 SDL_Joystick *stick = nullptr; ///< Underlying SDL joystick handle.
103 int deviceIndex = -1; ///< Open index in the current joystick list.
104 SDL_JoystickID instanceId = 0; ///< Stable SDL joystick instance identifier.
105 };
106
107 /**
108 * @class VK_Controller
109 * @brief RAII wrapper for SDL_Gamepad (standard layout mapping).
110 *
111 * Provides button, hat, and axis queries using SDL's gamepad API,
112 * which normalizes button layouts across different physical devices.
113 */
115 public:
116 /** @brief Default constructor — device not yet opened. */
118 /** @brief Destructor — closes the controller if open. */
119 ~VK_Controller() noexcept;
120
121 VK_Controller(const VK_Controller &) = delete;
122 VK_Controller &operator=(const VK_Controller &) = delete;
124 VK_Controller &operator=(VK_Controller &&) = delete;
125
126 /**
127 * @brief Open a gamepad by device index.
128 * @param index SDL gamepad index (0-based) within the current device list.
129 * @return @c true on success.
130 */
131 bool open(int index);
132
133 /**
134 * @brief Handle a device-added/removed event to maintain connection state.
135 * @param e SDL event to inspect.
136 * @return @c true if the event was a controller connect/disconnect.
137 */
138 bool connectEvent(SDL_Event &e);
139
140 /**
141 * @brief Return the controller's name as reported by SDL.
142 * @return Device name string.
143 */
144 std::string name() const;
145
146 /** @brief Close the controller handle if open. */
147 void close();
148
149 /**
150 * @brief Return the underlying SDL_Gamepad handle as an optional.
151 * @return std::optional containing the handle, or std::nullopt.
152 */
153 [[nodiscard]] std::optional<SDL_Gamepad *> handle() const;
154
155 /**
156 * @brief Unwrap the SDL_Gamepad pointer, asserting it is open.
157 * @return Raw SDL_Gamepad pointer.
158 */
159 [[nodiscard]] SDL_Gamepad *unwrap() const;
160
161 /**
162 * @brief Return the device index this controller was opened with.
163 * @return Device index.
164 */
165 int controllerIndex() const;
166
167 /**
168 * @brief Return the number of connected joysticks/controllers.
169 * @return SDL_NumJoysticks() result.
170 */
171 static int joysticks();
172
173 /**
174 * @brief Test whether a mapped button is currently pressed.
175 * @param button SDL_GamepadButton constant.
176 * @return @c true if pressed.
177 */
178 [[nodiscard]] bool getButton(SDL_GamepadButton button) const;
179
180 /**
181 * @brief Read the position of a POV hat.
182 * @param hat Hat index.
183 * @return SDL hat position bitmask.
184 */
185 [[nodiscard]] Uint8 getHat(int hat) const;
186
187 /**
188 * @brief Read the current axis value.
189 * @param axis SDL_GamepadAxis constant.
190 * @return Axis value in the range [-32768, 32767].
191 */
192 [[nodiscard]] Sint16 getAxis(SDL_GamepadAxis axis) const;
193
194 /**
195 * @brief Check whether the controller is currently usable.
196 * @return @c true if the controller is open and has a valid index.
197 */
198 [[nodiscard]] bool active() const;
199
200 protected:
201 bool openByInstanceId(SDL_JoystickID instanceId, int index_hint = -1);
202
203 SDL_Gamepad *stick = nullptr; ///< Underlying SDL gamepad handle.
204 int deviceIndex = -1; ///< Open index in the current gamepad list.
205 SDL_JoystickID instanceId = 0; ///< Stable SDL gamepad instance identifier.
206 };
207
208 // Backward-compatible aliases.
211
212} // namespace mxvk
213
RAII wrapper for SDL_Gamepad (standard layout mapping).
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.
bool open(int index)
Open a gamepad by device index.
std::string name() const
Return the controller's name as reported by SDL.
RAII wrapper for a raw SDL_Joystick device.
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.
mxvk::VK_Joystick VK_Joystick
mxvk::VK_Controller Controller
mxvk::VK_Controller VK_Controller
mxvk::VK_Joystick Joystick
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
VK_Controller Controller
VK_Joystick Joystick