12 if(SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER))
14 printf(
"[error] init gamecontroller failed:\n%s\n", SDL_GetError());
19 SDL_GameControllerAddMappingsFromFile(
"gamecontrollerdb.txt");
21 int n= SDL_NumJoysticks();
22 for(
int i= 0; i < n; i++)
24 if(SDL_IsGameController(i))
26 SDL_GameController *
pad= SDL_GameControllerOpen(i);
29 printf(
"[error] opening pad: %s\n", SDL_GetError());
37 printf(
"found %d pads...\n", (
int) m_pads.size());
38 return (m_pads.size() > 0);
41 Gamepads::~Gamepads( )
44 SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
47 void Gamepads::release( )
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);
58 for(
size_t p= 0; p < m_pads.size(); p++)
60 if(SDL_GameControllerGetAttached(m_pads[p].m_pad) == SDL_FALSE)
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;
68 SDL_GameControllerClose(m_pads[p].m_pad);
69 m_pads[p].m_pad= NULL;
72 SDL_GameController *
pad= m_pads[p].m_pad;
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;
81 m_pads[p].m_buttons[i]= 0;
84 for(
int i= 0; i < SDL_CONTROLLER_AXIS_MAX; i++)
86 int value= SDL_GameControllerGetAxis(
pad, (SDL_GameControllerAxis) i);
91 m_pads[p].m_axis[i]= (float)
value / 32768.f;
99 return (
int) m_pads.size();
104 assert(index < m_pads.size());
105 return m_pads[index];
130 return (m_pad != NULL);
135 assert(b < SDL_CONTROLLER_BUTTON_MAX);
141 assert(b < SDL_CONTROLLER_BUTTON_MAX);
147 assert(a < SDL_CONTROLLER_AXIS_MAX);
153 assert(a < SDL_CONTROLLER_AXIS_MAX);
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.
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().
float axis(const SDL_GameControllerAxis a)
renvoie la position d'un axe.
int button(const SDL_GameControllerButton b)
renvoie l'etat d'un bouton.
void clear_axis(const SDL_GameControllerAxis a)
re-initialise la position d'un axe.
void clear_button(const SDL_GameControllerButton b)
desactive un button.
bool connected()
renvoie l'etat d'un gamepad. debranche ou pas.
bool create()
detection des pads connectes.
void update()
lecture des infos des pads connectes.
Gamepads()
constructeur par defaut.
void clear_button(const unsigned int index, const SDL_GameControllerButton b)
desactive un button d'un controlleur.
float axis(const unsigned int index, const SDL_GameControllerAxis a)
renvoie la position d'un axe d'un controlleur.
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.
void clear_axis(const unsigned int index, const SDL_GameControllerAxis a)
re-initialise la position d'un axe d'un controlleur.
int pads()
renvoie le nombre de game controllers.
Gamepad & pad(const unsigned int index)
renvoie un game controller.