summaryrefslogtreecommitdiff
path: root/src/joystick.cpp
diff options
context:
space:
mode:
authorIra Rice <irarice@gmail.com>2008-11-27 22:24:48 +0000
committerIra Rice <irarice@gmail.com>2008-11-27 22:24:48 +0000
commitc6adb930edd2d0a88d7c5ca4edd58d95382a5abf (patch)
tree83e8a74019c8c633d90a7e24504dd22006e768cb /src/joystick.cpp
parent886a5d4803e277b053014ae6b5c1c347f7756d48 (diff)
downloadmana-c6adb930edd2d0a88d7c5ca4edd58d95382a5abf.tar.gz
mana-c6adb930edd2d0a88d7c5ca4edd58d95382a5abf.tar.bz2
mana-c6adb930edd2d0a88d7c5ca4edd58d95382a5abf.tar.xz
mana-c6adb930edd2d0a88d7c5ca4edd58d95382a5abf.zip
Merged a patch by Bjorn to fix allowing the joystick to be used without
being enabled first. A similar patch was asked for by Doorsman here, so this combined with the setup button on client startup, should be enough to fulfill that request from Doors.
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 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;
}