cpp-sdl2
C++ header-only SDL2 wrapper
sdl::GameController Class Reference

Represent a gamepad (game controller) More...

#include <game_controller.hpp>

Public Member Functions

 GameController (int joystick_index)
 Construct a controller from a joystick index, throws if that index is not a game controller. More...
 
 GameController (SDL_GameController *controller)
 Construct a controller from a GameController pointer. This object will take ownership of the controller pointed. More...
 
 GameController ()=default
 Construct an empty controller. More...
 
 GameController (GameController const &)=delete
 Not copyable. More...
 
GameControlleroperator= (GameController const &)=delete
 
 GameController (GameController &&other) noexcept
 move ctor More...
 
GameControlleroperator= (GameController &&other) noexcept
 move-assing operator More...
 
 ~GameController ()
 Close the opened controller pointer, unless this wrapper was created via GameController::non_owning() More...
 
SDL_GameController * ptr () const
 Get the SDL pointer. More...
 
Haptic open_haptic () const
 Open the haptic device from the controller. More...
 
bool is_attached () const
 Return true if this controller is attached. More...
 
int16_t get_axis (SDL_GameControllerAxis axis) const
 Get the current imediate value of the given axis. More...
 
int8_t get_button (SDL_GameControllerButton button) const
 Get the current imedate value of the given button. More...
 
int rumble (uint16_t low_freq, uint16_t high_freq, std::chrono::milliseconds duration) const
 Play a simple rumble. If the controller has 2 motors, the two values will control one of them. If the controller only has one, the values will be mixed together. More...
 
int rumble (uint16_t low_freq, uint16_t high_freq, uint32_t millisec_duration) const
 Play a simple rumble. If the controller has 2 motors, the two values will control one of them. If the controller only has one, the values will be mixed together. More...
 
std::string name () const
 

Static Public Member Functions

static std::string get_controller_name (int joystick_index)
 
static int load_mapping_database (std::string const &file_path)
 Load a file database. More...
 
static int load_mapping_database (const char *file_path)
 Load a file database. More...
 
static int add_mapping (std::string const &mapping_string)
 Add a mapping string. More...
 
static int add_mapping (const char *mapping_string)
 Add a mapping string. More...
 
static std::vector< GameControlleropen_all_available_controllers ()
 Try to open all available controllers, and return an array of all controller sucessfully openned. More...
 
static GameController non_owning (SDL_JoystickID joystick_id)
 Create a non_owning controller around a stick ID, to use the C++ API without managing the controller. More...
 
static GameController non_owning (SDL_GameController *controller)
 Create a non_owning controller around an SDL controller pointer, to use the C++ aPI withiout managing the controller. More...
 

Private Member Functions

 GameController (SDL_GameController *controller, bool non_owned)
 Private controller for a non-onwer controller. The bool argument is expected to be false here. More...
 

Private Attributes

SDL_GameController * controller_ = nullptr
 
bool const owned_ = true
 

Detailed Description

Represent a gamepad (game controller)

Definition at line 14 of file game_controller.hpp.

Constructor & Destructor Documentation

◆ GameController() [1/6]

sdl::GameController::GameController ( int  joystick_index)
inline

Construct a controller from a joystick index, throws if that index is not a game controller.

Definition at line 18 of file game_controller.hpp.

References controller_.

◆ GameController() [2/6]

sdl::GameController::GameController ( SDL_GameController *  controller)
inline

Construct a controller from a GameController pointer. This object will take ownership of the controller pointed.

Definition at line 27 of file game_controller.hpp.

References GameController(), and operator=().

◆ GameController() [3/6]

sdl::GameController::GameController ( )
default

Construct an empty controller.

Referenced by GameController().

◆ GameController() [4/6]

sdl::GameController::GameController ( GameController const &  )
delete

Not copyable.

◆ GameController() [5/6]

sdl::GameController::GameController ( GameController &&  other)
inlinenoexcept

move ctor

Definition at line 37 of file game_controller.hpp.

◆ ~GameController()

sdl::GameController::~GameController ( )
inline

Close the opened controller pointer, unless this wrapper was created via GameController::non_owning()

Definition at line 61 of file game_controller.hpp.

References controller_, and owned_.

◆ GameController() [6/6]

sdl::GameController::GameController ( SDL_GameController *  controller,
bool  non_owned 
)
inlineprivate

Private controller for a non-onwer controller. The bool argument is expected to be false here.

