MXVK Vulkan Framework 0.35.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_Controller & operator= (const VK_Controller &)=delete
VK_Controller & operator= (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 136 of file mxvk_controller.cpp.

136{ close(); }
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 238 of file mxvk_controller.cpp.

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

◆ close()

void mxvk::VK_Controller::close ( )

Close the controller handle if open.

Definition at line 186 of file mxvk_controller.cpp.

186 {
187 if (stick != nullptr) {
188 SDL_CloseGamepad(stick);
189 }
190
191 stick = nullptr;
192 deviceIndex = -1;
193 instanceId = 0;
194 }
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 240 of file mxvk_controller.cpp.

240 {
241 if (e.type == SDL_EVENT_GAMEPAD_ADDED) {
242 return openByInstanceId(e.gdevice.which);
243 }
244
245 if (e.type == SDL_EVENT_GAMEPAD_REMOVED) {
246 if (stick != nullptr && instanceId == e.gdevice.which) {
247 close();
248 }
249 return true;
250 }
251
252 return false;
253 }
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 210 of file mxvk_controller.cpp.

210{ return deviceIndex; }

◆ 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 231 of file mxvk_controller.cpp.

231 {
232 if (stick == nullptr) {
233 return 0;
234 }
235 return SDL_GetGamepadAxis(stick, axis);
236 }

◆ 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 212 of file mxvk_controller.cpp.

212 {
213 if (stick == nullptr) {
214 return false;
215 }
216 return SDL_GetGamepadButton(stick, button);
217 }

◆ 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 219 of file mxvk_controller.cpp.

219 {
220 if (stick == nullptr) {
221 return 0;
222 }
223
224 SDL_Joystick *joystick = SDL_GetGamepadJoystick(stick);
225 if (joystick == nullptr) {
226 return 0;
227 }
228 return SDL_GetJoystickHat(joystick, hat);
229 }

◆ 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 196 of file mxvk_controller.cpp.

196 {
197 if (stick != nullptr) {
198 return stick;
199 }
200 return std::nullopt;
201 }

◆ joysticks()

int mxvk::VK_Controller::joysticks ( )
static

Return the number of connected joysticks/controllers.

Returns
SDL_NumJoysticks() result.

Definition at line 138 of file mxvk_controller.cpp.

138 {
139 int count = 0;
140 SDL_JoystickID *ids = SDL_GetGamepads(&count);
141 if (ids != nullptr) {
142 SDL_free(ids);
143 }
144 return count;
145 }

◆ name()

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

Return the controller's name as reported by SDL.

Returns
Device name string.

Definition at line 176 of file mxvk_controller.cpp.

176 {
177 if (stick != nullptr) {
178 const char *device_name = SDL_GetGamepadName(stick);
179 if (device_name != nullptr) {
180 return device_name;
181 }
182 }
183 return "Controller Not Opened.";
184 }

◆ 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 161 of file mxvk_controller.cpp.

161 {
162 int count = 0;
163 SDL_JoystickID *ids = SDL_GetGamepads(&count);
164 if (ids == nullptr || index < 0 || index >= count) {
165 if (ids != nullptr) {
166 SDL_free(ids);
167 }
168 return false;
169 }
170
171 const SDL_JoystickID openedInstanceId = ids[index];
172 SDL_free(ids);
173 return openByInstanceId(openedInstanceId, index);
174 }

◆ openByInstanceId()

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

Definition at line 147 of file mxvk_controller.cpp.

147 {
148 close();
149
150 stick = SDL_OpenGamepad(newInstanceId);
151 if (stick == nullptr) {
152 return false;
153 }
154
155 deviceIndex = index_hint;
156 instanceId = newInstanceId;
157 SDL_SetGamepadEventsEnabled(true);
158 return true;
159 }

◆ 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 203 of file mxvk_controller.cpp.

203 {
204 if (stick != nullptr) {
205 return stick;
206 }
207 throw mxvk::Exception("Invalid controller handle");
208 }

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: