diff --git a/Aquaria/AquariaMenuItem.cpp b/Aquaria/AquariaMenuItem.cpp index cdadcef..8b04b90 100644 --- a/Aquaria/AquariaMenuItem.cpp +++ b/Aquaria/AquariaMenuItem.cpp @@ -116,8 +116,16 @@ void AquariaGuiElement::updateMovement(float dt) if (guiMoveTimer==0) { Direction dir = DIR_NONE; - Vector p = core->joystick.position; - if (!p.isLength2DIn(0.4)) + Vector p; + for(size_t i = 0; i < core->joysticks.size(); ++i) + if(Joystick *j = core->joysticks[i]) + if(j->isEnabled()) + { + p = core->joysticks[i]->position; + if(!p.isLength2DIn(0.4f)) + break; + } + if (!p.isLength2DIn(0.4f)) { if (fabsf(p.x) > fabsf(p.y)) { @@ -346,10 +354,20 @@ bool AquariaSlider::doSliderInput(float dt) float inputAmount; // How much to adjust by? + Vector jpos; + for(size_t i = 0; i < core->joysticks.size(); ++i) + if(Joystick *j = core->joysticks[i]) + if(j->isEnabled()) + { + jpos = core->joysticks[i]->position; + if(fabsf(jpos.x) > SLIDER_JOY_THRESHOLD) + break; + } + StateObject *obj = dsq->getTopStateObject(); - if (core->joystick.position.x <= -SLIDER_JOY_THRESHOLD) + if (jpos.x <= -SLIDER_JOY_THRESHOLD) inputAmount = -0.1f; - else if (core->joystick.position.x >= SLIDER_JOY_THRESHOLD) + else if (jpos.x >= SLIDER_JOY_THRESHOLD) inputAmount = +0.1f; else if (obj && obj->isActing(ACTION_MENULEFT)) inputAmount = -0.1f; diff --git a/Aquaria/Avatar.cpp b/Aquaria/Avatar.cpp index 0dacce2..2640c0c 100644 --- a/Aquaria/Avatar.cpp +++ b/Aquaria/Avatar.cpp @@ -187,16 +187,23 @@ Vector Avatar::getAim() Vector d; if (dsq->inputMode == INPUT_JOYSTICK) { - if (!core->joystick.rightStick.isZero()) - { - d = core->joystick.rightStick * 300; - d.z = 1; - } - else - { - d = core->joystick.position * 300; - d.z = 0; - } + for(size_t i = 0; i < core->joysticks.size(); ++i) + if(Joystick *j = core->joysticks[i]) + if(j->isEnabled() && !j->rightStick.isZero()) + { + d = j->rightStick * 300; + d.z = 1; + break; + } + + if(d.isZero()) + for(size_t i = 0; i < core->joysticks.size(); ++i) + if(Joystick *j = core->joysticks[i]) + if(j->isEnabled() && !j->position.isZero()) + { + d = j->position * 300; + break; + } } else if (dsq->inputMode == INPUT_KEYBOARD) { @@ -1761,7 +1768,15 @@ void Avatar::updateSingingInterface(float dt) { if (dsq->inputMode == INPUT_JOYSTICK) { - Vector d = dsq->joystick.position; + Vector d; + for(size_t i = 0; i < core->joysticks.size(); ++i) + if(Joystick *j = core->joysticks[i]) + if(j->isEnabled()) + { + d = j->position; + if(!d.isZero()) + break; + } if (d.isLength2DIn(JOYSTICK_NOTE_THRESHOLD)) { diff --git a/BBGE/ActionMapper.cpp b/BBGE/ActionMapper.cpp index 9ee8fe5..57e9be8 100644 --- a/BBGE/ActionMapper.cpp +++ b/BBGE/ActionMapper.cpp @@ -200,8 +200,10 @@ bool ActionMapper::getKeyState(int k) int v = k - JOY_BUTTON_0; for(size_t i = 0; i < core->joysticks.size(); ++i) - if( ((keyState = core->joysticks[i]->getButton(v))) ) - break; + if(Joystick *j = core->joysticks[i]) + if(j->isEnabled()) + if( ((keyState = j->getButton(v))) ) + break; } return keyState; diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index 00cdc2d..384f598 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -188,17 +188,6 @@ void Core::toggleScreenMode(int t) sound->resume(); } -void Core::updateCursorFromJoystick(float dt, int spd) -{ - - - core->mouse.position += joystick.position*dt*spd; - - - - doMouseConstraint(); -} - void Core::setWindowCaption(const std::string &caption, const std::string &icon) { #ifndef BBGE_BUILD_SDL2 @@ -712,7 +701,7 @@ bool Core::getKeyState(int k) return k > 0 && k < KEY_MAXARRAY ? keys[k] : 0; } -bool Core::initJoystickLibrary() +void Core::initJoystickLibrary() { #ifdef BBGE_BUILD_SDL2 @@ -721,11 +710,35 @@ bool Core::initJoystickLibrary() SDL_InitSubSystem(SDL_INIT_JOYSTICK); #endif - joystick.init(0); + detectJoysticks(); +} - joystickEnabled = true; +void Core::clearJoysticks() +{ + for(size_t i = 0; i < joysticks.size(); ++i) + delete joysticks[i]; + joysticks.clear(); +} - return true; +void Core::detectJoysticks() +{ + clearJoysticks(); + + std::ostringstream os; + const unsigned n = SDL_NumJoysticks(); + os << "Found [" << n << "] joysticks"; + debugLog(os.str()); + + for(unsigned i = 0; i < n; ++i) + { + Joystick *j = new Joystick; + if(j->init(i)) + joysticks.push_back(j); + else + delete j; + } + + joystickEnabled = !joysticks.empty(); } bool Core::initInputLibrary() @@ -737,8 +750,6 @@ bool Core::initInputLibrary() keys[i] = 0; } - - return true; } @@ -753,7 +764,9 @@ void Core::onUpdate(float dt) core->mouse.lastScrollWheel = core->mouse.scrollWheel; pollEvents(); - joystick.update(dt); + + for(size_t i = 0; i < joysticks.size(); ++i) + joysticks[i]->update(dt); onMouseInput(); @@ -2350,7 +2363,7 @@ void Core::shutdownInputLibrary() void Core::shutdownJoystickLibrary() { if (joystickEnabled) { - joystick.shutdown(); + clearJoysticks(); SDL_QuitSubSystem(SDL_INIT_JOYSTICK); joystickEnabled = false; } diff --git a/BBGE/Core.h b/BBGE/Core.h index b2c6b05..4595771 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -293,8 +293,6 @@ public: int getWindowWidth() { return width; } int getWindowHeight() { return height; } - void updateCursorFromJoystick(float dt, int spd); - unsigned getTicks(); void resetGraphics(int w, int h, int fullscreen=-1, int vsync=-1, int bpp=-1); @@ -521,13 +519,15 @@ protected: float baseCullRadius; bool initSoundLibrary(const std::string &defaultDevice); bool initInputLibrary(); - bool initJoystickLibrary(); + void initJoystickLibrary(); bool initGraphicsLibrary(int w, int h, bool fullscreen, int vsync, int bpp, bool recreate=true); void shutdownInputLibrary(); void shutdownJoystickLibrary(); void shutdownGraphicsLibrary(bool kill=true); void shutdownSoundLibrary(); + void detectJoysticks(); + void clearJoysticks(); virtual void onJoystickAdded(int deviceID); virtual void onJoystickRemoved(int instanceID); @@ -565,8 +565,6 @@ protected: public: std::vector joysticks; - - Joystick joystick; // TEMP: TO GET IT TO COMPILE }; extern Core *core; diff --git a/BBGE/Joystick.cpp b/BBGE/Joystick.cpp index c131e88..b64ac61 100644 --- a/BBGE/Joystick.cpp +++ b/BBGE/Joystick.cpp @@ -26,6 +26,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Joystick.h" #include "Core.h" +unsigned Joystick::GetNumJoysticks() +{ + return SDL_NumJoysticks(); +} Joystick::Joystick() { @@ -45,70 +49,61 @@ Joystick::Joystick() s1ay = 1; s2ax = 4; s2ay = 3; + + enabled = true; } -void Joystick::init(int stick) +bool Joystick::init(int stick) { - std::ostringstream os; - stickIndex = stick; - const int numJoy = SDL_NumJoysticks(); - os << "Found [" << numJoy << "] joysticks"; - debugLog(os.str()); - if (numJoy > stick) + #ifdef BBGE_BUILD_SDL2 + if (SDL_IsGameController(stick)) + { + sdl_controller = SDL_GameControllerOpen(stick); + if (sdl_controller) + sdl_joy = SDL_GameControllerGetJoystick(sdl_controller); + } + if (!sdl_joy) + sdl_joy = SDL_JoystickOpen(stick); + if (sdl_joy && SDL_JoystickIsHaptic(sdl_joy)) + { + sdl_haptic = SDL_HapticOpenFromJoystick(sdl_joy); + bool rumbleok = false; + if (sdl_haptic && SDL_HapticRumbleSupported(sdl_haptic)) + rumbleok = (SDL_HapticRumbleInit(sdl_haptic) == 0); + if (!rumbleok) + { + SDL_HapticClose(sdl_haptic); + sdl_haptic = NULL; + } + } + #endif + + if (!sdl_joy) + sdl_joy = SDL_JoystickOpen(stick); + + if (sdl_joy) { #ifdef BBGE_BUILD_SDL2 - if (SDL_IsGameController(stick)) - { - sdl_controller = SDL_GameControllerOpen(stick); - if (sdl_controller) - sdl_joy = SDL_GameControllerGetJoystick(sdl_controller); - } - if (!sdl_joy) - sdl_joy = SDL_JoystickOpen(stick); - if (sdl_joy && SDL_JoystickIsHaptic(sdl_joy)) - { - sdl_haptic = SDL_HapticOpenFromJoystick(sdl_joy); - bool rumbleok = false; - if (sdl_haptic && SDL_HapticRumbleSupported(sdl_haptic)) - rumbleok = (SDL_HapticRumbleInit(sdl_haptic) == 0); - if (!rumbleok) - { - SDL_HapticClose(sdl_haptic); - sdl_haptic = NULL; - } - } + debugLog(std::string("Initialized Joystick [") + SDL_JoystickName(sdl_joy) + "]"); + if (sdl_controller) + debugLog("Joystick is a Game Controller"); + if (sdl_haptic) + debugLog("Joystick has force feedback support"); + instanceID = SDL_JoystickInstanceID(sdl_joy); + #else + debugLog(std::string("Initialized Joystick [") + SDL_JoystickName(stick)) + std::string("]")); + instanceID = SDL_JoystickIndex(sdl_joy); #endif - if (!sdl_joy) - sdl_joy = SDL_JoystickOpen(stick); - - if (sdl_joy) - { - #ifdef BBGE_BUILD_SDL2 - debugLog(std::string("Initialized Joystick [") + SDL_JoystickName(sdl_joy) + "]"); - if (sdl_controller) - debugLog("Joystick is a Game Controller"); - if (sdl_haptic) - debugLog("Joystick has force feedback support"); - instanceID = SDL_JoystickInstanceID(sdl_joy); - #else - debugLog(std::string("Initialized Joystick [") + SDL_JoystickName(stick)) + std::string("]")); - instanceID = SDL_JoystickIndex(sdl_joy); - #endif - } - else - { - std::ostringstream os; - os << "Failed to init Joystick [" << stick << "]"; - debugLog(os.str()); - } - } - else - { - debugLog("Not enough Joystick(s) found"); + return true; } + + std::ostringstream os; + os << "Failed to init Joystick [" << stick << "]"; + debugLog(os.str()); + return false; } void Joystick::shutdown() @@ -241,13 +236,9 @@ void Joystick::update(float dt) rightStick.y = yaxis2/32768.0f; #endif - - calibrate(position, deadZone1); - calibrate(rightStick, deadZone2); - buttonBitmask = 0; #ifdef BBGE_BUILD_SDL2 diff --git a/BBGE/Joystick.h b/BBGE/Joystick.h index 0efb6f5..59e4f87 100644 --- a/BBGE/Joystick.h +++ b/BBGE/Joystick.h @@ -16,8 +16,10 @@ class Joystick { public: + static unsigned GetNumJoysticks(); + Joystick(); - void init(int stick=0); + bool init(int stick=0); void shutdown(); //Ranges from 0 to 1 (full speed). void rumble(float leftMotor, float rightMotor, float time); @@ -32,12 +34,14 @@ public: bool getButton(unsigned id) const { return !!(buttonBitmask & (1u << id)); } int getIndex() const { return stickIndex; } int getInstanceID() const { return instanceID; } + inline bool isEnabled() const { return enabled; } Vector rightStick; int s1ax, s1ay, s2ax, s2ay; private: + bool enabled; int stickIndex; int instanceID; unsigned buttonBitmask; // FIXME: this should go