summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/game.cpp4
-rw-r--r--src/joystick.cpp42
-rw-r--r--src/joystick.h39
3 files changed, 35 insertions, 50 deletions
diff --git a/src/game.cpp b/src/game.cpp
index 34fb4622..16bc082b 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);
- }
+ joystick = new Joystick(0);
network->registerHandler(mBeingHandler.get());
network->registerHandler(mBuySellHandler.get());
diff --git a/src/joystick.cpp b/src/joystick.cpp
index f009ebc4..b3aba4fc 100644
--- a/src/joystick.cpp
+++ b/src/joystick.cpp
@@ -19,6 +19,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <cassert>
+
#include "configuration.h"
#include "joystick.h"
#include "log.h"
@@ -28,7 +30,10 @@ 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++)
@@ -36,11 +41,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);
@@ -65,50 +70,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()
@@ -125,24 +119,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()
@@ -156,5 +142,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..2baf3e61 100644
--- a/src/joystick.h
+++ b/src/joystick.h
@@ -30,12 +30,19 @@ class Joystick
/**
* Number of buttons we can handle.
*/
- enum { MAX_BUTTONS = 6 };
+ enum {
+ MAX_BUTTONS = 6
+ };
/**
* 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 +62,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 +98,4 @@ class Joystick
void doCalibration();
};
-#endif
+#endif // _TMW_JOYSTICK_H