summaryrefslogtreecommitdiff
path: root/src
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
parenta4e81905cb19ba65c45a91371651026b524b7f44 (diff)
downloadmana-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.tar.gz
mana-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.tar.bz2
mana-76d25a53fad89f43472ebe25ea3a97c664eb8fcd.tar.xz
mana-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')
-rw-r--r--src/game.cpp2
-rw-r--r--src/joystick.cpp42
-rw-r--r--src/joystick.h35
3 files changed, 31 insertions, 48 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 3e69c28e..97b9e57e 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -315,9 +315,7 @@ Game::Game():
// TODO: The user should be able to choose which one to use
// Open the first device
if (Joystick::getNumberOfJoysticks() > 0)
- {
joystick = new Joystick(0);
- }
Net::registerHandler(mBeingHandler.get());
Net::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