Introducing the new DeviceManager input API...

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3294 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2009-03-23 18:38:06 +00:00
parent 63a94df516
commit 098ce5b32e
11 changed files with 273 additions and 77 deletions

View File

@ -24,6 +24,8 @@
953F038C0F6C880D00C77FE2 /* state_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 953F038B0F6C880D00C77FE2 /* state_manager.cpp */; };
95A117350F77D7BE00B18B3D /* sdl_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95A117330F77D7BE00B18B3D /* sdl_manager.cpp */; };
95A1182C0F77EA3100B18B3D /* input_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95A1182A0F77EA3100B18B3D /* input_manager.cpp */; };
95A1184B0F77FC3900B18B3D /* input_device.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95A1184A0F77FC3900B18B3D /* input_device.cpp */; };
95A1187B0F78024E00B18B3D /* device_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95A1187A0F78024E00B18B3D /* device_manager.cpp */; };
95C1E3F20F699079005D33E6 /* race_gui.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C1E3F10F699079005D33E6 /* race_gui.cpp */; };
95C1E4000F699427005D33E6 /* font.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C1E3FF0F699427005D33E6 /* font.cpp */; };
95C2B1FB0F296546000D3E5D /* music_information.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2AC280F296540000D3E5D /* music_information.cpp */; };
@ -253,7 +255,6 @@
9505577B0F696A900056E88C /* engine.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = engine.hpp; path = ../../gui/engine.hpp; sourceTree = SOURCE_ROOT; };
9505577C0F696A900056E88C /* my_button.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = my_button.cpp; path = ../../gui/my_button.cpp; sourceTree = SOURCE_ROOT; };
9505577D0F696A900056E88C /* my_button.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = my_button.hpp; path = ../../gui/my_button.hpp; sourceTree = SOURCE_ROOT; };
9505577E0F696A900056E88C /* ptr_vector.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ptr_vector.hpp; path = ../../gui/ptr_vector.hpp; sourceTree = SOURCE_ROOT; };
9505577F0F696A900056E88C /* screen.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = screen.cpp; path = ../../gui/screen.cpp; sourceTree = SOURCE_ROOT; };
950557800F696A900056E88C /* screen.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = screen.hpp; path = ../../gui/screen.hpp; sourceTree = SOURCE_ROOT; };
950557810F696A900056E88C /* screen_loader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = screen_loader.cpp; path = ../../gui/screen_loader.cpp; sourceTree = SOURCE_ROOT; };
@ -275,6 +276,11 @@
95A118290F77EA3100B18B3D /* input.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = input.hpp; path = ../../input/input.hpp; sourceTree = SOURCE_ROOT; };
95A1182A0F77EA3100B18B3D /* input_manager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = input_manager.cpp; path = ../../input/input_manager.cpp; sourceTree = SOURCE_ROOT; };
95A1182B0F77EA3100B18B3D /* input_manager.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = input_manager.hpp; path = ../../input/input_manager.hpp; sourceTree = SOURCE_ROOT; };
95A1184A0F77FC3900B18B3D /* input_device.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = input_device.cpp; path = games/supertuxkart/src/input/input_device.cpp; sourceTree = SYSTEM_DEVELOPER_DIR; };
95A1184C0F77FC8800B18B3D /* input_device.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = input_device.hpp; path = games/supertuxkart/src/input/input_device.hpp; sourceTree = SYSTEM_DEVELOPER_DIR; };
95A1187A0F78024E00B18B3D /* device_manager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = device_manager.cpp; path = games/supertuxkart/src/input/device_manager.cpp; sourceTree = SYSTEM_DEVELOPER_DIR; };
95A1187C0F78026D00B18B3D /* device_manager.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = device_manager.hpp; path = games/supertuxkart/src/input/device_manager.hpp; sourceTree = SYSTEM_DEVELOPER_DIR; };
95A1187D0F78033700B18B3D /* ptr_vector.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ptr_vector.hpp; path = games/supertuxkart/src/ptr_vector.hpp; sourceTree = SYSTEM_DEVELOPER_DIR; };
95C1E3EB0F698F23005D33E6 /* race_gui.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = race_gui.hpp; path = ../../gui/race_gui.hpp; sourceTree = SOURCE_ROOT; };
95C1E3F10F699079005D33E6 /* race_gui.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = race_gui.cpp; path = ../../gui/race_gui.cpp; sourceTree = SOURCE_ROOT; };
95C1E3FF0F699427005D33E6 /* font.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = font.cpp; path = ../../gui/font.cpp; sourceTree = SOURCE_ROOT; };
@ -1184,7 +1190,6 @@
9505577D0F696A900056E88C /* my_button.hpp */,
95C1E3FF0F699427005D33E6 /* font.cpp */,
95C1E4020F69943D005D33E6 /* font.hpp */,
9505577E0F696A900056E88C /* ptr_vector.hpp */,
9505577F0F696A900056E88C /* screen.cpp */,
950557800F696A900056E88C /* screen.hpp */,
950557810F696A900056E88C /* screen_loader.cpp */,
@ -1221,6 +1226,10 @@
95A118290F77EA3100B18B3D /* input.hpp */,
95A1182A0F77EA3100B18B3D /* input_manager.cpp */,
95A1182B0F77EA3100B18B3D /* input_manager.hpp */,
95A1184A0F77FC3900B18B3D /* input_device.cpp */,
95A1184C0F77FC8800B18B3D /* input_device.hpp */,
95A1187A0F78024E00B18B3D /* device_manager.cpp */,
95A1187C0F78026D00B18B3D /* device_manager.hpp */,
);
name = input;
path = games/supertuxkart/src/input;
@ -1275,6 +1284,7 @@
95C2B1430F296545000D3E5D /* material_manager.hpp */,
95C2B17A0F296545000D3E5D /* no_copy.hpp */,
95C2B18A0F296545000D3E5D /* player.hpp */,
95A1187D0F78033700B18B3D /* ptr_vector.hpp */,
95C2B1930F296545000D3E5D /* race_manager.cpp */,
95C2B1940F296545000D3E5D /* race_manager.hpp */,
95C2B1BF0F296545000D3E5D /* static_ssg.cpp */,
@ -2765,6 +2775,8 @@
953F038C0F6C880D00C77FE2 /* state_manager.cpp in Sources */,
95A117350F77D7BE00B18B3D /* sdl_manager.cpp in Sources */,
95A1182C0F77EA3100B18B3D /* input_manager.cpp in Sources */,
95A1184B0F77FC3900B18B3D /* input_device.cpp in Sources */,
95A1187B0F78024E00B18B3D /* device_manager.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,42 @@
#include "input/device_manager.hpp"
DeviceManager::DeviceManager()
{
m_keyboard_amount = 0;
m_gamepad_amount = 0;
}
void DeviceManager::add(KeyboardDevice* d)
{
m_keyboards.push_back(d);
m_keyboard_amount = m_keyboards.size();
}
void DeviceManager::add(GamePadDevice* d)
{
m_gamepads.push_back(d);
m_gamepad_amount = m_gamepads.size();
}
// TODO
bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int id0, int id1, int id2,
int* player /* out */, PlayerAction* action /* out */ )
{
// TODO - auto-detect player ID from device
*player = 0;
if(type == Input::IT_KEYBOARD)
{
for(unsigned int n=0; n<m_keyboard_amount; n++)
{
if( m_keyboards[n].hasBinding(id0, id1, id2, action) ) return true;
}
return false;
}
else
{
return false;
}
}

