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 /src/joystick.cpp | |
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.
Diffstat (limited to 'src/joystick.cpp')
-rw-r--r-- | src/joystick.cpp | 42 |
1 files changed, 14 insertions, 28 deletions
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; } |