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::VK_Controller Class Reference

RAII wrapper for SDL_Gamepad (standard layout mapping). More...

#include <mxvk/include/mxvk/mxvk_controller.hpp>

Public Member Functions

bool active () const
 Check whether the controller is currently usable.
void close ()
 Close the controller handle if open.
bool connectEvent (SDL_Event &e)
 Handle a device-added/removed event to maintain connection state.
int controllerIndex () const
 Return the device index this controller was opened with.
Sint16 getAxis (SDL_GamepadAxis axis) const
 Read the current axis value.
bool getButton (SDL_GamepadButton button) const
 Test whether a mapped button is currently pressed.
Uint8 getHat (int hat) const
 Read the position of a POV hat.
std::optional< SDL_Gamepad * > handle () const
 Return the underlying SDL_Gamepad handle as an optional.
std::string name () const
 Return the controller's name as reported by SDL.
bool open (int index)
 Open a gamepad by device index.
VK_Controlleroperator= (const VK_Controller &)=delete
VK_Controlleroperator= (VK_Controller &&)=delete
SDL_Gamepad * unwrap () const
 Unwrap the SDL_Gamepad pointer, asserting it is open.
 VK_Controller ()
 Default constructor — device not yet opened.
 VK_Controller (const VK_Controller &)=delete
 VK_Controller (VK_Controller &&)=delete
 ~VK_Controller () noexcept
 Destructor — closes the controller if open.

Static Public Member Functions

static int joysticks ()
 Return the number of connected joysticks/controllers.

Protected Member Functions

bool openByInstanceId (SDL_JoystickID instanceId, int index_hint=-1)

Protected Attributes

int deviceIndex = -1
 Open index in the current gamepad list.
SDL_JoystickID instanceId = 0
 Stable SDL gamepad instance identifier.
SDL_Gamepad * stick = nullptr
 Underlying SDL gamepad handle.

Detailed Description

RAII wrapper for SDL_Gamepad (standard layout mapping).

Provides button, hat, and axis queries using SDL's gamepad API, which normalizes button layouts across different physical devices.

Definition at line 114 of file mxvk_controller.hpp.

Constructor & Destructor Documentation

◆ VK_Controller() [1/3]

mxvk::VK_Controller::VK_Controller ( )
default

Default constructor — device not yet opened.

◆ ~VK_Controller()

mxvk::VK_Controller::~VK_Controller ( )
noexcept

Destructor — closes the controller if open.

Definition at line 140 of file mxvk_controller.cpp.

140 {
141 close();
142 }
void close()
Close the controller handle if open.

◆ VK_Controller() [2/3]

mxvk::VK_Controller::VK_Controller ( const VK_Controller & )
delete

◆ VK_Controller() [3/3]

mxvk::VK_Controller::VK_Controller ( VK_Controller && )
delete

Member Function Documentation

◆ active()

bool mxvk::VK_Controller::active ( ) const
nodiscard

Check whether the controller is currently usable.

Returns
true if the controller is open and has a valid index.

Definition at line 246 of file mxvk_controller.cpp.

246 {
247 return stick != nullptr && SDL_GamepadConnected(stick);
248 }
SDL_Gamepad * stick
Underlying SDL gamepad handle.

◆ close()

void mxvk::VK_Controller::close ( )

Close the controller handle if open.

Definition at line 192 of file mxvk_controller.cpp.

192 {
193 if (stick != nullptr) {
194 SDL_CloseGamepad(stick);
195 }
196
197 stick = nullptr;
198 deviceIndex = -1;
199 instanceId = 0;
200 }
SDL_JoystickID instanceId
Stable SDL gamepad instance identifier.
int deviceIndex
Open index in the current gamepad list.

◆ connectEvent()

bool mxvk::VK_Controller::connectEvent ( SDL_Event & e)

Handle a device-added/removed event to maintain connection state.

Parameters
eSDL event to inspect.
Returns
true if the event was a controller connect/disconnect.

Definition at line 250 of file mxvk_controller.cpp.

250 {
251 if (e.type == SDL_EVENT_GAMEPAD_ADDED) {
252 return openByInstanceId(e.gdevice.which);
253 }
254
255 if (e.type == SDL_EVENT_GAMEPAD_REMOVED) {
256 if (stick != nullptr && instanceId == e.gdevice.which) {
257 close();
258 }
259 return true;
260 }
261
262 return false;
263 }
bool openByInstanceId(SDL_JoystickID instanceId, int index_hint=-1)

◆ controllerIndex()

int mxvk::VK_Controller::controllerIndex ( ) const

Return the device index this controller was opened with.

Returns
Device index.

Definition at line 216 of file mxvk_controller.cpp.

216 {
217 return deviceIndex;
218 }

◆ getAxis()

Sint16 mxvk::VK_Controller::getAxis ( SDL_GamepadAxis axis) const
nodiscard

Read the current axis value.

Parameters
axisSDL_GamepadAxis constant.
Returns
Axis value in the range [-32768, 32767].

