sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
joystick.cpp
1#include "joystick.h"
2
3using namespace sgltk;
4
5unsigned int Joystick::id_max;
6std::list<unsigned int> Joystick::ids;
7
8Joystick::Joystick(unsigned int device_id) {
9 joystick = SDL_JoystickOpen(device_id);
10 if(!joystick) {
11 std::string error = std::string("Error opening a new joystick: ") +
12 SDL_GetError();
13 App::error_string.push_back(error);
14 throw std::runtime_error(error);
15 }
16
17 name = std::string(SDL_JoystickName(joystick));
18
19 num_axes = SDL_JoystickNumAxes(joystick);
20 num_hats = SDL_JoystickNumHats(joystick);
21 num_buttons = SDL_JoystickNumButtons(joystick);
22 instance_id = SDL_JoystickInstanceID(joystick);
23
24 deadzone = 0;
25
26 unsigned int i;
27 for(i = 0; i <= id_max + 1; i++) {
28 if(std::find(std::begin(ids), std::end(ids), i) == std::end(ids)) {
29 break;
30 }
31 }
32 id = i;
33 ids.push_back(id);
34 if(i > id_max)
35 id_max = i;
36}
37
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)) {
42 ids.erase(pos);
43 }
44}
45
46void Joystick::set_button_state(int button, bool state) {
47 std::vector<int>::iterator it;
48 if(state) {
49 it = std::find(switches.begin(), switches.end(), button);
50 if(it == switches.end())
51 buttons_pressed.push_back(button);
52 }
53 else {
54 it = std::find(buttons_pressed.begin(),
55 buttons_pressed.end(), button);
56 if(it != buttons_pressed.end())
57 buttons_pressed.erase(it);
58 }
59}
60
61void Joystick::mark_switch(int button, bool mark) {
62 std::vector<int>::iterator it = std::find(switches.begin(),
63 switches.end(),
64 button);
65
66 if(it == switches.end()) {
67 if(mark)
68 switches.push_back(button);
69 } else {
70 if(!mark)
71 switches.erase(it);
72 }
73}
74
75void Joystick::set_deadzone(unsigned int deadzone) {
76 this->deadzone = deadzone;
77}
78
79int Joystick::get_axis_value(unsigned int axis) {
80 int value = SDL_JoystickGetAxis(joystick, (SDL_GameControllerAxis)axis);
81 if((unsigned int)std::abs(value) > deadzone)
82 return value;
83 return 0;
84}
85
86unsigned int Joystick::get_hat_value(unsigned int hat) {
87 return SDL_JoystickGetHat(joystick, hat);
88}
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
std::vector< int > buttons_pressed
A list of all buttons currently pressed.
Definition joystick.h:55
unsigned int instance_id
The instance ID of the gamepad as provided by an SDL2 event.
Definition joystick.h:31
int get_axis_value(unsigned int axis)
Call this function to get the current value of an axis.
Definition joystick.cpp:79
unsigned int num_axes
The number of axes the joystick has.
Definition joystick.h:39
unsigned int get_hat_value(unsigned int hat)
Call this function to get the current value of a hat.
Definition joystick.cpp:86
std::string name
The name of the joystick.
Definition joystick.h:35
void set_button_state(int button, bool state)
Sets the state of a button.
Definition joystick.cpp:46
unsigned int deadzone
The axis deadzone.
Definition joystick.h:51
unsigned int num_hats
The number of hats the joystick has.
Definition joystick.h:47
void set_deadzone(unsigned int deadzone)
Sets a dedzone for all axes.
Definition joystick.cpp:75
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.
Definition joystick.cpp:61
Joystick(unsigned int device_id)
Definition joystick.cpp:8
unsigned int num_buttons
The number of buttons the joystick has.
Definition joystick.h:43