5unsigned int Joystick::id_max;
6std::list<unsigned int> Joystick::ids;
9 joystick = SDL_JoystickOpen(device_id);
11 std::string error = std::string(
"Error opening a new joystick: ") +
14 throw std::runtime_error(error);
17 name = std::string(SDL_JoystickName(joystick));
19 num_axes = SDL_JoystickNumAxes(joystick);
20 num_hats = SDL_JoystickNumHats(joystick);
27 for(i = 0; i <= id_max + 1; i++) {
28 if(std::find(std::begin(ids), std::end(ids), i) == std::end(ids)) {
38Joystick::~Joystick() {
39 SDL_JoystickClose(joystick);
40 const auto& pos = std::find(std::begin(ids), std::end(ids),
id);
41 if(pos != std::end(ids)) {
47 std::vector<int>::iterator it;
49 it = std::find(switches.begin(), switches.end(), button);
50 if(it == switches.end())
62 std::vector<int>::iterator it = std::find(switches.begin(),
66 if(it == switches.end()) {
68 switches.push_back(button);
80 int value = SDL_JoystickGetAxis(joystick, (SDL_GameControllerAxis)axis);
81 if((
unsigned int)std::abs(value) >
deadzone)
87 return SDL_JoystickGetHat(joystick, hat);
static std::vector< std::string > error_string
A list of all error strings.
std::vector< int > buttons_pressed
A list of all buttons currently pressed.
unsigned int instance_id
The instance ID of the gamepad as provided by an SDL2 event.
int get_axis_value(unsigned int axis)
Call this function to get the current value of an axis.
unsigned int num_axes
The number of axes the joystick has.
unsigned int get_hat_value(unsigned int hat)
Call this function to get the current value of a hat.
std::string name
The name of the joystick.
void set_button_state(int button, bool state)
Sets the state of a button.
unsigned int deadzone
The axis deadzone.
unsigned int num_hats
The number of hats the joystick has.
void set_deadzone(unsigned int deadzone)
Sets a dedzone for all axes.
void mark_switch(int button, bool mark=true)
Marks or unmarks a button as a switch. Switches will not be added to the buttons_pressed list.
Joystick(unsigned int device_id)
unsigned int num_buttons
The number of buttons the joystick has.