|
From: knittl <kn...@us...> - 2009-08-03 08:40:03
|
Module: performous
Branch: master
Commit: 376141d58e60d6913c44a84b13e7957766c8bf4e
Author: Vincent Le Ligeour <yo...@us...>
Date: Sat Aug 1 13:01:46 2009 +0200
Added a new stable InputDev version
---
game/joystick.cc | 77 +++++++++++++++++-------------------------------------
game/joystick.hh | 22 +++------------
2 files changed, 29 insertions(+), 70 deletions(-)
diff --git a/game/joystick.cc b/game/joystick.cc
index 213284d..804c48e 100644
--- a/game/joystick.cc
+++ b/game/joystick.cc
@@ -5,61 +5,14 @@
Joysticks joysticks;
-Joystick::Joystick(unsigned int _id): m_id(_id) {
- if( getName().find("Guitar Hero") != std::string::npos ) {
- m_type = Joystick::GUITARHERO;
- } else if( getName().find("Harmonix") != std::string::npos ) {
- m_type = Joystick::ROCKBAND;
- } else {
- m_type = Joystick::UNKNOWN;
- }
-};
+Joystick::Joystick(Joystick::Type _type): m_type(_type) { }
+
Joystick::~Joystick() {
- // Do it another way :/
- //if(SDL_JoystickOpened(m_id)) SDL_JoystickClose(m_joystick);
-};
-std::string Joystick::getName() const {
- return std::string(SDL_JoystickName(m_id));
};
+
Joystick::Type Joystick::getType() const {
return m_type;
}
-bool Joystick::buttonState(unsigned char _button_id) const {
- return (SDL_JoystickGetButton(m_joystick, _button_id) != 0);
-};
-JoystickEvent::HatDirection Joystick::hat(unsigned char _hat_id) const {
- unsigned char direction = SDL_JoystickGetHat(m_joystick, _hat_id);
- switch(direction) {
- case SDL_HAT_LEFTUP:
- return JoystickEvent::LEFT_UP;
- case SDL_HAT_LEFT:
- return JoystickEvent::LEFT;
- case SDL_HAT_LEFTDOWN:
- return JoystickEvent::LEFT_DOWN;
- case SDL_HAT_UP:
- return JoystickEvent::UP;
- case SDL_HAT_CENTERED:
- return JoystickEvent::CENTERED;
- case SDL_HAT_DOWN:
- return JoystickEvent::DOWN;
- case SDL_HAT_RIGHTUP:
- return JoystickEvent::RIGHT_UP;
- case SDL_HAT_RIGHT:
- return JoystickEvent::RIGHT;
- case SDL_HAT_RIGHTDOWN:
- return JoystickEvent::RIGHT_DOWN;
- }
- // we should not go here
- return JoystickEvent::CENTERED;
-};
-short Joystick::axis(unsigned char _axis_id) const {
- return SDL_JoystickGetAxis(m_joystick, _axis_id);
-};
-std::pair<int, int> Joystick::ball(int _ball_id) const {
- int dx, dy;
- SDL_JoystickGetBall(m_joystick, _ball_id, &dx,&dy);
- return std::pair<int,int>(dx,dy);
-};
void Joystick::addEvent(SDL_JoyAxisEvent event) {
JoystickEvent joy_event(JoystickEvent::AXIS_MOTION);
@@ -69,6 +22,7 @@ void Joystick::addEvent(SDL_JoyAxisEvent event) {
m_events.push_back(joy_event);
}
+
void Joystick::addEvent(SDL_JoyBallEvent event) {
JoystickEvent joy_event(JoystickEvent::BALL_MOTION);
@@ -78,6 +32,7 @@ void Joystick::addEvent(SDL_JoyBallEvent event) {
m_events.push_back(joy_event);
}
+
void Joystick::addEvent(SDL_JoyHatEvent event) {
JoystickEvent joy_event(JoystickEvent::HAT_MOTION);
@@ -117,6 +72,7 @@ void Joystick::addEvent(SDL_JoyHatEvent event) {
m_events.push_back(joy_event);
}
+
void Joystick::addEvent(SDL_JoyButtonEvent event) {
JoystickEvent joy_event((event.type == SDL_JOYBUTTONDOWN) ? JoystickEvent::BUTTON_DOWN : JoystickEvent::BUTTON_UP);
@@ -124,6 +80,7 @@ void Joystick::addEvent(SDL_JoyButtonEvent event) {
joy_event.button_state = event.state;
m_events.push_back(joy_event);
+
}
bool Joystick::tryPollEvent(JoystickEvent& _event) {
if( m_events.empty() ) return false;
@@ -132,6 +89,7 @@ bool Joystick::tryPollEvent(JoystickEvent& _event) {
m_events.pop_front();
return true;
}
+
void Joystick::clearEvents() {
m_events.clear();
}
@@ -148,13 +106,26 @@ void input::init() {
for (unsigned int i = 0 ; i < nbjoysticks ; i++) {
SDL_JoystickOpen(i);
- input::Private::devices[i] = input::Private::InputDevPrivate();
+ std::string name = SDL_JoystickName(i);
+ if( name.find("Guitar Hero") != std::string::npos ) {
+ input::Private::devices[i] = input::Private::InputDevPrivate(input::GUITAR_GH);
+ } else if( name.find("Harmonix") != std::string::npos ) {
+ input::Private::devices[i] = input::Private::InputDevPrivate(input::GUITAR_RB);
+ } else {
+ input::Private::devices[i] = input::Private::InputDevPrivate();
+ }
std::cout << "Id: " << i << std::endl;
- std::cout << " Name: " << SDL_JoystickName(i) << std::endl;
+ std::cout << " Name: " << name << std::endl;
}
// compatibility with old joystick stuffs
for (input::Private::InputDevs::iterator it = input::Private::devices.begin() ; it != input::Private::devices.end() ; ++it) {
- joysticks[it->first] = Joystick(it->first);
+ if( it->second.type() == GUITAR_GH || it->second.type() == DRUM_GH ) {
+ joysticks[it->first] = Joystick(Joystick::GUITARHERO);
+ } else if( it->second.type() == GUITAR_GH || it->second.type() == DRUM_GH ) {
+ joysticks[it->first] = Joystick(Joystick::ROCKBAND);
+ } else {
+ joysticks[it->first] = Joystick();
+ }
}
}
void input::clear() {
diff --git a/game/joystick.hh b/game/joystick.hh
index 3e75435..8901241 100644
--- a/game/joystick.hh
+++ b/game/joystick.hh
@@ -41,14 +41,11 @@ class JoystickEvent {
class Joystick {
public:
enum Type {UNKNOWN, GUITARHERO, ROCKBAND};
- Joystick() {};
- /// create joystick object with given id
- Joystick(unsigned int _id);
+ /// create joystick object
+ Joystick(Joystick::Type _type = Joystick::UNKNOWN);
~Joystick();
/// Return joystick Type
Joystick::Type getType() const;
- /// get name of joystick
- std::string getName() const;
/// add an even from an SDL_JoyAxisEvent
void addEvent(SDL_JoyAxisEvent event);
/// add an even from an SDL_JoyBallEvent
@@ -61,18 +58,8 @@ class Joystick {
bool tryPollEvent(JoystickEvent& _event);
/// clear all the events
void clearEvents();
- /// returns button state (true if pressed, else false)
- bool buttonState(unsigned char _button_id) const;
- /// returns hi-hat state
- JoystickEvent::HatDirection hat(unsigned char _hat_id) const;
- /// returns axis state
- short axis(unsigned char _axis_id) const;
- /// returns ball state
- std::pair<int, int> ball(int _ball_id) const;
private:
- SDL_Joystick * m_joystick;
std::deque<JoystickEvent> m_events;
- unsigned int m_id;
Type m_type;
};
@@ -83,7 +70,7 @@ extern Joysticks joysticks;
* New input management, superseed all joysticks stuffs
*/
namespace input {
- enum Type {UNKNOWN, GUITAR, DRUM};
+ enum Type {UNKNOWN, GUITAR_RB, DRUM_RB, GUITAR_GH, DRUM_GH};
static const std::size_t BUTTONS = 6;
@@ -98,7 +85,7 @@ namespace input {
namespace Private {
class InputDevPrivate {
public:
- InputDevPrivate() : m_assigned(false), m_type(input::UNKNOWN) {};
+ InputDevPrivate(input::Type _type = input::UNKNOWN) : m_assigned(false), m_type(_type) {};
bool tryPoll(InputDevEvent&) {return true;};
void addEvent(InputDevEvent) {};
void clearEvents() {m_events.clear();};
@@ -107,6 +94,7 @@ namespace input {
void assign() {m_assigned = true;};
void unassign() {m_assigned = false;};
bool assigned() {return m_assigned;};
+ input::Type type() {return m_type;};
private:
std::deque<InputDevEvent> m_events;
bool m_assigned;
|