Definition at line 239 of file mxvk_controller.cpp.

239 {
240 if (stick == nullptr) {
241 return 0;
242 }
243 return SDL_GetGamepadAxis(stick, axis);
244 }

◆ getButton()

bool mxvk::VK_Controller::getButton ( SDL_GamepadButton button) const
nodiscard

Test whether a mapped button is currently pressed.

Parameters
buttonSDL_GamepadButton constant.
Returns
true if pressed.

Definition at line 220 of file mxvk_controller.cpp.

220 {
221 if (stick == nullptr) {
222 return false;
223 }
224 return SDL_GetGamepadButton(stick, button);
225 }

◆ getHat()

Uint8 mxvk::VK_Controller::getHat ( int hat) const
nodiscard

Read the position of a POV hat.

Parameters
hatHat index.
Returns
SDL hat position bitmask.

Definition at line 227 of file mxvk_controller.cpp.

227 {
228 if (stick == nullptr) {
229 return 0;
230 }
231
232 SDL_Joystick *joystick = SDL_GetGamepadJoystick(stick);
233 if (joystick == nullptr) {
234 return 0;
235 }
236 return SDL_GetJoystickHat(joystick, hat);
237 }

◆ handle()

std::optional< SDL_Gamepad * > mxvk::VK_Controller::handle ( ) const
nodiscard

Return the underlying SDL_Gamepad handle as an optional.

Returns
std::optional containing the handle, or std::nullopt.

Definition at line 202 of file mxvk_controller.cpp.

202 {
203 if (stick != nullptr) {
204 return stick;
205 }
206 return std::nullopt;
207 }

◆ joysticks()

int mxvk::VK_Controller::joysticks ( )
static

Return the number of connected joysticks/controllers.

Returns
SDL_NumJoysticks() result.

Definition at line 144 of file mxvk_controller.cpp.

144 {
145 int count = 0;
146 SDL_JoystickID *ids = SDL_GetGamepads(&count);
147 if (ids != nullptr) {
148 SDL_free(ids);
149 }
150 return count;
151 }

◆ name()

std::string mxvk::VK_Controller::name ( ) const

Return the controller's name as reported by SDL.

Returns
Device name string.

Definition at line 182 of file mxvk_controller.cpp.

182 {
183 if (stick != nullptr) {
184 const char *device_name = SDL_GetGamepadName(stick);
185 if (device_name != nullptr) {
186 return device_name;
187 }
188 }
189 return "Controller Not Opened.";
190 }

◆ open()

bool mxvk::VK_Controller::open ( int index)

Open a gamepad by device index.

Parameters
indexSDL gamepad index (0-based) within the current device list.
Returns
true on success.

Definition at line 167 of file mxvk_controller.cpp.

167 {
168 int count = 0;
169 SDL_JoystickID *ids = SDL_GetGamepads(&count);
170 if (ids == nullptr || index < 0 || index >= count) {
171 if (ids != nullptr) {
172 SDL_free(ids);
173 }
174 return false;
175 }
176
177 const SDL_JoystickID openedInstanceId = ids[index];
178 SDL_free(ids);
179 return openByInstanceId(openedInstanceId, index);
180 }

◆ openByInstanceId()

bool mxvk::VK_Controller::openByInstanceId ( SDL_JoystickID instanceId,
int index_hint = -1 )
protected

Definition at line 153 of file mxvk_controller.cpp.

153 {
154 close();
155
156 stick = SDL_OpenGamepad(newInstanceId);
157 if (stick == nullptr) {
158 return false;
159 }
160
161 deviceIndex = index_hint;
162 instanceId = newInstanceId;
163 SDL_SetGamepadEventsEnabled(true);
164 return true;
165 }

◆ operator=() [1/2]

VK_Controller & mxvk::VK_Controller::operator= ( const VK_Controller & )
delete

◆ operator=() [2/2]

VK_Controller & mxvk::VK_Controller::operator= ( VK_Controller && )
delete

◆ unwrap()

SDL_Gamepad * mxvk::VK_Controller::unwrap ( ) const
nodiscard

Unwrap the SDL_Gamepad pointer, asserting it is open.

Returns
Raw SDL_Gamepad pointer.

Definition at line 209 of file mxvk_controller.cpp.

209 {
210 if (stick != nullptr) {
211 return stick;
212 }
213 throw mxvk::Exception("Invalid controller handle");
214 }

Member Data Documentation

◆ deviceIndex

int mxvk::VK_Controller::deviceIndex = -1
protected

Open index in the current gamepad list.

Definition at line 204 of file mxvk_controller.hpp.

◆ instanceId

SDL_JoystickID mxvk::VK_Controller::instanceId = 0
protected

Stable SDL gamepad instance identifier.

Definition at line 205 of file mxvk_controller.hpp.

◆ stick

SDL_Gamepad* mxvk::VK_Controller::stick = nullptr
protected

Underlying SDL gamepad handle.

Definition at line 203 of file mxvk_controller.hpp.


The documentation for this class was generated from the following files: