From 501d5b6f288c21a78f97966b0b9004b33336365d Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 26 Oct 2008 18:05:08 +0000 Subject: minor engine function cleanup --- src/client/joystick.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/client/joystick.cc') diff --git a/src/client/joystick.cc b/src/client/joystick.cc index 5f09280..486542e 100644 --- a/src/client/joystick.cc +++ b/src/client/joystick.cc @@ -15,7 +15,6 @@ namespace client { core::Cvar *input_joystick = 0; - int current_joystick_number = 0; SDL_Joystick *current_joystick = 0; @@ -56,6 +55,49 @@ void Joystick::shutdown() SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } +void Joystick::list() +{ + if (current_joystick) { + SDL_JoystickEventState(SDL_IGNORE); + SDL_JoystickClose(current_joystick); + current_joystick = 0; + } + /* + // reset makes glorious segfaults + //SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + //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); + } + } + } + + current_joystick_number = (int) input_joystick->value(); + if ((current_joystick_number < 1) || (current_joystick_number > nbjoysticks)) { + current_joystick_number = 0; + } + (*input_joystick) = (float) current_joystick_number; + + 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); + } +} + bool Joystick::is_enabled() { return (current_joystick != 0); @@ -72,6 +114,10 @@ void Joystick::frame() } current_joystick_number = (int) input_joystick->value(); + if ((current_joystick_number < 1) || (current_joystick_number > SDL_NumJoysticks())) { + current_joystick_number = 0; + } + (*input_joystick) = (float) current_joystick_number; if (current_joystick_number) { current_joystick = SDL_JoystickOpen(current_joystick_number -1); -- cgit v1.2.3