gKit2 light
Loading...
Searching...
No Matches
Gamepads Struct Reference

Public Member Functions

 Gamepads ()
 constructeur par defaut.
bool create ()
 detection des pads connectes.
void release ()
void update ()
 lecture des infos des pads connectes.
int pads ()
 renvoie le nombre de game controllers.
Gamepadpad (const unsigned int index)
 renvoie un game controller.
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_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.
void clear_axis (const unsigned int index, const SDL_GameControllerAxis a)
 re-initialise la position d'un axe d'un controlleur.

Protected Attributes

std::vector< Gamepadm_pads

Detailed Description

Definition at line 34 of file gamepads.h.

Constructor & Destructor Documentation

◆ Gamepads()

Gamepads::Gamepads ( )

constructeur par defaut.

Definition at line 8 of file gamepads.cpp.

8{}

◆ ~Gamepads()

Gamepads::~Gamepads ( )

Definition at line 41 of file gamepads.cpp.

42{
43 release();
44 SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
45}

Member Function Documentation

◆ create()

bool Gamepads::create ( )

detection des pads connectes.

Definition at line 10 of file gamepads.cpp.

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}
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
Gamepad & pad(const unsigned int index)
renvoie un game controller.
Definition gamepads.cpp:102

◆ release()

void Gamepads::release ( )

Definition at line 47 of file gamepads.cpp.

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}

◆ update()

void Gamepads::update ( )

lecture des infos des pads connectes.

Definition at line 56 of file gamepads.cpp.

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}

◆ pads()

int Gamepads::pads ( )

renvoie le nombre de game controllers.

Definition at line 97 of file gamepads.cpp.

98{
99 return (int) m_pads.size();
100}

◆ pad()

Gamepad & Gamepads::pad ( const unsigned int index)

renvoie un game controller.

Definition at line 102 of file gamepads.cpp.

103{
104 assert(index < m_pads.size());
105 return m_pads[index];
106}

◆ button()

int Gamepads::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 at line 108 of file gamepads.cpp.

109{
110 return pad(index).button(b);
111}
int button(const SDL_GameControllerButton b)
renvoie l'etat d'un bouton.
Definition gamepads.cpp:133

◆ clear_button()

void Gamepads::clear_button ( const unsigned int index,
const SDL_GameControllerButton b )

desactive un button d'un controlleur.

Definition at line 113 of file gamepads.cpp.

114{
115 return pad(index).clear_button(b);
116}
void clear_button(const SDL_GameControllerButton b)
desactive un button.
Definition gamepads.cpp:139

◆ axis()

float Gamepads::axis ( const unsigned int index,
const SDL_GameControllerAxis a )

renvoie la position d'un axe d'un controlleur.

Definition at line 118 of file gamepads.cpp.

119{
120 return pad(index).axis(a);
121}
float axis(const SDL_GameControllerAxis a)
renvoie la position d'un axe.
Definition gamepads.cpp:145

◆ clear_axis()

void Gamepads::clear_axis ( const unsigned int index,
const SDL_GameControllerAxis a )

re-initialise la position d'un axe d'un controlleur.

Definition at line 123 of file gamepads.cpp.

124{
125 return pad(index).clear_axis(a);
126}
void clear_axis(const SDL_GameControllerAxis a)
re-initialise la position d'un axe.
Definition gamepads.cpp:151

Member Data Documentation

◆ m_pads

std::vector<Gamepad> Gamepads::m_pads
protected

Definition at line 64 of file gamepads.h.


The documentation for this struct was generated from the following files: