|
From: knittl <kn...@us...> - 2009-08-03 08:40:39
|
Module: performous
Branch: master
Commit: 05b678e6dada0a38af37e4f92386e5408adee747
Author: Vincent Le Ligeour <yo...@us...>
Date: Sat Aug 1 17:57:23 2009 +0200
Added input with event
---
game/joystick.cc | 56 +++++++++++++++++++++++++++++++++++++++--------------
game/joystick.hh | 28 +++++++++-----------------
2 files changed, 51 insertions(+), 33 deletions(-)
diff --git a/game/joystick.cc b/game/joystick.cc
index e115e00..21c3f9c 100644
--- a/game/joystick.cc
+++ b/game/joystick.cc
@@ -3,22 +3,29 @@
#include <boost/lexical_cast.hpp>
-/*
-enum TypeSDL {UNKNOWN, GUITAR_RB, DRUM_RB, GUITAR_GH, DRUM_GH, TYPESDL_N};
static const unsigned SDL_BUTTONS = 6;
-int buttonFromSDL(TypeSDL type, unsigned sdlButton) {
- static const int inputmap[TYPESDL_N][SDL_BUTTONS] = {
+int buttonFromSDL(input::Private::Type _type, unsigned int _sdl_button) {
+ static const int inputmap[4][SDL_BUTTONS] = {
{ 2, 0, 1, 3, 4, 0 }, // Guitar Hero guitar
{ 3, 4, 1, 2, 0, 4 }, // Guitar Hero drums
{ 3, 0, 1, 2, 4, 0 }, // Rock Band guitar
{ 3, 4, 1, 2, 0, 0 } // Rock Band drums
};
- if (sdlButton > SDL_BUTTONS) return -1;
- return inputmap[type][sdlButton];
+ if( _sdl_button > 6 ) return -1;
+ switch(_type) {
+ case input::Private::GUITAR_GH:
+ return inputmap[0][_sdl_button];
+ case input::Private::DRUMS_GH:
+ return inputmap[1][_sdl_button];
+ case input::Private::GUITAR_RB:
+ return inputmap[2][_sdl_button];
+ case input::Private::DRUMS_RB:
+ return inputmap[3][_sdl_button];
+ default:
+ return -1;
+ }
}
-*/
-
/*
@@ -80,21 +87,21 @@ void input::init() {
std::cout << "Id: " << i << std::endl;
std::cout << " Name: " << name << std::endl;
if( name.find("Guitar Hero3") != std::string::npos ) {
- input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::GUITAR_GH);
std::cout << " Detected as: Guitar Hero Guitar" << std::endl;
+ input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::GUITAR_GH);
} else if( name.find("Guitar Hero4") != std::string::npos ) {
// here we can have both drumkit or guitar .... let say the drumkit
- input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::DRUM_GH);
std::cout << " Detected as: Guitar Hero Drums (guessed)" << std::endl;
+ input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::DRUMS_GH);
} else if( name.find("Harmonix Guitar") != std::string::npos ) {
std::cout << " Detected as: RockBand Guitar" << std::endl;
input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::GUITAR_RB);
} else if( name.find("Harmonix Drum Kit") != std::string::npos ) {
std::cout << " Detected as: RockBand Drums" << std::endl;
- input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::DRUM_RB);
+ input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::DRUMS_RB);
} else {
- std::cout << " Detected as: Unknwown (please report the name)" << std::endl;
- input::Private::devices[i] = input::Private::InputDevPrivate();
+ std::cout << " Detected as: Unknwown (please report the name, assuming Guitar Hero Drums)" << std::endl;
+ input::Private::devices[i] = input::Private::InputDevPrivate(input::Private::DRUMS_GH);
}
}
// Here we should send an event to have correct state buttons
@@ -103,6 +110,7 @@ void input::init() {
bool input::pushEvent(SDL_Event _e) {
int joy_id;
+ int button;
using namespace input::Private;
Event event;
@@ -138,10 +146,28 @@ bool input::pushEvent(SDL_Event _e) {
}
break;
case SDL_JOYBUTTONDOWN:
+ joy_id = _e.jbutton.which;
+ button = buttonFromSDL(devices[joy_id].type(),_e.jbutton.button);
+ if( button == -1 ) break;
+ for( unsigned int i = 0 ; i < BUTTONS ; ++i ) {
+ event.pressed[i] = devices[joy_id].pressed(i);
+ }
+ event.type = input::Event::PRESS;
+ event.button = button;
+ event.pressed[button] = true;
+ devices[joy_id].addEvent(event);
+ break;
case SDL_JOYBUTTONUP:
joy_id = _e.jbutton.which;
- // do stuffs to devices[joy_id] states (BUTTONS)
- // do stuffs to devices[joy_id] events (BUTTONS)
+ button = buttonFromSDL(devices[joy_id].type(),_e.jbutton.button);
+ if( button == -1 ) break;
+ for( unsigned int i = 0 ; i < BUTTONS ; ++i ) {
+ event.pressed[i] = devices[joy_id].pressed(i);
+ }
+ event.type = input::Event::RELEASE;
+ event.button = button;
+ event.pressed[button] = false;
+ devices[joy_id].addEvent(event);
break;
case SDL_JOYBALLMOTION:
default:
diff --git a/game/joystick.hh b/game/joystick.hh
index 6e2c004..603fa23 100644
--- a/game/joystick.hh
+++ b/game/joystick.hh
@@ -22,10 +22,11 @@ namespace input {
};
namespace Private {
- enum Type {UNKNOWN, GUITAR_RB, DRUM_RB, GUITAR_GH, DRUM_GH};
+ enum Type {GUITAR_RB, DRUMS_RB, GUITAR_GH, DRUMS_GH};
class InputDevPrivate {
public:
- InputDevPrivate(input::Private::Type _type = input::Private::UNKNOWN) : m_assigned(false), m_type(_type) {};
+ InputDevPrivate() : m_assigned(false), m_type(input::Private::DRUMS_GH) {};
+ InputDevPrivate(input::Private::Type _type) : m_assigned(false), m_type(_type) {};
bool tryPoll(Event& _event) {
if( m_events.empty() ) return false;
_event = m_events.front();
@@ -57,22 +58,12 @@ namespace input {
(m_type == input::Private::GUITAR_GH || m_type == input::Private::GUITAR_RB) ) {
return true;
} else if( _type == input::DRUMS &&
- (m_type == input::Private::DRUM_GH || m_type == input::Private::DRUM_RB) ) {
+ (m_type == input::Private::DRUMS_GH || m_type == input::Private::DRUMS_RB) ) {
return true;
} else {
return false;
}
};
- void setType(input::DevType _type) {
- if( _type == input::GUITAR ) {
- m_type = input::Private::GUITAR_RB;
- } else if( _type == input::DRUMS ) {
- m_type = input::Private::DRUM_RB;
- } else {
- m_type = input::Private::UNKNOWN;
- }
- init_devices();
- };
private:
std::deque<Event> m_events;
bool m_assigned;
@@ -108,12 +99,13 @@ namespace input {
throw std::runtime_error("No matching instrument available");
};
~InputDev() {
- std::cout << "Releasing @" << m_device_id << std::endl;
- input::Private::devices[m_device_id].unassign();
+ using namespace input::Private;
+ // we assume find will success
+ devices.find(m_device_id)->second.unassign();
};
- bool tryPoll(Event& _e) {return input::Private::devices[m_device_id].tryPoll(_e);};
- void addEvent(Event _e) {input::Private::devices[m_device_id].addEvent(_e);};
- bool pressed(int _button) {return input::Private::devices[m_device_id].pressed(_button);}; // Current state
+ bool tryPoll(Event& _e) {return input::Private::devices.find(m_device_id)->second.tryPoll(_e);};
+ void addEvent(Event _e) {input::Private::devices.find(m_device_id)->second.addEvent(_e);};
+ bool pressed(int _button) {return input::Private::devices.find(m_device_id)->second.pressed(_button);}; // Current state
private:
unsigned int m_device_id; // should be some kind of reference
};
|