View File

@ -0,0 +1,28 @@
#ifndef DEVICE_MANAGER_HPP
#define DEVICE_MANAGER_HPP
#include "ptr_vector.hpp"
#include "input/input_device.hpp"
class DeviceManager
{
ptr_vector<KeyboardDevice, HOLD> m_keyboards;
ptr_vector<GamePadDevice, HOLD> m_gamepads;
unsigned int m_keyboard_amount;
unsigned int m_gamepad_amount;
public:
DeviceManager();
void add(KeyboardDevice* d);
void add(GamePadDevice* d);
/** Given some input, finds to which device it belongs and, using the corresponding device object,
maps this input to the corresponding player and game action. returns false if player/action could not be set */
bool mapInputToPlayerAndAction( Input::InputType type, int id0, int id1, int id2,
int* player /* out */, PlayerAction* action /* out */ );
};
#endif

View File

@ -22,6 +22,11 @@
#include <string>
const int DEADZONE_MOUSE = 150;
const int DEADZONE_MOUSE_SENSE = 200;
const int DEADZONE_JOYSTICK = 2000;
const int MULTIPLIER_MOUSE = 750;
struct Input
{
enum AxisDirection {
@ -85,9 +90,6 @@ struct Input
}
};
//FIXME: KartAction and Gameaction should probably go in their own files.
//When adding any action at the beginning or at the end, remember to update
//the KA_FIRST and/or KA_LAST constants.
enum PlayerAction
{
PA_FIRST = -1,

View File

@ -0,0 +1,78 @@
#include "input/input_device.hpp"
InputDevice::InputDevice()
{
for(int n=0; n<PA_COUNT; n++)
{
m_bindings[n].id0 = 0;
m_bindings[n].id1 = 0;
m_bindings[n].id2 = 0;
}
}
KeyboardDevice::KeyboardDevice()
{
}
void KeyboardDevice::loadDefaults()
{
m_bindings[PA_NITRO].id0 = SDLK_SPACE;
m_bindings[PA_ACCEL].id0 = SDLK_UP;
m_bindings[PA_BRAKE].id0 = SDLK_DOWN;
m_bindings[PA_LEFT].id0 = SDLK_LEFT;
m_bindings[PA_RIGHT].id0 = SDLK_RIGHT;
m_bindings[PA_DRIFT].id0 = SDLK_LSHIFT;
m_bindings[PA_RESCUE].id0 = SDLK_ESCAPE;
m_bindings[PA_FIRE].id0 = SDLK_LALT;
m_bindings[PA_LOOK_BACK].id0 = SDLK_b;
}
/** checks if this key belongs to this belongs. if yes, sets action and returns true; otherwise returns false */
bool KeyboardDevice::hasBinding(int id0, int id1, int id2, PlayerAction* action /* out */)
{
for(int n=0; n<PA_COUNT; n++)
{
if(m_bindings[n].id0 == id0)
{
*action = (PlayerAction)n;
return true;
}
}// next device
return false;
}
// -----------------------------------------------------------------------------
/** Constructor for GamePadDevice.
* \param sdlIndex Index of stick.
*/
GamePadDevice::GamePadDevice(int sdlIndex)
{
m_type = DT_GAMEPAD;
m_sdlJoystick = SDL_JoystickOpen(sdlIndex);
m_id = SDL_JoystickName(sdlIndex);
const int count = SDL_JoystickNumAxes(m_sdlJoystick);
m_prevAxisDirections = new Input::AxisDirection[count];
for (int i = 0; i < count; i++)
m_prevAxisDirections[i] = Input::AD_NEUTRAL;
m_deadzone = DEADZONE_JOYSTICK;
m_index = -1;
} // GamePadDevice
// -----------------------------------------------------------------------------
/** Destructor for GamePadDevice.
*/
GamePadDevice::~GamePadDevice()
{
delete m_prevAxisDirections;
SDL_JoystickClose(m_sdlJoystick);
} // ~GamePadDevice

View File

@ -0,0 +1,57 @@
#ifndef INPUT_DEVICE_HPP
#define INPUT_DEVICE_HPP
#include <SDL/SDL.h>
#include <string>
#include "input/input.hpp"
enum DeviceType
{
DT_KEYBOARD,
DT_GAMEPAD
};
struct KeyBinding
{
int id0, id1, id2;
};
class InputDevice
{
protected:
DeviceType m_type;
KeyBinding m_bindings[PA_COUNT];
public:
InputDevice();
DeviceType getType() const { return m_type; };
};
class KeyboardDevice : public InputDevice
{
public:
KeyboardDevice();
/** checks if this key belongs to this belongs. if yes, sets action and returns true; otherwise returns false */
bool hasBinding(int id0, int id1, int id2, PlayerAction* action /* out */);
void loadDefaults();
};
class GamePadDevice : public InputDevice
{
public:
SDL_Joystick *m_sdlJoystick;
std::string m_id;
int m_deadzone;
int m_index;
Input::AxisDirection *m_prevAxisDirections;
GamePadDevice(int sdlIndex);
~GamePadDevice();
}; // Stickinfo
#endif

View File

@ -18,6 +18,7 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "input/input_manager.hpp"
#include "input/device_manager.hpp"
#include <map>
#include <vector>
@ -29,7 +30,7 @@
#include <SDL/SDL.h>
#include "input/input.hpp"
#include "actionmap.hpp"
//#include "actionmap.hpp"
#include "user_config.hpp"
#include "main_loop.hpp"
@ -44,44 +45,49 @@
#include "gui/race_gui.hpp"
#include "sdl_manager.hpp"
#define DEADZONE_MOUSE 150
#define DEADZONE_MOUSE_SENSE 200
#define DEADZONE_JOYSTICK 2000
#define MULTIPLIER_MOUSE 750
InputManager *input_manager;
//-----------------------------------------------------------------------------
/** Initialise SDL.
*/
InputManager::InputManager()
: m_sensed_input(0), m_stick_infos(0),
: m_sensed_input(0),
m_mode(BOOTSTRAP), m_mouse_val_x(0), m_mouse_val_y(0)
{
initStickInfos();
m_device_manager = new DeviceManager();
KeyboardDevice* default_device = new KeyboardDevice();
default_device->loadDefaults();
m_device_manager->add( default_device );
initGamePadDevices();
}
// -----------------------------------------------------------------------------
/** Initialises joystick/gamepad info.
*/
void InputManager::initStickInfos()
void InputManager::initGamePadDevices()
{
int nextIndex = 0;
// Prepare a list of connected joysticks.
const int numSticks = SDL_NumJoysticks();
m_stick_infos = new StickInfo *[numSticks];
std::vector<StickInfo *> *si = new std::vector<StickInfo *>;
// TODO - init gamepad devices
/*
m_stick_infos = new GamePadDevice *[numSticks];
std::vector<GamePadDevice *> *si = new std::vector<GamePadDevice *>;
for (int i = 0; i < numSticks; i++)
si->push_back(m_stick_infos[i] = new StickInfo(i));
si->push_back(m_stick_infos[i] = new GamePadDevice(i));
// Get the list of known configs and make a copy of it.
std::vector<UserConfig::StickConfig *> *sc
= new std::vector<UserConfig::StickConfig *>(*user_config->getStickConfigs());
bool match;
std::vector<StickInfo *>::iterator si_ite = si->begin();
std::vector<GamePadDevice *>::iterator si_ite = si->begin();
// FIXME: Visual Studio triggers an exception (in debug mode) when si
// becomes empty (incompatible iterators). This is apparently caused
@ -149,18 +155,22 @@ void InputManager::initStickInfos()
}
delete si;
} // initStickInfos
*/
} // initGamePadDevices
//-----------------------------------------------------------------------------
/** Destructor. Frees all data structures.
*/
InputManager::~InputManager()
{
const int NUM_STICKS = SDL_NumJoysticks();
for (int i = 0; i < NUM_STICKS; i++)
delete m_stick_infos[i];
delete [] m_stick_infos;
delete m_device_manager;
//const int NUM_STICKS = SDL_NumJoysticks();
//for (int i = 0; i < NUM_STICKS; i++)
// delete m_stick_infos[i];
//delete [] m_stick_infos;
// FIXME LEAK: delete m_action_map if defined
@ -344,7 +354,7 @@ void InputManager::handleGameAction(GameAction ga, int value)
}
*/
/*
// TODO
bool mapToPlayerAndAction( Input::InputType type, int id0, int id1, int id2, int* player, PlayerAction* action )
{
@ -384,7 +394,7 @@ bool mapToPlayerAndAction( Input::InputType type, int id0, int id1, int id2, int
return true;
}
*/
//-----------------------------------------------------------------------------
/** Handles the conversion from some input to a GameAction and its distribution
* to the currently active menu.
@ -442,7 +452,7 @@ void InputManager::input(Input::InputType type, int id0, int id1, int id2,
int player;
PlayerAction action;
const bool action_found = mapToPlayerAndAction( type, id0, id1, id2, &player, &action );
const bool action_found = m_device_manager->mapInputToPlayerAndAction( type, id0, id1, id2, &player, &action );
//GameAction ga = m_action_map->getEntry(type, id0, id1, id2);
#if 0 // TODO - input sensing
@ -615,6 +625,7 @@ void InputManager::input()
printf("axis motion: which %d axis %d value %d\n",
ev.jaxis.which, ev.jaxis.axis, ev.jaxis.value);
}
/* TODO - bring gamepad back in, with new InputDevice interface
stickIndex = m_stick_infos[ev.jaxis.which]->m_index;
// If the joystick axis exceeds the deadzone report the input.
// In menu mode (mode = MENU = 0) the joystick number is reported
@ -667,17 +678,21 @@ void InputManager::input()
m_stick_infos[ev.jaxis.which]->m_prevAxisDirections[ev.jaxis.axis]
= Input::AD_NEUTRAL;
}
*/
break;
case SDL_JOYBUTTONUP:
/* TODO - bring gamepad back in, with new InputDevice interface
stickIndex = m_stick_infos[ev.jbutton.which]->m_index;
*/
// See the SDL_JOYAXISMOTION case label because of !m_mode thingie.
input(Input::IT_STICKBUTTON, !m_mode ? 0 : stickIndex,
ev.jbutton.button, 0, 0);
break;
case SDL_JOYBUTTONDOWN:
/* TODO - bring gamepad back in, with new InputDevice interface
stickIndex = m_stick_infos[ev.jbutton.which]->m_index;
*/
// See the SDL_JOYAXISMOTION case label because of !m_mode thingie.
input(Input::IT_STICKBUTTON, !m_mode ? 0 : stickIndex,
@ -866,33 +881,3 @@ void InputManager::setMode(InputDriverMode new_mode)
}
}
// -----------------------------------------------------------------------------
/** Constructor for StickInfo.
* \param sdlIndex Index of stick.
*/
InputManager::StickInfo::StickInfo(int sdlIndex)
{
m_sdlJoystick = SDL_JoystickOpen(sdlIndex);
m_id = SDL_JoystickName(sdlIndex);
const int count = SDL_JoystickNumAxes(m_sdlJoystick);
m_prevAxisDirections = new Input::AxisDirection[count];
for (int i = 0; i < count; i++)
m_prevAxisDirections[i] = Input::AD_NEUTRAL;
m_deadzone = DEADZONE_JOYSTICK;
m_index = -1;
} // StickInfo
// -----------------------------------------------------------------------------
/** Destructor for StickInfo.
*/
InputManager::StickInfo::~StickInfo()
{
delete m_prevAxisDirections;
SDL_JoystickClose(m_sdlJoystick);
} // ~StickInfo

