|
From: knittl <kn...@us...> - 2009-08-03 08:40:12
|
Module: performous
Branch: master
Commit: 659a9e44d603c3ac6f33f3c8a7e6f7bb4cd5d58d
Author: Vincent Le Ligeour <yo...@us...>
Date: Sat Aug 1 15:03:02 2009 +0200
Added some init stuffs when changing instrument type
---
game/joystick.cc | 33 +++++++++++++++++++++------------
game/joystick.hh | 5 +++++
2 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/game/joystick.cc b/game/joystick.cc
index 226aa75..8e7edd9 100644
--- a/game/joystick.cc
+++ b/game/joystick.cc
@@ -98,6 +98,24 @@ void Joystick::clearEvents() {
* New input management, superseed all joysticks stuffs
*/
input::Private::InputDevs input::Private::devices;
+input::SDL_devices input::sdl_devices;
+
+void input::init_devices() {
+ for (input::Private::InputDevs::iterator it = input::Private::devices.begin() ; it != input::Private::devices.end() ; ++it) {
+ unsigned int id = it->first;
+ if( it->second.assigned() ) continue;
+ unsigned int num_buttons = SDL_JoystickNumButtons(input::sdl_devices[id]);
+ for( unsigned int i = 0 ; i < num_buttons ; ++i ) {
+ SDL_Event event;
+ event.type = SDL_JOYBUTTONDOWN;
+ event.jbutton.type = SDL_JOYBUTTONDOWN;
+ event.jbutton.which = id;
+ event.jbutton.button = i;
+ event.jbutton.state = SDL_PRESSED;
+ input::pushEvent(event);
+ }
+ }
+}
void input::init() {
// new stuffs
@@ -105,7 +123,7 @@ void input::init() {
std::cout << "Detecting " << nbjoysticks << " joysticks..." << std::endl;
for (unsigned int i = 0 ; i < nbjoysticks ; ++i) {
- SDL_Joystick * joystick = SDL_JoystickOpen(i);
+ input::sdl_devices[i] = 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,18 +134,9 @@ 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);
- }
}
+ // Here we should send an event to have correct state buttons
+ init_devices();
// compatibility with old joystick stuffs
for (input::Private::InputDevs::iterator it = input::Private::devices.begin() ; it != input::Private::devices.end() ; ++it) {
if( it->second.type() == GUITAR_GH || it->second.type() == DRUM_GH ) {
diff --git a/game/joystick.hh b/game/joystick.hh
index 7262d02..6b8d0ff 100644
--- a/game/joystick.hh
+++ b/game/joystick.hh
@@ -74,6 +74,10 @@ namespace input {
static const std::size_t BUTTONS = 6;
+ typedef std::map<unsigned int,SDL_Joystick*> SDL_devices;
+ extern SDL_devices sdl_devices;
+ void init_devices();
+
struct InputDevEvent {
enum Type { PRESS, RELEASE, PICK };
Type type;
@@ -106,6 +110,7 @@ namespace input {
bool assigned() {return m_assigned;};
bool pressed(int _button) {return m_pressed[_button];};
input::Type type() {return m_type;};
+ void setType(input::Type _type) {m_type=_type;init_devices();};
private:
std::deque<InputDevEvent> m_events;
bool m_assigned;
|