|
From: knittl <kn...@us...> - 2009-08-03 08:40:15
|
Module: performous
Branch: master
Commit: f387273d15defbfdcb4b9bd2c3226648de5a1831
Author: Vincent Le Ligeour <yo...@us...>
Date: Sat Aug 1 14:35:12 2009 +0200
Finished InputDevPrivate and add init event to have right button state at start
---
game/joystick.cc | 18 +++++++++++++-----
game/joystick.hh | 17 ++++++++++++++---
2 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/game/joystick.cc b/game/joystick.cc
index 7ab17e6..226aa75 100644
--- a/game/joystick.cc
+++ b/game/joystick.cc
@@ -104,8 +104,8 @@ void input::init() {
unsigned int nbjoysticks = SDL_NumJoysticks();
std::cout << "Detecting " << nbjoysticks << " joysticks..." << std::endl;
- for (unsigned int i = 0 ; i < nbjoysticks ; i++) {
- SDL_JoystickOpen(i);
+ for (unsigned int i = 0 ; i < nbjoysticks ; ++i) {
+ SDL_Joystick * joystick = SDL_JoystickOpen(i);
std::string name = SDL_JoystickName(i);
if( name.find("Guitar Hero") != std::string::npos ) {
input::Private::devices[i] = input::Private::InputDevPrivate(input::GUITAR_GH);
@@ -116,6 +116,17 @@ void input::init() {
}
std::cout << "Id: " << i << std::endl;
std::cout << " Name: " << name << std::endl;
+ // Here we should send an event to have correct state buttons
+ unsigned int num_buttons = SDL_JoystickNumButtons(joystick);
+ for( unsigned int j = 0 ; j < num_buttons ; ++j ) {
+ SDL_Event event;
+ event.type = SDL_JOYBUTTONDOWN;
+ event.jbutton.type = SDL_JOYBUTTONDOWN;
+ event.jbutton.which = i;
+ event.jbutton.button = j;
+ event.jbutton.state = SDL_PRESSED;
+ input::pushEvent(event);
+ }
}
// compatibility with old joystick stuffs
for (input::Private::InputDevs::iterator it = input::Private::devices.begin() ; it != input::Private::devices.end() ; ++it) {
@@ -143,19 +154,16 @@ bool input::pushEvent(SDL_Event _e) {
switch(_e.type) {
case SDL_JOYAXISMOTION:
joy_id = _e.jaxis.which;
- if( !devices[joy_id].assigned() ) break;
// do stuffs to devices[joy_id] events (PICK)
break;
case SDL_JOYHATMOTION:
joy_id = _e.jhat.which;
- if( !devices[joy_id].assigned() ) break;
// do stuffs to devices[joy_id] events (PICK)
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
joy_id = _e.jbutton.which;
// do stuffs to devices[joy_id] states (BUTTONS)
- if( !devices[joy_id].assigned() ) break;
// do stuffs to devices[joy_id] events (BUTTONS)
break;
case SDL_JOYBALLMOTION:
diff --git a/game/joystick.hh b/game/joystick.hh
index bc42d6a..7262d02 100644
--- a/game/joystick.hh
+++ b/game/joystick.hh
@@ -86,9 +86,20 @@ namespace input {
class InputDevPrivate {
public:
InputDevPrivate(input::Type _type = input::UNKNOWN) : m_assigned(false), m_type(_type) {};
- bool tryPoll(InputDevEvent&) {return true;};
- // this will modify the m_pressed member
- void addEvent(InputDevEvent) {};
+ bool tryPoll(InputDevEvent& _event) {
+ if( m_events.empty() ) return false;
+ _event = m_events.front();
+ m_events.pop_front();
+ return true;
+ };
+ void addEvent(InputDevEvent _event) {
+ // only add event if the device is assigned
+ if( m_assigned ) m_events.push_back(_event);
+ // always keep track of button status
+ for( unsigned int i = 0 ; i < BUTTONS ; ++i ) {
+ m_pressed[i] = _event.pressed[i];
+ }
+ };
void clearEvents() {m_events.clear();};
void assign() {m_assigned = true;};
void unassign() {m_assigned = false; clearEvents();};
|