diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-11-27 21:46:48 +0100 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-11-27 21:47:49 +0100 |
commit | 65e94b56c9b7b0f69911ba37fe1a22f22e9ba09e (patch) | |
tree | c4037f2e53442107f4b421d23db5625559755df8 | |
parent | 8ca7a933eb5d2b900ceb11fd8008941d59566666 (diff) | |
download | mana-65e94b56c9b7b0f69911ba37fe1a22f22e9ba09e.tar.gz mana-65e94b56c9b7b0f69911ba37fe1a22f22e9ba09e.tar.bz2 mana-65e94b56c9b7b0f69911ba37fe1a22f22e9ba09e.tar.xz mana-65e94b56c9b7b0f69911ba37fe1a22f22e9ba09e.zip |
Make sure to initialize joystick enabled state
Joystick enabled state could end up uninitialized on unsuccesfully
trying to open a joystick. In addition, the enabled state wasn't
actually used in the accessor methods for the joystick buttons.
-rw-r--r-- | src/game.cpp | 2 | ||||
-rw-r--r-- | src/joystick.cpp | 42 | ||||
-rw-r--r-- | src/joystick.h | 35 |
3 files changed, 31 insertions, 48 deletions
diff --git a/src/game.cpp b/src/game.cpp index 245e711e..9fa0129e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -297,9 +297,7 @@ Game::Game(Network *network): // TODO: The user should be able to choose which one to use // Open the first device if (Joystick::getNumberOfJoysticks() > 0) - { joystick = new Joystick(0); - } network->registerHandler(mBeingHandler.get()); network->registerHandler(mBuySellHandler.get()); diff --git a/src/joystick.cpp b/src/joystick.cpp index b69537cf..b05e9b5f 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -24,12 +24,17 @@ #include "configuration.h" #include "log.h" +#include <cassert> + int Joystick::joystickCount = 0; void Joystick::init() { SDL_InitSubSystem(SDL_INIT_JOYSTICK); - //SDL_JoystickEventState(SDL_ENABLE); + + // Have SDL call SDL_JoystickUpdate() automatically + SDL_JoystickEventState(SDL_ENABLE); + joystickCount = SDL_NumJoysticks(); logger->log("%i joysticks/gamepads found", joystickCount); for (int i = 0; i < joystickCount; i++) @@ -37,11 +42,11 @@ void Joystick::init() } Joystick::Joystick(int no): - mDirection(0), mCalibrating(false) + mDirection(0), + mCalibrating(false), + mEnabled(false) { - // TODO Bail out here? - if (no > joystickCount) - return; + assert(no < joystickCount); mJoystick = SDL_JoystickOpen(no); @@ -66,50 +71,39 @@ Joystick::Joystick(int no): Joystick::~Joystick() { - SDL_JoystickClose(mJoystick); + SDL_JoystickClose(mJoystick); } void Joystick::update() { mDirection = 0; - SDL_JoystickUpdate(); - // When calibrating, don't bother the outside with our state if (mCalibrating) { doCalibration(); return; }; - if (!mEnabled) return; + if (!mEnabled) + return; // X-Axis int position = SDL_JoystickGetAxis(mJoystick, 0); if (position >= mRightTolerance) - { mDirection |= RIGHT; - } else if (position <= mLeftTolerance) - { mDirection |= LEFT; - } // Y-Axis position = SDL_JoystickGetAxis(mJoystick, 1); if (position <= mUpTolerance) - { mDirection |= UP; - } else if (position >= mDownTolerance) - { mDirection |= DOWN; - } // Buttons for (int i = 0; i < MAX_BUTTONS; i++) - { mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1); - } } void Joystick::startCalibration() @@ -126,24 +120,16 @@ void Joystick::doCalibration() // X-Axis int position = SDL_JoystickGetAxis(mJoystick, 0); if (position > mRightTolerance) - { mRightTolerance = position; - } else if (position < mLeftTolerance) - { mLeftTolerance = position; - } // Y-Axis position = SDL_JoystickGetAxis(mJoystick, 1); if (position > mDownTolerance) - { mDownTolerance = position; - } else if (position < mUpTolerance) - { mUpTolerance = position; - } } void Joystick::finishCalibration() @@ -157,5 +143,5 @@ void Joystick::finishCalibration() bool Joystick::buttonPressed(unsigned char no) const { - return (no < MAX_BUTTONS) ? mButtons[no] : false; + return (mEnabled && no < MAX_BUTTONS) ? mButtons[no] : false; } diff --git a/src/joystick.h b/src/joystick.h index 4cc1babd..ee029915 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -35,7 +35,12 @@ class Joystick /** * Directions, to be used as bitmask values. */ - enum { UP = 1, DOWN = 2, LEFT = 4, RIGHT = 8 }; + enum { + UP = 1, + DOWN = 2, + LEFT = 4, + RIGHT = 8 + }; /** * Initializes the joystick subsystem. @@ -55,33 +60,27 @@ class Joystick ~Joystick(); - bool - isEnabled() const { return mEnabled; } + bool isEnabled() const { return mEnabled; } - void - setEnabled(bool enabled) { mEnabled = enabled; } + void setEnabled(bool enabled) { mEnabled = enabled; } /** * Updates the direction and button information. */ - void - update(); + void update(); - void - startCalibration(); + void startCalibration(); - void - finishCalibration(); + void finishCalibration(); - bool - isCalibrating() const { return mCalibrating; } + bool isCalibrating() const { return mCalibrating; } bool buttonPressed(unsigned char no) const; - bool isUp() const { return mDirection & UP; }; - bool isDown() const { return mDirection & DOWN; }; - bool isLeft() const { return mDirection & LEFT; }; - bool isRight() const { return mDirection & RIGHT; }; + bool isUp() const { return mEnabled && (mDirection & UP); }; + bool isDown() const { return mEnabled && (mDirection & DOWN); }; + bool isLeft() const { return mEnabled && (mDirection & LEFT); }; + bool isRight() const { return mEnabled && (mDirection & RIGHT); }; protected: unsigned char mDirection; @@ -97,4 +96,4 @@ class Joystick void doCalibration(); }; -#endif +#endif // _TMW_JOYSTICK_H |