6 #include <SDL_gamecontroller.h> 22 throw Exception(
"SDL_GameControllerOpen");
53 *(
const_cast<bool*
>(&
owned_)) = other.owned_;
54 *(
const_cast<bool*
>(&other.owned_)) =
false;
55 other.controller_ =
nullptr;
76 int16_t
get_axis(SDL_GameControllerAxis axis)
const 78 return SDL_GameControllerGetAxis(
controller_, axis);
84 return SDL_GameControllerGetButton(
controller_, button);
87 #if SDL_VERSION_ATLEAST(2, 0, 9) 88 int rumble(uint16_t low_freq, uint16_t high_freq, std::chrono::milliseconds duration)
const 91 return rumble(low_freq, high_freq, static_cast<uint32_t>(duration.count()));
95 int rumble(uint16_t low_freq, uint16_t high_freq, uint32_t millisec_duration)
const 97 return SDL_GameControllerRumble(
controller_, low_freq, high_freq, millisec_duration);
110 const char*
name = SDL_GameControllerNameForIndex(joystick_index);
111 if (!name)
throw Exception(
"SDL_GameControllerNameForIndex");
137 const auto state = SDL_GameControllerAddMappingsFromFile(file_path);
140 throw Exception(
"SDL_GameControllerAddMappingsFromFile");
155 const auto state = SDL_GameControllerAddMapping(mapping_string);
159 throw Exception(
"SDL_GameControllerAddMapping");
169 std::vector<GameController> controllers;
171 for (
int nb_sticks = SDL_NumJoysticks(), i = 0; i < nb_sticks; ++i)
173 if (SDL_IsGameController(i))
177 controllers.emplace_back(i);
193 return {SDL_GameControllerFromInstanceID(joystick_id),
false};
int8_t get_button(SDL_GameControllerButton button) const
Get the current imedate value of the given button.
static int load_mapping_database(std::string const &file_path)
Load a file database.
static std::string get_controller_name(int joystick_index)
GameController(int joystick_index)
Construct a controller from a joystick index, throws if that index is not a game controller.
GameController(GameController &&other) noexcept
move ctor
int16_t get_axis(SDL_GameControllerAxis axis) const
Get the current imediate value of the given axis.
Haptic open_haptic() const
Open the haptic device from the controller.
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...
SDL_GameController * controller_
static std::vector< GameController > open_all_available_controllers()
Try to open all available controllers, and return an array of all controller sucessfully openned...
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...
Define to deactivate exception support.
GameController & operator=(GameController const &)=delete
SDL_GameController * ptr() const
Get the SDL pointer.
static int add_mapping(std::string const &mapping_string)
Add a mapping string.
~GameController()
Close the opened controller pointer, unless this wrapper was created via GameController::non_owning()...
static int load_mapping_database(const char *file_path)
Load a file database.
GameController()=default
Construct an empty controller.
GameController(SDL_GameController *controller)
Construct a controller from a GameController pointer. This object will take ownership of the controll...
bool is_attached() const
Return true if this controller is attached.
GameController & operator=(GameController &&other) noexcept
move-assing operator
static int add_mapping(const char *mapping_string)
Add a mapping string.
GameController(SDL_GameController *controller, bool non_owned)
Private controller for a non-onwer controller. The bool argument is expected to be false here...
static GameController non_owning(SDL_GameController *controller)
Create a non_owning controller around an SDL controller pointer, to use the C++ aPI withiout managing...
Define to deactivate exception support.
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...
Represent a gamepad (game controller)