libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::Joystick Class Reference

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

#include <joystick.hpp>

Public Member Functions

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

Static Public Member Functions

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

Protected Attributes

SDL_Joystick * stick = nullptr
 Underlying SDL joystick handle.
int index = -1
 Device index (-1 if not open).

Detailed Description

RAII wrapper for a raw SDL_Joystick device.

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

Definition at line 26 of file joystick.hpp.

Constructor & Destructor Documentation

◆ Joystick() [1/3]

mx::Joystick::Joystick ( )

Default constructor — device not yet opened.

Definition at line 11 of file joystick.cpp.

References index, and stick.

Referenced by Joystick(), Joystick(), operator=(), and operator=().

◆ ~Joystick()

mx::Joystick::~Joystick ( )

Destructor — closes the joystick if open.

Definition at line 13 of file joystick.cpp.

References close().

◆ Joystick() [2/3]

mx::Joystick::Joystick ( const Joystick & )
delete

References Joystick().

◆ Joystick() [3/3]

mx::Joystick::Joystick ( Joystick && )
delete

References Joystick().

Member Function Documentation

◆ close()

void mx::Joystick::close ( )

Close the joystick handle if open.

Definition at line 36 of file joystick.cpp.

References index, and stick.

Referenced by ~Joystick().

◆ getAxis()

Sint16 mx::Joystick::getAxis ( int axis)

Read the current axis value.

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

Definition at line 77 of file joystick.cpp.

References stick.

◆ getButton()

bool mx::Joystick::getButton ( int button)

Test whether a button is currently pressed.

Parameters
buttonButton index.
Returns
true if pressed.

Definition at line 63 of file joystick.cpp.

References stick.

◆ getHat()

Uint8 mx::Joystick::getHat ( int hat)

Read the position of a POV hat.

Parameters
hatHat index.
Returns
SDL hat position bitmask.

Definition at line 70 of file joystick.cpp.

References stick.

◆ handle()

std::optional< SDL_Joystick * > mx::Joystick::handle ( )

Return the underlying SDL_Joystick handle as an optional.

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

Definition at line 44 of file joystick.cpp.

References stick.

◆ joystickIndex()

int mx::Joystick::joystickIndex ( ) const

Return the device index this joystick was opened with.

Returns
Device index.

Definition at line 59 of file joystick.cpp.

References index.

◆ joysticks()

int mx::Joystick::joysticks ( )
inlinestatic

Return the number of connected joysticks.

Returns
SDL_NumJoysticks() result.

Definition at line 76 of file joystick.hpp.

◆ name()

std::string mx::Joystick::name ( ) const

Return the joystick's name as reported by SDL.

Returns
Device name string.

Definition at line 29 of file joystick.cpp.

References stick.

◆ numAxes()

int mx::Joystick::numAxes ( )
Returns
Number of axes on this joystick.

Definition at line 95 of file joystick.cpp.

References stick.

◆ numButtons()

int mx::Joystick::numButtons ( )
Returns
Number of buttons on this joystick.

Definition at line 84 of file joystick.cpp.

References stick.

◆ numHats()

int mx::Joystick::numHats ( )
Returns
Number of hats on this joystick.

Definition at line 90 of file joystick.cpp.

References stick.

◆ open()

bool mx::Joystick::open ( int index)

Open the joystick at the given device index.

Parameters
indexSDL joystick index (0-based).
Returns
true on success.

Definition at line 17 of file joystick.cpp.

References index, and stick.

◆ operator=() [1/2]

Joystick & mx::Joystick::operator= ( const Joystick & )
delete

References Joystick().

◆ operator=() [2/2]

Joystick & mx::Joystick::operator= ( Joystick && )
delete

References index, and Joystick().

◆ unwrap()

SDL_Joystick * mx::Joystick::unwrap ( ) const

Unwrap the SDL_Joystick pointer, asserting it is open.

Returns
Raw SDL_Joystick pointer.

Definition at line 51 of file joystick.cpp.

References stick.

◆ wrapper()

mx::Wrapper< SDL_Joystick * > mx::Joystick::wrapper ( ) const
inline

Return a Wrapper around the SDL_Joystick handle.

Returns
Wrapper holding the pointer or nullopt.

Definition at line 110 of file joystick.hpp.

References stick.

Member Data Documentation

◆ index

int mx::Joystick::index = -1
protected

Device index (-1 if not open).

Definition at line 117 of file joystick.hpp.

Referenced by close(), Joystick(), joystickIndex(), open(), and operator=().

◆ stick

SDL_Joystick* mx::Joystick::stick = nullptr
protected

Underlying SDL joystick handle.

Definition at line 116 of file joystick.hpp.

Referenced by close(), getAxis(), getButton(), getHat(), handle(), Joystick(), name(), numAxes(), numButtons(), numHats(), open(), unwrap(), and wrapper().


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