From 65e94b56c9b7b0f69911ba37fe1a22f22e9ba09e Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 27 Nov 2008 21:46:48 +0100 Subject: 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. --- src/joystick.cpp | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'src/joystick.cpp') 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 + 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; } -- cgit v1.2.3-70-g09d2