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_Joystick Class Reference

RAII wrapper for a raw SDL_Joystick device. More...

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

Public Member Functions

void close ()
 Close the joystick handle if open.
Sint16 getAxis (int axis) const
 Read the current axis value.
bool getButton (int button) const
 Test whether a button is currently pressed.
Uint8 getHat (int hat) const
 Read the position of a POV hat.
std::optional< SDL_Joystick * > handle () const
 Return the underlying SDL_Joystick handle as an optional.
int joystickIndex () const
 Return the device index this joystick was opened with.
std::string name () const
 Return the joystick's name as reported by SDL.
int numAxes () const
int numButtons () const
int numHats () const
bool open (int index)
 Open the joystick at the given device index.
VK_Joystick & operator= (const VK_Joystick &)=delete
VK_Joystick & operator= (VK_Joystick &&)=delete
SDL_Joystick * unwrap () const
 Unwrap the SDL_Joystick pointer, asserting it is open.
 VK_Joystick ()
 Default constructor — device not yet opened.
 VK_Joystick (const VK_Joystick &)=delete
 VK_Joystick (VK_Joystick &&)=delete
 ~VK_Joystick () noexcept
 Destructor — closes the joystick if open.

Static Public Member Functions

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

Protected Attributes

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

Detailed Description

RAII wrapper for a raw SDL_Joystick device.

Opens an SDL joystick by index and provides axis, hat, and button readback. The handle is automatically closed on destruction.

Definition at line 21 of file mxvk_controller.hpp.

Constructor & Destructor Documentation

◆ VK_Joystick() [1/3]

mxvk::VK_Joystick::VK_Joystick ( )
default

Default constructor — device not yet opened.

◆ ~VK_Joystick()

mxvk::VK_Joystick::~VK_Joystick ( )
noexcept

Destructor — closes the joystick if open.

Definition at line 16 of file mxvk_controller.cpp.

16{ close(); }
void close()
Close the joystick handle if open.

◆ VK_Joystick() [2/3]

mxvk::VK_Joystick::VK_Joystick ( const VK_Joystick & )
delete

◆ VK_Joystick() [3/3]

mxvk::VK_Joystick::VK_Joystick ( VK_Joystick && )
delete

Member Function Documentation

◆ close()

void mxvk::VK_Joystick::close ( )

Close the joystick handle if open.

Definition at line 63 of file mxvk_controller.cpp.

63 {
64 if (stick != nullptr) {
65 SDL_CloseJoystick(stick);
66 }
67
68 stick = nullptr;
69 deviceIndex = -1;
70 instanceId = 0;
71 }
SDL_JoystickID instanceId
Stable SDL joystick instance identifier.
int deviceIndex
Open index in the current joystick list.
SDL_Joystick * stick
Underlying SDL joystick handle.

◆ getAxis()

Sint16 mxvk::VK_Joystick::getAxis ( int axis) const
nodiscard

Read the current axis value.

Parameters
axisAxis index.
Returns
Axis value in the range [-32768, 32767].

Definition at line 103 of file mxvk_controller.cpp.

103 {
104 if (stick == nullptr) {
105 return 0;
106 }
107 return SDL_GetJoystickAxis(stick, axis);
108 }

◆ getButton()

bool mxvk::VK_Joystick::getButton ( int button) const
nodiscard

Test whether a button is currently pressed.

Parameters
buttonButton index.
Returns
true if pressed.

Definition at line 89 of file mxvk_controller.cpp.

89 {
90 if (stick == nullptr) {
91 return false;
92 }
93 return SDL_GetJoystickButton(stick, button);
94 }

◆ getHat()

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

Read the position of a POV hat.

Parameters
hatHat index.
Returns
SDL hat position bitmask.

Definition at line 96 of file mxvk_controller.cpp.

96 {
97 if (stick == nullptr) {
98 return 0;
99 }
100 return SDL_GetJoystickHat(stick, hat);
101 }

◆ handle()

std::optional< SDL_Joystick * > mxvk::VK_Joystick::handle ( ) const
nodiscard

Return the underlying SDL_Joystick handle as an optional.

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

