gKit2 light
gamepads.cpp
1 
2 #include <cassert>
3 #include <vector>
4 #include <stdio.h>
5 
6 #include "gamepads.h"
7 
9 
11 {
12  if(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER))
13  {
14  printf("[error] init gamecontroller failed:\n%s\n", SDL_GetError());
15  return false;
16  }
17 
18  // mapping cf https://github.com/gabomdq/SDL_GameControllerDB
19  SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
20 
21  int n= SDL_NumJoysticks();
22  for(int i= 0; i < n; i++)
23  {
24  if(SDL_IsGameController(i))
25  {
26  SDL_GameController *pad= SDL_GameControllerOpen(i);
27  if(pad == nullptr)
28  {
29  printf("[error] opening pad: %s\n", SDL_GetError());
30  continue;
31  }
32 
33  m_pads.push_back(Gamepad(pad));
34  }
35  }
36 
37  printf("found %d pads...\n", (int) m_pads.size());
38  return (m_pads.size() > 0);
39 }
40 
41 Gamepads::~Gamepads( )
42 {
43  release();
44  SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
45 }
46 
47 void Gamepads::release( )
48 {
49  for(size_t i= 0; i < m_pads.size(); i++)
50  if(m_pads[i].m_pad != NULL)
51  SDL_GameControllerClose(m_pads[i].m_pad);
52 
53  m_pads.clear();
54 }
55 
57 {
58  for(size_t p= 0; p < m_pads.size(); p++)
59  {
60  if(SDL_GameControllerGetAttached(m_pads[p].m_pad) == SDL_FALSE)
61  {
62  // le pad est debranche...
63  for(int i= 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
64  m_pads[p].m_buttons[i]= 0;
65  for(int i= 0; i < SDL_CONTROLLER_AXIS_MAX; i++)
66  m_pads[p].m_axis[i]= 0;
67 
68  SDL_GameControllerClose(m_pads[p].m_pad);
69  m_pads[p].m_pad= NULL;
70  }
71 
72  SDL_GameController *pad= m_pads[p].m_pad;
73  if(pad == NULL)
74  continue;
75 
76  // boutons
77  for(int i= 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
78  if(SDL_GameControllerGetButton(pad, (SDL_GameControllerButton) i) == SDL_PRESSED)
79  m_pads[p].m_buttons[i]= 1;
80  else
81  m_pads[p].m_buttons[i]= 0;
82 
83  // axes
84  for(int i= 0; i < SDL_CONTROLLER_AXIS_MAX; i++)
85  {
86  int value= SDL_GameControllerGetAxis(pad, (SDL_GameControllerAxis) i);
87  if(value > -6000 && value < 6000)
88  // dead zone...
89  value= 0;
90 
91  m_pads[p].m_axis[i]= (float) value / 32768.f;
92  }
93  }
94 }
95 
96 
98 {
99  return (int) m_pads.size();
100 }
101 
102 Gamepad& Gamepads::pad( const unsigned int index )
103 {
104  assert(index < m_pads.size());
105  return m_pads[index];
106 }
107 
108 int Gamepads::button( const unsigned int index, const SDL_GameControllerButton b )
109 {
110  return pad(index).button(b);
111 }
112 
113 void Gamepads::clear_button( const unsigned int index, const SDL_GameControllerButton b )
114 {
115  return pad(index).clear_button(b);
116 }
117 
118 float Gamepads::axis( const unsigned int index, const SDL_GameControllerAxis a )
119 {
120  return pad(index).axis(a);
121 }
122 
123 void Gamepads::clear_axis( const unsigned int index, const SDL_GameControllerAxis a )
124 {
125  return pad(index).clear_axis(a);
126 }
127 
129 {
130  return (m_pad != NULL);
131 }
132 
133 int Gamepad::button( const SDL_GameControllerButton b )
134 {
135  assert(b < SDL_CONTROLLER_BUTTON_MAX);
136  return m_buttons[b];
137 }
138 
139 void Gamepad::clear_button( const SDL_GameControllerButton b )
140 {
141  assert(b < SDL_CONTROLLER_BUTTON_MAX);
142  m_buttons[b]= 0;
143 }
144 
145 float Gamepad::axis( const SDL_GameControllerAxis a )
146 {
147  assert(a < SDL_CONTROLLER_AXIS_MAX);
148  return m_axis[a];
149 }
150 
151 void Gamepad::clear_axis( const SDL_GameControllerAxis a )
152 {
153  assert(a < SDL_CONTROLLER_AXIS_MAX);
154  m_axis[a]= 0;
155 }
156 
bool value(Widgets &w, const char *label, int &value, const int value_min, const int value_max, const int value_step)
valeur editable par increment.
Definition: widgets.cpp:191
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
float axis(const SDL_GameControllerAxis a)
renvoie la position d'un axe.
Definition: gamepads.cpp:145
int button(const SDL_GameControllerButton b)
renvoie l'etat d'un bouton.
Definition: gamepads.cpp:133
void clear_axis(const SDL_GameControllerAxis a)
re-initialise la position d'un axe.
Definition: gamepads.cpp:151
void clear_button(const SDL_GameControllerButton b)
desactive un button.
Definition: gamepads.cpp:139
bool connected()
renvoie l'etat d'un gamepad. debranche ou pas.
Definition: gamepads.cpp:128
bool create()
detection des pads connectes.
Definition: gamepads.cpp:10
void update()
lecture des infos des pads connectes.
Definition: gamepads.cpp:56
Gamepads()
constructeur par defaut.
Definition: gamepads.cpp:8
void clear_button(const unsigned int index, const SDL_GameControllerButton b)
desactive un button d'un controlleur.
Definition: gamepads.cpp:113
float axis(const unsigned int index, const SDL_GameControllerAxis a)
renvoie la position d'un axe d'un controlleur.
Definition: gamepads.cpp:118
int button(const unsigned int index, const SDL_GameControllerButton b)
renvoie l'etat d'un button d'un controlleur. cf la doc SDL2 pour les codes.
Definition: gamepads.cpp:108
void clear_axis(const unsigned int index, const SDL_GameControllerAxis a)
re-initialise la position d'un axe d'un controlleur.
Definition: gamepads.cpp:123
int pads()
renvoie le nombre de game controllers.
Definition: gamepads.cpp:97
Gamepad & pad(const unsigned int index)
renvoie un game controller.
Definition: gamepads.cpp:102