diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-02-06 10:33:41 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-02-06 10:33:41 +0000 |
commit | a626ab48948941c3ec86d3ee6a3ab3bc2a98538c (patch) | |
tree | 9aab3c81fa8aad5433da55face7a94d2899876a0 /src | |
parent | b0bc59179bd64ad6b1f006f8e109cbeb16ad7094 (diff) | |
download | mana-a626ab48948941c3ec86d3ee6a3ab3bc2a98538c.tar.gz mana-a626ab48948941c3ec86d3ee6a3ab3bc2a98538c.tar.bz2 mana-a626ab48948941c3ec86d3ee6a3ab3bc2a98538c.tar.xz mana-a626ab48948941c3ec86d3ee6a3ab3bc2a98538c.zip |
Added a Joystick class.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/game.cpp | 121 | ||||
-rw-r--r-- | src/gui/setup.cpp | 53 | ||||
-rw-r--r-- | src/gui/setup.h | 4 | ||||
-rw-r--r-- | src/joystick.cpp | 163 | ||||
-rw-r--r-- | src/joystick.h | 92 |
6 files changed, 293 insertions, 142 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 6e0747a8..8adbdf0a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -198,6 +198,8 @@ tmw_SOURCES = graphic/spriteset.cpp \ inventory.h \ item.cpp \ item.h \ + joystick.cpp \ + joystick.h \ localplayer.cpp \ localplayer.h \ lockedarray.h \ diff --git a/src/game.cpp b/src/game.cpp index a2e0912b..9f28096a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -27,7 +27,6 @@ #include <physfs.h> #include <sstream> #include <string> -#include <SDL_types.h> #include <guichan/sdl/sdlinput.hpp> @@ -36,6 +35,7 @@ #include "engine.h" #include "flooritemmanager.h" #include "graphics.h" +#include "joystick.h" #include "localplayer.h" #include "log.h" #include "npc.h" @@ -74,25 +74,6 @@ #include "resources/imagewriter.h" -enum { - JOY_UP, - JOY_DOWN, - JOY_LEFT, - JOY_RIGHT, - JOY_BTN0, - JOY_BTN1, - JOY_BTN2, - JOY_BTN3, - JOY_BTN4, - JOY_BTN5, - JOY_BTN6, - JOY_BTN7, - JOY_BTN8, - JOY_BTN9, - JOY_BTN10, - JOY_BTN11 -}; - extern Graphics *graphics; extern gcn::SDLInput *guiInput; @@ -105,7 +86,7 @@ volatile int tick_time; volatile bool action_time = false; volatile int fps = 0, frame = 0; Engine *engine = NULL; -SDL_Joystick *joypad = NULL; /**< Joypad object */ +Joystick *joystick; extern Window *weightNotice; extern Window *deathNotice; @@ -290,28 +271,12 @@ Game::Game(Network *network): player_node->setNetwork(network); engine->changeMap(map_path); - // Initialize joypad - SDL_InitSubSystem(SDL_INIT_JOYSTICK); - //SDL_JoystickEventState(SDL_ENABLE); - int num_joy = SDL_NumJoysticks(); - logger->log("%i joysticks/gamepads found", num_joy); - for (int i = 0; i < num_joy; i++) - logger->log("- %s", SDL_JoystickName(i)); + Joystick::init(); // TODO: The user should be able to choose which one to use // Open the first device - if (num_joy > 0) + if (Joystick::getNumberOfJoysticks() > 0) { - joypad = SDL_JoystickOpen(0); - if (joypad == NULL) - { - logger->log("Couldn't open joystick: %s", SDL_GetError()); - } - else { - logger->log("Axes: %i ", SDL_JoystickNumAxes(joypad)); - logger->log("Balls: %i", SDL_JoystickNumBalls(joypad)); - logger->log("Hats: %i", SDL_JoystickNumHats(joypad)); - logger->log("Buttons: %i", SDL_JoystickNumButtons(joypad)); - } + joystick = new Joystick(0); } mBeingHandler = new BeingHandler(); @@ -360,9 +325,9 @@ Game::~Game() beingManager = NULL; floorItemManager = NULL; - if (joypad != NULL) + if (joystick != NULL) { - SDL_JoystickClose(joypad); + delete joystick; } } @@ -430,43 +395,9 @@ void Game::handleInput() Uint8* keys; keys = SDL_GetKeyState(NULL); - // TODO: Only <= 6 buttons joypads are allowed - bool joy[10]; - memset(joy, 0, 10 * sizeof(bool)); - - // Get the state of the joypad axis/buttons - if (joypad != NULL) + if (joystick != NULL) { - int lowerTolerance = (int)config.getValue("leftTolerance", -100); - int upperTolerance = (int)config.getValue("rightTolerance", 100); - SDL_JoystickUpdate(); - int position = SDL_JoystickGetAxis(joypad, 0); - if (position >= upperTolerance) - { - joy[JOY_RIGHT] = true; - } - else if (position <= lowerTolerance) - { - joy[JOY_LEFT] = true; - } - lowerTolerance = (int)config.getValue("upTolerance", -100); - upperTolerance = (int)config.getValue("downTolerance", 100); - position = SDL_JoystickGetAxis(joypad, 1); - if (position <= lowerTolerance) - { - joy[JOY_UP] = true; - } - else if (position >= upperTolerance) - { - joy[JOY_DOWN] = true; - } - for (int i=0; i<6; i++) - { - if (SDL_JoystickGetButton(joypad, i) == 1) - { - joy[JOY_BTN0 + i] = true; - } - } + joystick->update(); } // Events @@ -690,15 +621,15 @@ void Game::handleInput() Being::Direction Direction = Being::DIR_NONE; // Translate pressed keys to movement and direction - if (keys[SDLK_UP] || keys[SDLK_KP8] || joy[JOY_UP]) + if (keys[SDLK_UP] || keys[SDLK_KP8] || joystick && joystick->isUp()) { Direction = Being::NORTH; } - if (keys[SDLK_DOWN] || keys[SDLK_KP2] || joy[JOY_DOWN]) + if (keys[SDLK_DOWN] || keys[SDLK_KP2] || joystick && joystick->isDown()) { Direction = Being::SOUTH; } - if (keys[SDLK_LEFT] || keys[SDLK_KP4] || joy[JOY_LEFT]) + if (keys[SDLK_LEFT] || keys[SDLK_KP4] || joystick && joystick->isLeft()) { // Allow diagonal walking // TODO: Make this nicer, once we got a bitfield for directions @@ -709,7 +640,7 @@ void Game::handleInput() else Direction = Being::WEST; } - if (keys[SDLK_RIGHT] || keys[SDLK_KP6] || joy[JOY_RIGHT]) + if (keys[SDLK_RIGHT] || keys[SDLK_KP6] || joystick && joystick->isRight()) { // Allow diagonal walking // TODO: Make this nicer, once we got a bitfield for directions @@ -740,7 +671,8 @@ void Game::handleInput() player_node->walk(Direction); // Attacking monsters - if (keys[SDLK_LCTRL] || keys[SDLK_RCTRL] || joy[JOY_BTN0]) + if (keys[SDLK_LCTRL] || keys[SDLK_RCTRL] || + joystick && joystick->buttonPressed(0)) { Being *target = NULL; bool newTarget = keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT]; @@ -780,18 +712,21 @@ void Game::handleInput() player_node->attack(target, newTarget); } - if (joy[JOY_BTN1]) + if (joystick) { - FloorItem *item = floorItemManager->findByCoordinates( - player_node->x, player_node->y); + if (joystick->buttonPressed(1)) + { + FloorItem *item = floorItemManager->findByCoordinates( + player_node->x, player_node->y); - if (item) - player_node->pickUp(item); - } - else if (joy[JOY_BTN2] && action_time) - { - player_node->toggleSit(); - action_time = false; + if (item) + player_node->pickUp(item); + } + else if (joystick->buttonPressed(2) && action_time) + { + player_node->toggleSit(); + action_time = false; + } } } } diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp index c47d12da..c73d1de0 100644 --- a/src/gui/setup.cpp +++ b/src/gui/setup.cpp @@ -38,15 +38,14 @@ #include "../configuration.h" #include "../graphics.h" +#include "../joystick.h" #include "../log.h" #include "../main.h" #include "../sound.h" extern Graphics *graphics; -extern SDL_Joystick *joypad; - -extern SDL_Joystick *joypad; +extern Joystick *joystick; extern Window *statusWindow; extern Window *minimap; @@ -100,8 +99,7 @@ std::string ModeListModel::getElementAt(int i) Setup::Setup(): - Window("Setup"), mCalibrating(false), leftTolerance(0), rightTolerance(0), - upTolerance(0), downTolerance(0) + Window("Setup") { modeListModel = new ModeListModel(); modeList = new ListBox(modeListModel); @@ -268,27 +266,20 @@ void Setup::action(const std::string &eventId) config.setValue("customcursor", customCursorCheckBox->isMarked() ? 1 : 0); } - else if (eventId == "calibrate" && joypad != NULL) + else if (eventId == "calibrate" && joystick != NULL) { - if (mCalibrating) + if (joystick->isCalibrating()) { calibrateButton->setCaption("Calibrate"); calibrateLabel->setCaption("Press the button to start calibration"); - config.setValue("leftTolerance", leftTolerance); - config.setValue("rightTolerance", rightTolerance); - config.setValue("upTolerance", upTolerance); - config.setValue("downTolerance", downTolerance); + joystick->finishCalibration(); } else { calibrateButton->setCaption("Stop"); - calibrateLabel->setCaption("Rotate the stick"); - leftTolerance = 0; - rightTolerance = 0; - upTolerance = 0; - downTolerance = 0; + calibrateLabel->setCaption("Rotate the stick"); + joystick->startCalibration(); } - mCalibrating = !mCalibrating; } else if (eventId == "apply") { @@ -406,31 +397,3 @@ void Setup::action(const std::string &eventId) skillDialog->resetToDefaultSize(); } } - -void Setup::logic() -{ - Window::logic(); - if (mCalibrating) - { - SDL_JoystickUpdate(); - int position = SDL_JoystickGetAxis(joypad, 0); - if (position > rightTolerance) - { - rightTolerance = position; - } - else if (position < leftTolerance) - { - leftTolerance = position; - } - - position = SDL_JoystickGetAxis(joypad, 1); - if (position > downTolerance) - { - downTolerance = position; - } - else if (position < upTolerance) - { - upTolerance = position; - } - } -} diff --git a/src/gui/setup.h b/src/gui/setup.h index 0b62fee6..91f181d5 100644 --- a/src/gui/setup.h +++ b/src/gui/setup.h @@ -88,8 +88,6 @@ class Setup : public Window, public gcn::ActionListener */ void action(const std::string& eventId); - - void logic(); private: ModeListModel *modeListModel; @@ -117,8 +115,6 @@ class Setup : public Window, public gcn::ActionListener bool openGLEnabled; bool customCursorEnabled; bool soundEnabled; - bool mCalibrating; - int leftTolerance, rightTolerance, upTolerance, downTolerance; }; #endif diff --git a/src/joystick.cpp b/src/joystick.cpp new file mode 100644 index 00000000..bb6e887b --- /dev/null +++ b/src/joystick.cpp @@ -0,0 +1,163 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "joystick.h" + +#include "configuration.h" +#include "log.h" + +int Joystick::joystickCount = 0; + +void Joystick::init() +{ + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + //SDL_JoystickEventState(SDL_ENABLE); + joystickCount = SDL_NumJoysticks(); + logger->log("%i joysticks/gamepads found", joystickCount); + for (int i = 0; i < joystickCount; i++) + logger->log("- %s", SDL_JoystickName(i)); +} + +Joystick::Joystick(int no): + mDirection(0), mCalibrating(false) +{ + // TODO Bail out here? + if (no > joystickCount) + return; + + mJoystick = SDL_JoystickOpen(no); + + // TODO Bail out! + if (!mJoystick) + { + logger->log("Couldn't open joystick: %s", SDL_GetError()); + return; + } + + logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick)); + logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick)); + logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick)); + logger->log("Buttons: %i", SDL_JoystickNumButtons(mJoystick)); + + mUpTolerance = (int)config.getValue("upTolerance", 100); + mDownTolerance = (int)config.getValue("downTolerance", 100); + mLeftTolerance = (int)config.getValue("leftTolerance", 100); + mRightTolerance = (int)config.getValue("rightTolerance", 100); +} + +Joystick::~Joystick() +{ + SDL_JoystickClose(mJoystick); +} + +void Joystick::update() +{ + mDirection = 0; + SDL_JoystickUpdate(); + + // When calibrating, don't bother the outside with our state + if (mCalibrating) { + doCalibration(); + 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() +{ + mUpTolerance = 0; + mDownTolerance = 0; + mLeftTolerance = 0; + mRightTolerance = 0; + mCalibrating = true; +} + +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() +{ + config.setValue("leftTolerance", mLeftTolerance); + config.setValue("rightTolerance", mRightTolerance); + config.setValue("upTolerance", mUpTolerance); + config.setValue("downTolerance", mDownTolerance); + mCalibrating = false; +} + +bool Joystick::buttonPressed(unsigned char no) +{ + if (no > MAX_BUTTONS) + return false; + + return mButtons[no]; +} diff --git a/src/joystick.h b/src/joystick.h new file mode 100644 index 00000000..ebdfabeb --- /dev/null +++ b/src/joystick.h @@ -0,0 +1,92 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_JOYSTICK_H +#define _TMW_JOYSTICK_H + +#include <SDL.h> + +class Joystick +{ + public: + /** + * Number of buttons we can handle + */ + static const unsigned char MAX_BUTTONS = 6; + + /** + * Directions, to be used as bitmask values + */ + static const char UP = 1; + static const char DOWN = 2; + static const char LEFT = 4; + static const char RIGHT = 8; + + /** + * Initializes the joystick subsystem + */ + static void init(); + + /** + * Returns the number of available joysticks + */ + static int getNumberOfJoysticks() { return joystickCount; } + + /** + * Constructor, pass the number of the joystick the new object + * should access. + */ + Joystick(int no); + + ~Joystick(); + + /** + * Updates the direction and button information. + */ + void update(); + + void startCalibration(); + void finishCalibration(); + bool isCalibrating() { return mCalibrating; }; + + bool buttonPressed(unsigned char no); + + bool isUp() { return mDirection & UP; }; + bool isDown() { return mDirection & DOWN; }; + bool isLeft() { return mDirection & LEFT; }; + bool isRight() { return mDirection & RIGHT; }; + + protected: + unsigned char mDirection; + bool mButtons[MAX_BUTTONS]; + SDL_Joystick *mJoystick; + + int mUpTolerance, mDownTolerance, mLeftTolerance, mRightTolerance; + bool mCalibrating; + + static int joystickCount; + + void doCalibration(); +}; + +#endif |