summaryrefslogtreecommitdiff
path: root/src/joystick.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2008-11-27 21:46:48 +0100
committerBjørn Lindeijer <bjorn@lindeijer.nl>2008-12-13 16:55:12 +0100
commit76d25a53fad89f43472ebe25ea3a97c664eb8fcd (patch)
tree0272ba9dfebfc8f3f3797b944b1bfc3d2a5307b6 /src/joystick.cpp
parenta4e81905cb19ba65c45a91371651026b524b7f44 (diff)
downloadmana-client-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.tar.gz
mana-client-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.tar.bz2
mana-client-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.tar.xz
mana-client-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.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.cpp42
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;
}