From 5680cffdd4afdb99d39c644a6c397e670e958848 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 4 Aug 2008 22:32:59 +0000 Subject: initial joystick support, added cl_drawkeypress variable --- src/client/joystick.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/client/joystick.cc (limited to 'src/client/joystick.cc') diff --git a/src/client/joystick.cc b/src/client/joystick.cc new file mode 100644 index 0000000..cf2270f --- /dev/null +++ b/src/client/joystick.cc @@ -0,0 +1,82 @@ +/* + client/joystick.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "SDL/SDL.h" + +#include "auxiliary/functions.h" +#include "client/joystick.h" +#include "core/cvar.h" +#include "sys/sys.h" + +namespace client +{ + +core::Cvar *input_joystick = 0; + +int current_joystick_number = 0; +SDL_Joystick *current_joystick = 0; + +void Joystick::init() +{ + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + + int nbjoysticks = SDL_NumJoysticks(); + if (nbjoysticks) { + for (int i=0; i < nbjoysticks; i++) { + SDL_Joystick *joystick = SDL_JoystickOpen(i); + if (joystick) { + con_print << " joystick " << i+1 << ": " << + SDL_JoystickName(i) << " " << + SDL_JoystickNumAxes(joystick) << " axes " << + SDL_JoystickNumButtons(joystick) << " buttons " << std::endl; + + SDL_JoystickClose(joystick); + } + } + } + + input_joystick = core::Cvar::get("input_joystick", 0.0f, core::Cvar::Archive); + input_joystick->set_info("[int] set joystick number to use"); + + current_joystick_number = 0; + SDL_JoystickEventState(SDL_IGNORE); +} + +void Joystick::shutdown() +{ + if (current_joystick) { + SDL_JoystickEventState(SDL_IGNORE); + SDL_JoystickClose(current_joystick); + current_joystick = 0; + } + + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); +} + +void Joystick::frame() +{ + if (current_joystick_number != (int) input_joystick->value()) + { + if (current_joystick) { + SDL_JoystickEventState(SDL_IGNORE); + SDL_JoystickClose(current_joystick); + current_joystick = 0; + } + + current_joystick_number = (int) input_joystick->value(); + + if (current_joystick_number) { + current_joystick = SDL_JoystickOpen(current_joystick_number -1); + } + + if (current_joystick) { + con_debug << " using joystick " << SDL_JoystickName(current_joystick_number -1) << std::endl; + SDL_JoystickEventState(SDL_ENABLE); + } + } +} + +} -- cgit v1.2.3