sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
gamepad.cpp
1#include "gamepad.h"
2
3using namespace sgltk;
4
5unsigned int Gamepad::id_max;
6std::list<unsigned int> Gamepad::ids;
7
8Gamepad::Gamepad(unsigned int device_id) {
9 gamepad = SDL_GameControllerOpen(device_id);
10 if(!gamepad) {
11 std::string error = std::string("Error opening a new gamepad: ") +
12 SDL_GetError();
13 App::error_string.push_back(error);
14 throw std::runtime_error(error);
15 }
16
17 joystick = SDL_GameControllerGetJoystick(gamepad);
18 instance_id = SDL_JoystickInstanceID(joystick);
19
20 num_axes = SDL_JoystickNumAxes(joystick);
21 num_buttons = SDL_JoystickNumButtons(joystick);
22
23 deadzone = 0;
24
25 if(SDL_JoystickIsHaptic(joystick)) {
26 haptic = SDL_HapticOpenFromJoystick(joystick);
27 if(!haptic) {
28 std::string error = std::string("Error opening a new haptic device: ") +
29 SDL_GetError();
30 App::error_string.push_back(error);
31 throw std::runtime_error(error);
32 }
33 if(SDL_HapticRumbleSupported(haptic) == SDL_TRUE)
34 SDL_HapticRumbleInit(haptic);
35 } else {
36 haptic = nullptr;
37 }
38
39 unsigned int i;
40 for(i = 0; i <= id_max + 1; i++) {
41 if(std::find(std::begin(ids), std::end(ids), i) == std::end(ids)) {
42 break;
43 }
44 }
45 id = i;
46 ids.push_back(id);
47 if(i > id_max)
48 id_max = i;
49}
50
51Gamepad::~Gamepad() {
52 if(haptic) {
53 SDL_HapticClose(haptic);
54 }
55 SDL_GameControllerClose(gamepad);
56 const auto& pos = std::find(std::begin(ids), std::end(ids), id);
57 ids.erase(pos);
58}
59
60void Gamepad::set_button_state(int button, bool state) {
61 if(state) {
62 buttons_pressed.push_back(button);
63 } else {
64 std::vector<int>::iterator pressed_button = std::find(buttons_pressed.begin(),
65 buttons_pressed.end(), button);
66 if(pressed_button != buttons_pressed.end()) {
67 buttons_pressed.erase(pressed_button);
68 }
69 }
70}
71
72void Gamepad::set_deadzone(unsigned int deadzone) {
73 this->deadzone = deadzone;
74}
75
76int Gamepad::get_axis_value(unsigned int axis) {
77 int value = SDL_GameControllerGetAxis(gamepad, (SDL_GameControllerAxis)axis);
78 if((unsigned int)std::abs(value) > deadzone)
79 return value;
80 return 0;
81}
82
83void Gamepad::play_rumble(float magnitude, unsigned int duration) {
84 if(haptic) {
85 SDL_HapticRumblePlay(haptic, magnitude, duration);
86 }
87}
88
90 if(haptic) {
91 SDL_HapticRumbleStop(haptic);
92 }
93}
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
unsigned int num_axes
The number of axes the gamepad has.
Definition gamepad.h:36
std::vector< int > buttons_pressed
A list of all buttons currently pressed.
Definition gamepad.h:48
void set_button_state(int button, bool state)
Sets the state of a button.
Definition gamepad.cpp:60
void set_deadzone(unsigned int deadzone)
Sets a dedzone for all axes.
Definition gamepad.cpp:72
int get_axis_value(unsigned int axis)
Call this function to get the current value of an axis.
Definition gamepad.cpp:76
unsigned int deadzone
The axis deadzone.
Definition gamepad.h:44
unsigned int num_buttons
The number of buttons the gamepad has.
Definition gamepad.h:40
Gamepad(unsigned int device_id)
Definition gamepad.cpp:8
unsigned int instance_id
The instance ID of the gamepad as provided by an SDL2 event.
Definition gamepad.h:32
void stop_rumble()
Stops the rumble effect.
Definition gamepad.cpp:89
void play_rumble(float magnitude, unsigned int duration)
Plays a rumble effect.
Definition gamepad.cpp:83