|
From: Yoda-JM <yo...@us...> - 2009-07-24 17:07:11
|
Module: performous
Branch: master
Commit: 06bd1451a61eee101a853e5ce02ac4661cd00136
Author: Vincent Le Ligeour <yo...@us...>
Date: Fri Jul 24 19:06:48 2009 +0200
Added Joystick events management
---
game/joystick.cc | 104 +++++++++++++++++++++++++++++++++++++++++++++--
game/joystick.hh | 49 ++++++++++++++++++++--
game/main.cc | 17 ++++++++
game/screen_practice.cc | 9 ++--
4 files changed, 167 insertions(+), 12 deletions(-)
diff --git a/game/joystick.cc b/game/joystick.cc
index a787e68..951a0c1 100644
--- a/game/joystick.cc
+++ b/game/joystick.cc
@@ -28,11 +28,33 @@ std::string Joystick::getDescription() const {
bool Joystick::buttonState(unsigned char _button_id) const {
return (SDL_JoystickGetButton(m_joystick, _button_id) != 0);
};
-unsigned char Joystick::hat(unsigned char _hat_id) const {
- return SDL_JoystickGetHat(m_joystick, _hat_id);
+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::axe(unsigned char _axe_id) const {
- return SDL_JoystickGetAxis(m_joystick, _axe_id);
+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;
@@ -40,6 +62,80 @@ std::pair<int, int> Joystick::ball(int _ball_id) const {
return std::pair<int,int>(dx,dy);
};
+void Joystick::addEvent(SDL_JoyAxisEvent event) {
+ JoystickEvent joy_event(JoystickEvent::AXIS_MOTION);
+
+ joy_event.axis_id = event.axis;
+ joy_event.axis_value = event.value;
+
+ m_events.push_back(joy_event);
+}
+void Joystick::addEvent(SDL_JoyBallEvent event) {
+ JoystickEvent joy_event(JoystickEvent::BALL_MOTION);
+
+ joy_event.ball_id = event.ball;
+ joy_event.ball_dx = event.xrel;
+ joy_event.ball_dy = event.yrel;
+
+ m_events.push_back(joy_event);
+}
+void Joystick::addEvent(SDL_JoyHatEvent event) {
+ JoystickEvent joy_event(JoystickEvent::HAT_MOTION);
+
+ joy_event.hat_id = event.hat;
+ switch(event.value) {
+ case SDL_HAT_LEFTUP:
+ joy_event.hat_direction = JoystickEvent::LEFT_UP;
+ break;
+ case SDL_HAT_LEFT:
+ joy_event.hat_direction = JoystickEvent::LEFT;
+ break;
+ case SDL_HAT_LEFTDOWN:
+ joy_event.hat_direction = JoystickEvent::LEFT_DOWN;
+ break;
+ case SDL_HAT_UP:
+ joy_event.hat_direction = JoystickEvent::UP;
+ break;
+ case SDL_HAT_CENTERED:
+ joy_event.hat_direction = JoystickEvent::CENTERED;
+ break;
+ case SDL_HAT_DOWN:
+ joy_event.hat_direction = JoystickEvent::DOWN;
+ break;
+ case SDL_HAT_RIGHTUP:
+ joy_event.hat_direction = JoystickEvent::RIGHT_UP;
+ break;
+ case SDL_HAT_RIGHT:
+ joy_event.hat_direction = JoystickEvent::RIGHT;
+ break;
+ case SDL_HAT_RIGHTDOWN:
+ joy_event.hat_direction = JoystickEvent::RIGHT_DOWN;
+ break;
+ default:
+ joy_event.hat_direction = JoystickEvent::CENTERED;
+ break;
+ }
+
+ 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);
+
+ joy_event.button_id = event.button;
+
+ m_events.push_back(joy_event);
+}
+bool Joystick::tryPollEvent(JoystickEvent& _event) {
+ if( m_events.empty() ) return false;
+
+ _event = m_events.front();
+ m_events.pop_front();
+ return true;
+}
+void Joystick::clearEvents() {
+ m_events.clear();
+}
+
void joysticks_init() {
unsigned int nbjoysticks = SDL_NumJoysticks();
std::cout << "Detecting " << nbjoysticks << " joysticks..." << std::endl;
diff --git a/game/joystick.hh b/game/joystick.hh
index ea1f4a4..a0003bf 100644
--- a/game/joystick.hh
+++ b/game/joystick.hh
@@ -1,5 +1,6 @@
#pragma once
+#include "SDL_events.h"
#include "SDL_joystick.h"
#include "audio.hh"
@@ -12,6 +13,31 @@
void joysticks_init();
+class JoystickEvent {
+ public:
+ enum Type {BUTTON_DOWN, BUTTON_UP, HAT_MOTION, AXIS_MOTION, BALL_MOTION};
+ JoystickEvent(): button_id(), hat_id(), hat_direction(CENTERED), axis_id(), axis_value(), ball_id(), ball_dx(), ball_dy() {};
+ JoystickEvent(Type _type): type(_type), button_id(), hat_id(), hat_direction(CENTERED), axis_id(), axis_value(), ball_id(), ball_dx(), ball_dy() {};
+ Type type;
+ // for BUTTON_DOWN and BUTTON_UP
+ unsigned char button_id;
+ // for HAT_MOTION
+ enum HatDirection {
+ LEFT_UP, UP, RIGHT_UP,
+ LEFT, CENTERED, RIGHT,
+ LEFT_DOWN, DOWN, RIGHT_DOWN
+ };
+ unsigned char hat_id;
+ HatDirection hat_direction;
+ // for AXIS_MOTION
+ unsigned char axis_id;
+ short axis_value;
+ // for BALL_MOTION
+ unsigned char ball_id;
+ short ball_dx;
+ short ball_dy;
+};
+
/// joystick class used for dealing with ps3 drumsets
class Joystick {
public:
@@ -23,14 +49,29 @@ class Joystick {
std::string getName() const;
/// get longer description of joystick
std::string getDescription() const;
- /// returns true if the button with _button_id is pressed
+ /// add an even from an SDL_JoyAxisEvent
+ void addEvent(SDL_JoyAxisEvent event);
+ /// add an even from an SDL_JoyBallEvent
+ void addEvent(SDL_JoyBallEvent event);
+ /// add an even from an SDL_JoyHatEvent
+ void addEvent(SDL_JoyHatEvent event);
+ /// add an even from an SDL_JoyButtonEvent
+ void addEvent(SDL_JoyButtonEvent event);
+ /// try to poll an event, return false if no more event is available
+ 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;
- /// hi-hat
- unsigned char hat(unsigned char _hat_id) const;
- short axe(unsigned char _axe_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;
};
diff --git a/game/main.cc b/game/main.cc
index 3b2bae9..2efcaaa 100644
--- a/game/main.cc
+++ b/game/main.cc
@@ -1,6 +1,7 @@
#include "config.hh"
#include "fs.hh"
#include "screen.hh"
+#include "joystick.hh"
#include "screen_intro.hh"
#include "screen_songs.hh"
#include "screen_sing.hh"
@@ -44,6 +45,19 @@ static void checkEvents_SDL(ScreenManager& sm, Window& window) {
case SDL_VIDEORESIZE:
window.resize(event.resize.w, event.resize.h);
break;
+ case SDL_JOYAXISMOTION:
+ joysticks[event.jaxis.which].addEvent(event.jaxis);
+ break;
+ case SDL_JOYBALLMOTION:
+ joysticks[event.jball.which].addEvent(event.jball);
+ break;
+ case SDL_JOYHATMOTION:
+ joysticks[event.jhat.which].addEvent(event.jhat);
+ break;
+ case SDL_JOYBUTTONDOWN:
+ case SDL_JOYBUTTONUP:
+ joysticks[event.jbutton.which].addEvent(event.jbutton);
+ break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_ESCAPE) esc = false;
break;
@@ -75,6 +89,9 @@ static void checkEvents_SDL(ScreenManager& sm, Window& window) {
case GL_OUT_OF_MEMORY: std::cerr << "OpenGL error: invalid enum" << std::endl; break;
}
}
+ for(Joysticks::iterator it = joysticks.begin() ; it != joysticks.end() ; ++it ) {
+ it->second.clearEvents();
+ }
if( config["graphic/fullscreen"].b() != window.getFullscreen() )
window.setFullscreen(config["graphic/fullscreen"].b());
}
diff --git a/game/screen_practice.cc b/game/screen_practice.cc
index ba31139..7cd3339 100644
--- a/game/screen_practice.cc
+++ b/game/screen_practice.cc
@@ -40,9 +40,10 @@ void ScreenPractice::manageEvent(SDL_Event event) {
break;
}
}
- switch( event.type ) {
- case SDL_JOYBUTTONDOWN:
- switch( event.jbutton.button ) {
+ JoystickEvent joy_event;
+ for(Joysticks::iterator it = joysticks.begin() ; it != joysticks.end() ; ++it ) {
+ if( it->second.tryPollEvent(joy_event) && joy_event.type == JoystickEvent::BUTTON_DOWN) {
+ switch(joy_event.button_id) {
case PS3_DRUM_CONTROLLER_RED: // Snare drum
m_audio.playMusic(getDataPath("sounds/drum_snare.ogg"));
break;
@@ -62,7 +63,7 @@ void ScreenPractice::manageEvent(SDL_Event event) {
m_audio.playMusic(getDataPath("sounds/drum_bass.ogg"));
break;
}
- break;
+ }
}
}
|