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

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

#include <joystick.hpp>

Inheritance diagram for mx::Controller:
[legend]

Public Member Functions

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

Static Public Member Functions

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

Protected Attributes

SDL_GameController * stick = nullptr
 Underlying SDL game controller handle.
int index = -1
 Device index (-1 if not open).

Detailed Description

RAII wrapper for SDL_GameController (standard layout mapping).

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

Definition at line 127 of file joystick.hpp.

Constructor & Destructor Documentation

◆ Controller() [1/3]

mx::Controller::Controller ( )

Default constructor — device not yet opened.

Definition at line 101 of file joystick.cpp.

References index, and stick.

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

◆ ~Controller()

mx::Controller::~Controller ( )

Destructor — closes the controller if open.

Definition at line 103 of file joystick.cpp.

References close().

◆ Controller() [2/3]

mx::Controller::Controller ( const Controller & )
delete

References Controller().

◆ Controller() [3/3]

mx::Controller::Controller ( Controller && )
delete

References Controller().

Member Function Documentation

◆ active()

bool mx::Controller::active ( ) const
inline

Check whether the controller is currently usable.

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

Definition at line 221 of file joystick.hpp.

References index, and stick.

Referenced by mx::Input::getButton().

◆ close()

void mx::Controller::close ( )

Close the controller handle if open.

Definition at line 126 of file joystick.cpp.

References index, and stick.

Referenced by connectEvent(), and ~Controller().

◆ connectEvent()

bool mx::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 176 of file joystick.cpp.

References close(), name(), open(), and mx::system_out.

◆ controllerIndex()

int mx::Controller::controllerIndex ( ) const

Return the device index this controller was opened with.

Returns
Device index.

Definition at line 149 of file joystick.cpp.

References index.

◆ getAxis()

Sint16 mx::Controller::getAxis ( SDL_GameControllerAxis axis)

Read the current axis value.

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

Definition at line 169 of file joystick.cpp.

References stick.

◆ getButton()

bool mx::Controller::getButton ( SDL_GameControllerButton button)

Test whether a mapped button is currently pressed.

Parameters
buttonSDL_GameControllerButton constant.
Returns
true if pressed.

Definition at line 153 of file joystick.cpp.

References stick.

Referenced by mx::Input::getButton().

◆ getHat()

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

Read the position of a POV hat.

Parameters
hatHat index.
Returns
SDL hat position bitmask.

Definition at line 159 of file joystick.cpp.

References stick.

◆ handle()

std::optional< SDL_GameController * > mx::Controller::handle ( )

Return the underlying SDL_GameController handle as an optional.

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

Definition at line 134 of file joystick.cpp.

References stick.

◆ joysticks()

int mx::Controller::joysticks ( )
inlinestatic

Return the number of connected joysticks/controllers.

Returns
SDL_NumJoysticks() result.

Definition at line 184 of file joystick.hpp.

◆ name()

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

Return the controller's name as reported by SDL.

Returns
Device name string.

Definition at line 119 of file joystick.cpp.

References stick.

Referenced by connectEvent().

◆ open()

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

Open a game controller by device index.

Parameters
indexSDL joystick index of the controller.
Returns
true on success.

Definition at line 107 of file joystick.cpp.

References index, and stick.

Referenced by connectEvent().

◆ operator=() [1/2]

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

References Controller().

◆ operator=() [2/2]

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

References Controller(), and index.

◆ unwrap()

SDL_GameController * mx::Controller::unwrap ( ) const

Unwrap the SDL_GameController pointer, asserting it is open.

Returns
Raw SDL_GameController pointer.

Definition at line 141 of file joystick.cpp.

References stick.

◆ wrapper()

mx::Wrapper< SDL_GameController * > mx::Controller::wrapper ( ) const
inline

Return a Wrapper around the SDL_GameController handle.

Returns
Wrapper holding the pointer or nullopt.

Definition at line 211 of file joystick.hpp.

References stick.

Member Data Documentation

◆ index

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

Device index (-1 if not open).

Definition at line 224 of file joystick.hpp.

Referenced by active(), close(), Controller(), controllerIndex(), open(), and operator=().

◆ stick

SDL_GameController* mx::Controller::stick = nullptr
protected

Underlying SDL game controller handle.

Definition at line 223 of file joystick.hpp.

Referenced by active(), close(), Controller(), getAxis(), getButton(), getHat(), handle(), name(), open(), unwrap(), and wrapper().


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