Definition at line 73 of file mxvk_controller.cpp.

73 {
74 if (stick != nullptr) {
75 return stick;
76 }
77 return std::nullopt;
78 }

◆ joystickIndex()

int mxvk::VK_Joystick::joystickIndex ( ) const

Return the device index this joystick was opened with.

Returns
Device index.

Definition at line 87 of file mxvk_controller.cpp.

87{ return deviceIndex; }

◆ joysticks()

int mxvk::VK_Joystick::joysticks ( )
static

Return the number of connected joysticks.

Returns
SDL_NumJoysticks() result.

Definition at line 18 of file mxvk_controller.cpp.

18 {
19 int count = 0;
20 SDL_JoystickID *ids = SDL_GetJoysticks(&count);
21 if (ids != nullptr) {
22 SDL_free(ids);
23 }
24 return count;
25 }

◆ name()

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

Return the joystick's name as reported by SDL.

Returns
Device name string.

Definition at line 53 of file mxvk_controller.cpp.

53 {
54 if (stick != nullptr) {
55 const char *device_name = SDL_GetJoystickName(stick);
56 if (device_name != nullptr) {
57 return device_name;
58 }
59 }
60 return "Joystick Not Opened.";
61 }

◆ numAxes()

int mxvk::VK_Joystick::numAxes ( ) const
nodiscard
Returns
Number of axes on this joystick.

Definition at line 126 of file mxvk_controller.cpp.

126 {
127 if (stick == nullptr) {
128 return 0;
129 }
130 const int count = SDL_GetNumJoystickAxes(stick);
131 return count < 0 ? 0 : count;
132 }

◆ numButtons()

int mxvk::VK_Joystick::numButtons ( ) const
nodiscard
Returns
Number of buttons on this joystick.

Definition at line 110 of file mxvk_controller.cpp.

110 {
111 if (stick == nullptr) {
112 return 0;
113 }
114 const int count = SDL_GetNumJoystickButtons(stick);
115 return count < 0 ? 0 : count;
116 }

◆ numHats()

int mxvk::VK_Joystick::numHats ( ) const
nodiscard
Returns
Number of hats on this joystick.

Definition at line 118 of file mxvk_controller.cpp.

118 {
119 if (stick == nullptr) {
120 return 0;
121 }
122 const int count = SDL_GetNumJoystickHats(stick);
123 return count < 0 ? 0 : count;
124 }

◆ open()

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

Open the joystick at the given device index.

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

Definition at line 27 of file mxvk_controller.cpp.

27 {
28 close();
29
30 int count = 0;
31 SDL_JoystickID *ids = SDL_GetJoysticks(&count);
32 if (ids == nullptr || index < 0 || index >= count) {
33 if (ids != nullptr) {
34 SDL_free(ids);
35 }
36 return false;
37 }
38
39 const SDL_JoystickID openedInstanceId = ids[index];
40 SDL_free(ids);
41
42 stick = SDL_OpenJoystick(openedInstanceId);
43 if (stick == nullptr) {
44 return false;
45 }
46
47 deviceIndex = index;
48 instanceId = openedInstanceId;
49 SDL_SetJoystickEventsEnabled(true);
50 return true;
51 }

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ unwrap()

SDL_Joystick * mxvk::VK_Joystick::unwrap ( ) const

Unwrap the SDL_Joystick pointer, asserting it is open.

Returns
Raw SDL_Joystick pointer.

Definition at line 80 of file mxvk_controller.cpp.

80 {
81 if (stick != nullptr) {
82 return stick;
83 }
84 throw mxvk::Exception("Invalid joystick handle");
85 }

Member Data Documentation

◆ deviceIndex

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

Open index in the current joystick list.

Definition at line 103 of file mxvk_controller.hpp.

◆ instanceId

SDL_JoystickID mxvk::VK_Joystick::instanceId = 0
protected

Stable SDL joystick instance identifier.

Definition at line 104 of file mxvk_controller.hpp.

◆ stick

SDL_Joystick* mxvk::VK_Joystick::stick = nullptr
protected

Underlying SDL joystick handle.

Definition at line 102 of file mxvk_controller.hpp.


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