onepad: use range loop and vector of unique_ptr to manage the joystick

This commit is contained in:
Gregory Hainaut 2017-04-10 18:19:23 +02:00
parent 873ae6717e
commit 9c7e9cc6f2
7 changed files with 27 additions and 48 deletions

View File

@ -3,7 +3,8 @@
#include "SDL/joystick.h" #include "SDL/joystick.h"
#endif #endif
vector<GamePad *> s_vgamePad; std::vector<std::unique_ptr<GamePad>> s_vgamePad;
bool GamePadIdWithinBounds(int GamePadId) bool GamePadIdWithinBounds(int GamePadId)
{ {
return ((GamePadId >= 0) && (GamePadId < (int)s_vgamePad.size())); return ((GamePadId >= 0) && (GamePadId < (int)s_vgamePad.size()));
@ -17,7 +18,7 @@ bool GamePadIdWithinBounds(int GamePadId)
/** /**
* Find every interesting devices and create right structure for them(depend on backend) * Find every interesting devices and create right structure for them(depend on backend)
**/ **/
void GamePad::EnumerateGamePads(vector<GamePad *> &vgamePad) void GamePad::EnumerateGamePads(std::vector<std::unique_ptr<GamePad>> &vgamePad)
{ {
#ifdef SDL_BUILD #ifdef SDL_BUILD
JoystickInfo::EnumerateJoysticks(vgamePad); JoystickInfo::EnumerateJoysticks(vgamePad);
@ -38,9 +39,7 @@ void GamePad::DoRumble(int type, int pad)
{ {
u32 id = conf->get_joyid(pad); u32 id = conf->get_joyid(pad);
if (GamePadIdWithinBounds(id)) { if (GamePadIdWithinBounds(id)) {
GamePad *gamePad = s_vgamePad[id]; s_vgamePad[id]->Rumble(type, pad);
if (gamePad)
gamePad->Rumble(type, pad);
} }
} }

View File

@ -38,7 +38,7 @@ public:
/** /**
* Find every interesting devices and create right structure for them(depend on backend) * Find every interesting devices and create right structure for them(depend on backend)
**/ **/
static void EnumerateGamePads(vector<GamePad *> &vgamePad); static void EnumerateGamePads(std::vector<std::unique_ptr<GamePad>> &vgamePad);
static void UpdateReleaseState(); static void UpdateReleaseState();
/** /**
* Update state of every attached devices * Update state of every attached devices
@ -148,8 +148,8 @@ protected:
int numbuttons, numaxes, numhats; int numbuttons, numaxes, numhats;
int deadzone; int deadzone;
int pad; int pad;
vector<int> vbuttonstate, vaxisstate, vhatstate; std::vector<int> vbuttonstate, vaxisstate, vhatstate;
}; };
extern vector<GamePad *> s_vgamePad; extern std::vector<std::unique_ptr<GamePad>> s_vgamePad;
extern bool GamePadIdWithinBounds(int joyid); extern bool GamePadIdWithinBounds(int joyid);

View File

@ -534,7 +534,6 @@ void Dialog::config_key(int pad, int key)
// I don't have any guarantee that not-yet-pressed state is egual to released state // I don't have any guarantee that not-yet-pressed state is egual to released state
GamePad::UpdateReleaseState(); GamePad::UpdateReleaseState();
while (!captured) { while (!captured) {
vector<GamePad *>::iterator itjoy;
if (PollX11KeyboardMouseEvent(key_pressed)) { if (PollX11KeyboardMouseEvent(key_pressed)) {
// special case for keyboard/mouse to handle multiple keys // special case for keyboard/mouse to handle multiple keys
// Note: key_pressed == 0 when ESC is hit to abort the capture // Note: key_pressed == 0 when ESC is hit to abort the capture
@ -548,25 +547,25 @@ void Dialog::config_key(int pad, int key)
} else { } else {
GamePad::UpdateGamePadState(); GamePad::UpdateGamePadState();
itjoy = s_vgamePad.begin(); for (auto &j : s_vgamePad) {
while ((itjoy != s_vgamePad.end()) && (!captured)) { if (j->PollButtons(key_pressed)) {
if ((*itjoy)->PollButtons(key_pressed)) {
clear_key(pad, key); clear_key(pad, key);
set_key(pad, key, key_pressed); set_key(pad, key, key_pressed);
m_map_images[pad][key_pressed] = key; m_map_images[pad][key_pressed] = key;
captured = true; captured = true;
} else if ((*itjoy)->PollAxes(key_pressed)) { } else if (j->PollAxes(key_pressed)) {
clear_key(pad, key); clear_key(pad, key);
set_key(pad, key, key_pressed); set_key(pad, key, key_pressed);
m_map_images[pad][key_pressed] = key; m_map_images[pad][key_pressed] = key;
captured = true; captured = true;
} else if ((*itjoy)->PollHats(key_pressed)) { } else if (j->PollHats(key_pressed)) {
clear_key(pad, key); clear_key(pad, key);
set_key(pad, key, key_pressed); set_key(pad, key, key_pressed);
m_map_images[pad][key_pressed] = key; m_map_images[pad][key_pressed] = key;
captured = true; captured = true;
} }
++itjoy; if (captured)
break;
} }
} }
} }

View File

@ -78,14 +78,6 @@ void _PADclose()
{ {
SetAutoRepeat(true); SetAutoRepeat(true);
vector<GamePad *>::iterator it = s_vgamePad.begin();
// Delete everything in the vector vjoysticks.
while (it != s_vgamePad.end()) {
delete *it;
++it;
}
s_vgamePad.clear(); s_vgamePad.clear();
} }
@ -97,7 +89,7 @@ void PollForJoystickInput(int cpad)
GamePad::UpdateGamePadState(); GamePad::UpdateGamePadState();
for (int i = 0; i < MAX_KEYS; i++) { for (int i = 0; i < MAX_KEYS; i++) {
GamePad *gamePad = s_vgamePad[joyid]; auto &gamePad = s_vgamePad[joyid];
switch (type_of_joykey(cpad, i)) { switch (type_of_joykey(cpad, i)) {
case PAD_JOYBUTTONS: { case PAD_JOYBUTTONS: {

View File

@ -30,21 +30,16 @@ static u32 s_bSDLInit = false;
void JoystickInfo::UpdateReleaseState() void JoystickInfo::UpdateReleaseState()
{ {
vector<GamePad *>::iterator itjoy = s_vgamePad.begin();
SDL_JoystickUpdate(); SDL_JoystickUpdate();
// Save everything in the vector s_vjoysticks. // Save everything in the vector s_vjoysticks.
while (itjoy != s_vgamePad.end()) { for (auto &j : s_vgamePad)
(*itjoy)->SaveState(); j->SaveState();
++itjoy;
}
} }
// opens handles to all possible joysticks // opens handles to all possible joysticks
void JoystickInfo::EnumerateJoysticks(vector<GamePad *> &vjoysticks) void JoystickInfo::EnumerateJoysticks(std::vector<std::unique_ptr<GamePad>> &vjoysticks)
{ {
if (!s_bSDLInit) { if (!s_bSDLInit) {
// Tell SDL to catch event even if the windows isn't focussed // Tell SDL to catch event even if the windows isn't focussed
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
@ -61,19 +56,10 @@ void JoystickInfo::EnumerateJoysticks(vector<GamePad *> &vjoysticks)
s_bSDLInit = true; s_bSDLInit = true;
} }
vector<GamePad *>::iterator it = vjoysticks.begin(); vjoysticks.clear();
// Delete everything in the vector vjoysticks. for (int i = 0; i < SDL_NumJoysticks(); ++i) {
while (it != vjoysticks.end()) { vjoysticks.push_back(std::unique_ptr<GamePad>(new JoystickInfo(i)));
delete *it;
++it;
}
vjoysticks.resize(SDL_NumJoysticks());
for (int i = 0; i < (int)vjoysticks.size(); ++i) {
vjoysticks[i] = new JoystickInfo();
vjoysticks[i]->Init(i);
} }
} }

View File

@ -32,7 +32,7 @@
class JoystickInfo : GamePad class JoystickInfo : GamePad
{ {
public: public:
JoystickInfo() JoystickInfo(int id)
: GamePad() : GamePad()
, joy(nullptr) , joy(nullptr)
{ {
@ -40,6 +40,7 @@ public:
first = true; first = true;
memset(effects, 0, sizeof(effects)); memset(effects, 0, sizeof(effects));
memset(effects_id, 0, sizeof(effects_id)); memset(effects_id, 0, sizeof(effects_id));
Init(id);
} }
~JoystickInfo() ~JoystickInfo()
@ -47,12 +48,13 @@ public:
Destroy(); Destroy();
} }
JoystickInfo(const JoystickInfo &); // copy constructor JoystickInfo(const JoystickInfo &) = delete; // copy constructor
JoystickInfo &operator=(const JoystickInfo &); // assignment JoystickInfo &operator=(const JoystickInfo &) = delete; // assignment
void Destroy(); void Destroy();
// opens handles to all possible joysticks // opens handles to all possible joysticks
static void EnumerateJoysticks(vector<GamePad *> &vjoysticks); static void EnumerateJoysticks(std::vector<std::unique_ptr<GamePad>> &vjoysticks);
void Rumble(int type, int pad); void Rumble(int type, int pad);

View File

@ -44,6 +44,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <pthread.h> #include <pthread.h>
#include <memory>
using namespace std; using namespace std;
#define PADdefs #define PADdefs