View File

@ -28,9 +28,9 @@
#include "input/input.hpp"
class ActionMap;
class DeviceManager;
/** Class to handle SDL.
/** Class to handle input.
*/
class InputManager
{
@ -45,24 +45,16 @@ public:
};
private:
class StickInfo {
public:
SDL_Joystick *m_sdlJoystick;
std::string m_id;
int m_deadzone;
int m_index;
Input::AxisDirection *m_prevAxisDirections;
StickInfo(int);
~StickInfo();
}; // Stickinfo
Input *m_sensed_input;
DeviceManager *m_device_manager;
/** Stores the maximum sensed input values. This allows to select the
* axis which was pushed the furthest when sensing input. */
int m_max_sensed_input;
Input::InputType m_max_sensed_type;
//ActionMap *m_action_map;
StickInfo **m_stick_infos;
//GamePadDevice **m_stick_infos;
InputDriverMode m_mode;
/* Helper values to store and track the relative mouse movements. If these
@ -78,7 +70,7 @@ private:
public:
InputManager();
~InputManager();
void initStickInfos();
void initGamePadDevices();
void input();
void setMode(InputDriverMode);
bool isInMode(InputDriverMode);

View File

@ -38,7 +38,7 @@
#include <plib/ul.h>
#include "stk_config.hpp"
#include "actionmap.hpp"
//#include "actionmap.hpp"
#include "race_manager.hpp"
#include "challenges/unlock_manager.hpp"
#include "io/file_manager.hpp"
@ -1157,7 +1157,7 @@ void UserConfig::clearInput(int playerIndex, KartAction ka)
m_input_map[(GameAction) (GA_FIRST_KARTACTION + playerIndex * KC_COUNT + ka)]
.count = 0;
} // clearInput
#endif
// -----------------------------------------------------------------------------
ActionMap *UserConfig::newActionMap(const int from, const int to)
{
@ -1174,7 +1174,7 @@ ActionMap *UserConfig::newActionMap(const int from, const int to)
return am;
} // newActionMap
#endif
// -----------------------------------------------------------------------------
#if 0 // TODO
ActionMap *UserConfig::newMenuActionMap()

View File

@ -51,7 +51,7 @@
#include "lisp/parser.hpp"
#include "lisp/writer.hpp"
class ActionMap;
//class ActionMap;
struct Input;
/** Class for managing general STK user configuration data. */
@ -176,7 +176,7 @@ private:
/** Creates an ActionMap for the GameAction values of the specified
* range.
*/
ActionMap *newActionMap(const int, const int);
// ActionMap *newActionMap(const int, const int);
/** Sets the Input for the given GameAction. Includes a check for
* duplicates and automatic removing of the other candidate(s).