Definition at line 201 of file game_controller.hpp.

References owned_.

Member Function Documentation

◆ add_mapping() [1/2]

static int sdl::GameController::add_mapping ( std::string const &  mapping_string)
inlinestatic

Add a mapping string.

Definition at line 147 of file game_controller.hpp.

◆ add_mapping() [2/2]

static int sdl::GameController::add_mapping ( const char *  mapping_string)
inlinestatic

Add a mapping string.

Definition at line 153 of file game_controller.hpp.

◆ get_axis()

int16_t sdl::GameController::get_axis ( SDL_GameControllerAxis  axis) const
inline

Get the current imediate value of the given axis.

Definition at line 76 of file game_controller.hpp.

References controller_.

◆ get_button()

int8_t sdl::GameController::get_button ( SDL_GameControllerButton  button) const
inline

Get the current imedate value of the given button.

Definition at line 82 of file game_controller.hpp.

References controller_, and rumble().

◆ get_controller_name()

static std::string sdl::GameController::get_controller_name ( int  joystick_index)
inlinestatic

Definition at line 108 of file game_controller.hpp.

References name().

◆ is_attached()

bool sdl::GameController::is_attached ( ) const
inline

Return true if this controller is attached.

Definition at line 73 of file game_controller.hpp.

References controller_.

◆ load_mapping_database() [1/2]

static int sdl::GameController::load_mapping_database ( std::string const &  file_path)
inlinestatic

Load a file database.

Definition at line 129 of file game_controller.hpp.

◆ load_mapping_database() [2/2]

static int sdl::GameController::load_mapping_database ( const char *  file_path)
inlinestatic

Load a file database.

Definition at line 135 of file game_controller.hpp.

◆ name()

std::string sdl::GameController::name ( ) const
inline

Definition at line 101 of file game_controller.hpp.

References controller_.

Referenced by get_controller_name().

◆ non_owning() [1/2]

static GameController sdl::GameController::non_owning ( SDL_JoystickID  joystick_id)
inlinestatic

Create a non_owning controller around a stick ID, to use the C++ API without managing the controller.

Definition at line 191 of file game_controller.hpp.

◆ non_owning() [2/2]

static GameController sdl::GameController::non_owning ( SDL_GameController *  controller)
inlinestatic

Create a non_owning controller around an SDL controller pointer, to use the C++ aPI withiout managing the controller.

Definition at line 197 of file game_controller.hpp.

◆ open_all_available_controllers()

static std::vector<GameController> sdl::GameController::open_all_available_controllers ( )
inlinestatic

Try to open all available controllers, and return an array of all controller sucessfully openned.

Definition at line 167 of file game_controller.hpp.

◆ open_haptic()

Haptic sdl::GameController::open_haptic ( ) const
inline

Open the haptic device from the controller.

Definition at line 70 of file game_controller.hpp.

References controller_.

◆ operator=() [1/2]

GameController& sdl::GameController::operator= ( GameController const &  )
delete

Referenced by GameController().

◆ operator=() [2/2]

GameController& sdl::GameController::operator= ( GameController &&  other)
inlinenoexcept

move-assing operator

Definition at line 40 of file game_controller.hpp.

References controller_, and owned_.

◆ ptr()

SDL_GameController* sdl::GameController::ptr ( ) const
inline

Get the SDL pointer.

Definition at line 67 of file game_controller.hpp.

References controller_.

◆ rumble() [1/2]

int sdl::GameController::rumble ( uint16_t  low_freq,
uint16_t  high_freq,
std::chrono::milliseconds  duration 
) const
inline

Play a simple rumble. If the controller has 2 motors, the two values will control one of them. If the controller only has one, the values will be mixed together.

Definition at line 89 of file game_controller.hpp.

Referenced by get_button().

◆ rumble() [2/2]

int sdl::GameController::rumble ( uint16_t  low_freq,
uint16_t  high_freq,
uint32_t  millisec_duration 
) const
inline

Play a simple rumble. If the controller has 2 motors, the two values will control one of them. If the controller only has one, the values will be mixed together.

Definition at line 95 of file game_controller.hpp.

References controller_.

Member Data Documentation

◆ controller_

SDL_GameController* sdl::GameController::controller_ = nullptr
private

◆ owned_

bool const sdl::GameController::owned_ = true
private

Definition at line 208 of file game_controller.hpp.

Referenced by GameController(), operator=(), and ~GameController().


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