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"
#endif
vector<GamePad *> s_vgamePad;
std::vector<std::unique_ptr<GamePad>> s_vgamePad;
bool GamePadIdWithinBounds(int GamePadId)
{
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)
**/
void GamePad::EnumerateGamePads(vector<GamePad *> &vgamePad)
void GamePad::EnumerateGamePads(std::vector<std::unique_ptr<GamePad>> &vgamePad)
{
#ifdef SDL_BUILD
JoystickInfo::EnumerateJoysticks(vgamePad);
@ -38,9 +39,7 @@ void GamePad::DoRumble(int type, int pad)
{
u32 id = conf->get_joyid(pad);
if (GamePadIdWithinBounds(id)) {
GamePad *gamePad = s_vgamePad[id];
if (gamePad)
gamePad->Rumble(type, pad);
s_vgamePad[id]->Rumble(type, pad);
}
}

View File

@ -38,7 +38,7 @@ public:
/**
* 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();
/**
* Update state of every attached devices
@ -148,8 +148,8 @@ protected:
int numbuttons, numaxes, numhats;
int deadzone;
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);

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
GamePad::UpdateReleaseState();
while (!captured) {
vector<GamePad *>::iterator itjoy;
if (PollX11KeyboardMouseEvent(key_pressed)) {
// special case for keyboard/mouse to handle multiple keys
// 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 {
GamePad::UpdateGamePadState();
itjoy = s_vgamePad.begin();
while ((itjoy != s_vgamePad.end()) && (!captured)) {
if ((*itjoy)->PollButtons(key_pressed)) {
for (auto &j : s_vgamePad) {
if (j->PollButtons(key_pressed)) {
clear_key(pad, key);
set_key(pad, key, key_pressed);
m_map_images[pad][key_pressed] = key;
captured = true;
} else if ((*itjoy)->PollAxes(key_pressed)) {
} else if (j->PollAxes(key_pressed)) {
clear_key(pad, key);
set_key(pad, key, key_pressed);
m_map_images[pad][key_pressed] = key;
captured = true;
} else if ((*itjoy)->PollHats(key_pressed)) {
} else if (j->PollHats(key_pressed)) {
clear_key(pad, key);
set_key(pad, key, key_pressed);
m_map_images[pad][key_pressed] = key;
captured = true;
}
++itjoy;
if (captured)
break;
}
}
}

View File

@ -78,14 +78,6 @@ void _PADclose()
{
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();
}
@ -97,7 +89,7 @@ void PollForJoystickInput(int cpad)
GamePad::UpdateGamePadState();
for (int i = 0; i < MAX_KEYS; i++) {
GamePad *gamePad = s_vgamePad[joyid];
auto &gamePad = s_vgamePad[joyid];
switch (type_of_joykey(cpad, i)) {
case PAD_JOYBUTTONS: {

View File

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

View File

@ -32,7 +32,7 @@
class JoystickInfo : GamePad
{
public:
JoystickInfo()
JoystickInfo(int id)
: GamePad()
, joy(nullptr)
{
@ -40,6 +40,7 @@ public:
first = true;
memset(effects, 0, sizeof(effects));
memset(effects_id, 0, sizeof(effects_id));
Init(id);
}
~JoystickInfo()
@ -47,12 +48,13 @@ public:
Destroy();
}
JoystickInfo(const JoystickInfo &); // copy constructor
JoystickInfo &operator=(const JoystickInfo &); // assignment
JoystickInfo(const JoystickInfo &) = delete; // copy constructor
JoystickInfo &operator=(const JoystickInfo &) = delete; // assignment
void Destroy();
// 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);

View File

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