5unsigned int Gamepad::id_max;
6std::list<unsigned int> Gamepad::ids;
9 gamepad = SDL_GameControllerOpen(device_id);
11 std::string error = std::string(
"Error opening a new gamepad: ") +
14 throw std::runtime_error(error);
17 joystick = SDL_GameControllerGetJoystick(gamepad);
20 num_axes = SDL_JoystickNumAxes(joystick);
25 if(SDL_JoystickIsHaptic(joystick)) {
26 haptic = SDL_HapticOpenFromJoystick(joystick);
28 std::string error = std::string(
"Error opening a new haptic device: ") +
31 throw std::runtime_error(error);
33 if(SDL_HapticRumbleSupported(haptic) == SDL_TRUE)
34 SDL_HapticRumbleInit(haptic);
40 for(i = 0; i <= id_max + 1; i++) {
41 if(std::find(std::begin(ids), std::end(ids), i) == std::end(ids)) {
53 SDL_HapticClose(haptic);
55 SDL_GameControllerClose(gamepad);
56 const auto& pos = std::find(std::begin(ids), std::end(ids),
id);
64 std::vector<int>::iterator pressed_button = std::find(
buttons_pressed.begin(),
77 int value = SDL_GameControllerGetAxis(gamepad, (SDL_GameControllerAxis)axis);
78 if((
unsigned int)std::abs(value) >
deadzone)
85 SDL_HapticRumblePlay(haptic, magnitude, duration);
91 SDL_HapticRumbleStop(haptic);
static std::vector< std::string > error_string
A list of all error strings.
unsigned int num_axes
The number of axes the gamepad has.
std::vector< int > buttons_pressed
A list of all buttons currently pressed.
void set_button_state(int button, bool state)
Sets the state of a button.
void set_deadzone(unsigned int deadzone)
Sets a dedzone for all axes.
int get_axis_value(unsigned int axis)
Call this function to get the current value of an axis.
unsigned int deadzone
The axis deadzone.
unsigned int num_buttons
The number of buttons the gamepad has.
Gamepad(unsigned int device_id)
unsigned int instance_id
The instance ID of the gamepad as provided by an SDL2 event.
void stop_rumble()
Stops the rumble effect.
void play_rumble(float magnitude, unsigned int duration)
Plays a rumble effect.