From a7c2d054a2ed6331d159a0b738e3971a1265171e Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sat, 25 Jun 2016 23:59:34 +0200 Subject: [PATCH] Begin joystick code refactor; some related cleanups and unused code removal --- Aquaria/AquariaMenuItem.cpp | 6 +---- Aquaria/Avatar.cpp | 33 ------------------------ Aquaria/Avatar.h | 1 - Aquaria/DSQ.cpp | 21 ---------------- Aquaria/DSQ.h | 2 -- Aquaria/Game.cpp | 22 ---------------- Aquaria/UserSettings.cpp | 15 +++-------- BBGE/ActionMapper.cpp | 44 ++------------------------------ BBGE/ActionMapper.h | 37 ++------------------------- BBGE/Core.cpp | 26 +++++++++++++------ BBGE/Core.h | 47 +++++----------------------------- BBGE/Joystick.cpp | 48 +++++++++++------------------------ BBGE/Joystick.h | 50 +++++++++++++++++++++++++++++++++++++ win/vc90/BBGE.vcproj | 4 +++ 14 files changed, 102 insertions(+), 254 deletions(-) create mode 100644 BBGE/Joystick.h diff --git a/Aquaria/AquariaMenuItem.cpp b/Aquaria/AquariaMenuItem.cpp index 8271b90..0f454fc 100644 --- a/Aquaria/AquariaMenuItem.cpp +++ b/Aquaria/AquariaMenuItem.cpp @@ -350,10 +350,6 @@ bool AquariaSlider::doSliderInput(float dt) inputAmount = -0.1f; else if (core->joystick.position.x >= SLIDER_JOY_THRESHOLD) inputAmount = +0.1f; - else if (core->joystick.dpadLeft) - inputAmount = -0.1f; - else if (core->joystick.dpadRight) - inputAmount = +0.1f; else if (obj && obj->isActing(ACTION_MENULEFT)) inputAmount = -0.1f; else if (obj && obj->isActing(ACTION_MENURIGHT)) @@ -675,7 +671,7 @@ void AquariaKeyConfig::onUpdate(float dt) } else { - for (int i = ActionMapper::JOY1_BUTTON_0; i <= ActionMapper::JOY1_BUTTON_16; i++) + for (int i = ActionMapper::JOY1_BUTTON_0; i <= MAX_JOYSTICK_BTN; i++) { if (dsq->game->getKeyState(i)) { diff --git a/Aquaria/Avatar.cpp b/Aquaria/Avatar.cpp index a07fabf..debb5bc 100644 --- a/Aquaria/Avatar.cpp +++ b/Aquaria/Avatar.cpp @@ -5170,38 +5170,6 @@ bool Avatar::canQuickSong() return !isSinging() && !isEntityDead() && isInputEnabled() && quickSongCastDelay <= 0; } -void Avatar::updateJoystick(float dt) -{ - if (canQuickSong()) - { - - if (core->joystick.dpadUp) - { - if (dsq->continuity.hasSong(SONG_ENERGYFORM) && dsq->continuity.form != FORM_ENERGY) - { - quickSongCastDelay = QUICK_SONG_CAST_DELAY; - dsq->continuity.castSong(SONG_ENERGYFORM); - } - } - else if (core->joystick.dpadDown && dsq->continuity.hasSong(SONG_BEASTFORM) && dsq->continuity.form != FORM_BEAST) - { - quickSongCastDelay = QUICK_SONG_CAST_DELAY; - dsq->continuity.castSong(SONG_BEASTFORM); - } - else if (core->joystick.dpadLeft && dsq->continuity.hasSong(SONG_SUNFORM) && dsq->continuity.form != FORM_SUN) - { - quickSongCastDelay = QUICK_SONG_CAST_DELAY; - dsq->continuity.castSong(SONG_SUNFORM); - } - else if (core->joystick.dpadRight && dsq->continuity.hasSong(SONG_NATUREFORM) && dsq->continuity.form != FORM_NATURE) - { - quickSongCastDelay = QUICK_SONG_CAST_DELAY; - dsq->continuity.castSong(SONG_NATUREFORM); - } - - } -} - void Avatar::applyRidingPosition() { if (riding) @@ -5725,7 +5693,6 @@ void Avatar::onUpdate(float dt) if (songInterfaceTimer < 1) songInterfaceTimer += dt; } - updateJoystick(dt); if (quickSongCastDelay>0) { diff --git a/Aquaria/Avatar.h b/Aquaria/Avatar.h index ccdee0e..cd5960c 100644 --- a/Aquaria/Avatar.h +++ b/Aquaria/Avatar.h @@ -180,7 +180,6 @@ public: Vector getKeyDir(); void startBurstCommon(); - void updateJoystick(float dt); void openSingingInterface(); void closeSingingInterface(); diff --git a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp index 6f05c20..0e2c7a2 100644 --- a/Aquaria/DSQ.cpp +++ b/Aquaria/DSQ.cpp @@ -1603,12 +1603,6 @@ void DSQ::toggleInputGrabPlat(bool on) { } -void DSQ::instantQuit() -{ - if (core->getCtrlState() && core->getAltState()) - Core::instantQuit(); -} - int DSQ::getEntityLayerToLayer(int lcode) { if (lcode == -4) @@ -3970,21 +3964,6 @@ void DSQ::onUpdate(float dt) if (armb && ActionMapper::getKeyState(armb->joy[0])) mouse.buttons.right = DOWN; - - - - // not going to happen anymore! - // bye, bye xbox360 controller - if (!mouse.buttons.middle) - { - if (joystick.rightThumb) - mouse.buttons.middle = DOWN; - else if (joystick.leftThumb) - mouse.buttons.middle = DOWN; - } - - - } if (joystickEnabled) diff --git a/Aquaria/DSQ.h b/Aquaria/DSQ.h index 6b7864e..9cde5c5 100644 --- a/Aquaria/DSQ.h +++ b/Aquaria/DSQ.h @@ -1486,8 +1486,6 @@ public: void loadFonts(); - void instantQuit(); - void centerText(const std::string &text); void centerMessage(const std::string &text, float y=300, int type=0); diff --git a/Aquaria/Game.cpp b/Aquaria/Game.cpp index 5abae96..a052815 100644 --- a/Aquaria/Game.cpp +++ b/Aquaria/Game.cpp @@ -6981,28 +6981,6 @@ void Game::bindInput() addAction(ACTION_TOGGLEGRID, KEY_F9); } - /* - addAction(ACTION_MENULEFT, KEY_LEFT); - addAction(ACTION_MENURIGHT, KEY_RIGHT); - addAction(ACTION_MENUUP, KEY_UP); - addAction(ACTION_MENUDOWN, KEY_DOWN); - - dsq->user.control.actionSet.importAction(this, "SwimLeft", ACTION_MENULEFT); - dsq->user.control.actionSet.importAction(this, "SwimRight", ACTION_MENURIGHT); - dsq->user.control.actionSet.importAction(this, "SwimUp", ACTION_MENUUP); - dsq->user.control.actionSet.importAction(this, "SwimDown", ACTION_MENUDOWN); - - addAction(ACTION_MENULEFT, JOY1_DPAD_LEFT); - addAction(ACTION_MENURIGHT, JOY1_DPAD_RIGHT); - addAction(ACTION_MENUUP, JOY1_DPAD_UP); - addAction(ACTION_MENUDOWN, JOY1_DPAD_DOWN); - */ - - addAction(ACTION_MENULEFT, JOY1_STICK_LEFT); - addAction(ACTION_MENURIGHT, JOY1_STICK_RIGHT); - addAction(ACTION_MENUUP, JOY1_STICK_UP); - addAction(ACTION_MENUDOWN, JOY1_STICK_DOWN); - // To capture quick song keys via script dsq->user.control.actionSet.importAction(this, "SongSlot1", ACTION_SONGSLOT1); dsq->user.control.actionSet.importAction(this, "SongSlot2", ACTION_SONGSLOT2); diff --git a/Aquaria/UserSettings.cpp b/Aquaria/UserSettings.cpp index bb7a6ab..8f56ba8 100644 --- a/Aquaria/UserSettings.cpp +++ b/Aquaria/UserSettings.cpp @@ -20,14 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "UserSettings.h" -#ifndef AQUARIA_USERSETTINGS_DATAONLY - #include "DSQ.h" - #include "Game.h" - #include "Avatar.h" -#else - #include "tinyxml2.h" - using namespace tinyxml2; -#endif +#include "DSQ.h" +#include "Game.h" +#include "Avatar.h" void UserSettings::save() @@ -554,7 +549,6 @@ void UserSettings::load(bool doApply, const std::string &overrideFile) void UserSettings::apply() { -#ifndef AQUARIA_USERSETTINGS_DATAONLY core->sound->setMusicVolume(audio.musvol); core->sound->setSfxVolume(audio.sfxvol); core->sound->setVoiceVolume(audio.voxvol); @@ -563,6 +557,7 @@ void UserSettings::apply() dsq->loops.updateVolume(); + // FIXME: This should be per-joystick core->joystick.s1ax = control.s1ax; core->joystick.s1ay = control.s1ay; core->joystick.s2ax = control.s2ax; @@ -581,7 +576,5 @@ void UserSettings::apply() dsq->bindInput(); core->settings.prebufferSounds = audio.prebuffer; - -#endif } diff --git a/BBGE/ActionMapper.cpp b/BBGE/ActionMapper.cpp index 27974ed..33a9680 100644 --- a/BBGE/ActionMapper.cpp +++ b/BBGE/ActionMapper.cpp @@ -211,52 +211,12 @@ bool ActionMapper::getKeyState(int k) { keyState = (core->mouse.buttons.middle == DOWN); } - else if (k >= JOY1_BUTTON_0 && k <= JOY1_BUTTON_16) + else if (k >= JOY1_BUTTON_0 && k < JOY1_BUTTON_END) { int v = k - JOY1_BUTTON_0; if (core->joystickEnabled) - keyState = core->joystick.buttons[v]; - } - else if (k == JOY1_STICK_LEFT) - { - keyState = core->joystick.position.x < -0.6f; - } - else if (k == JOY1_STICK_RIGHT) - { - keyState = core->joystick.position.x > 0.6f; - } - else if (k == JOY1_STICK_UP) - { - keyState = core->joystick.position.y < -0.6f; - } - else if (k == JOY1_STICK_DOWN) - { - keyState = core->joystick.position.y > 0.6f; - } - else if (k == X360_BTN_START) - { - keyState = core->joystick.btnStart; - } - else if (k == X360_BTN_BACK) - { - keyState = core->joystick.btnSelect; - } - else if (k == JOY1_DPAD_LEFT) - { - keyState = core->joystick.dpadLeft; - } - else if (k == JOY1_DPAD_RIGHT) - { - keyState = core->joystick.dpadRight; - } - else if (k == JOY1_DPAD_UP) - { - keyState = core->joystick.dpadUp; - } - else if (k == JOY1_DPAD_DOWN) - { - keyState = core->joystick.dpadDown; + keyState = core->joystick.getButton(v); } return keyState; diff --git a/BBGE/ActionMapper.h b/BBGE/ActionMapper.h index 99b8335..2619366 100644 --- a/BBGE/ActionMapper.h +++ b/BBGE/ActionMapper.h @@ -27,6 +27,7 @@ class Event; class ActionMapper; #include "ActionSet.h" +#include "Joystick.h" typedef std::vector ButtonList; @@ -81,46 +82,12 @@ public: MOUSE_BUTTON_MIDDLE = 1001, JOY1_BUTTON_0 = 2000, - JOY1_BUTTON_1 = 2001, - JOY1_BUTTON_2 = 2002, - JOY1_BUTTON_3 = 2003, - JOY1_BUTTON_4 = 2004, - JOY1_BUTTON_5 = 2005, - JOY1_BUTTON_6 = 2006, - JOY1_BUTTON_7 = 2007, - JOY1_BUTTON_8 = 2008, - JOY1_BUTTON_9 = 2009, - JOY1_BUTTON_10 = 2010, - JOY1_BUTTON_11 = 2011, - JOY1_BUTTON_12 = 2012, - JOY1_BUTTON_13 = 2013, - JOY1_BUTTON_14 = 2014, - JOY1_BUTTON_15 = 2015, - JOY1_BUTTON_16 = 2016, - - - X360_BTN_START = 3016, - X360_BTN_BACK = 3017, - - JOY1_DPAD_LEFT = 4000, - JOY1_DPAD_RIGHT = 4001, - JOY1_DPAD_DOWN = 4002, - JOY1_DPAD_UP = 4003, - JOY1_STICK_LEFT = 4010, - JOY1_STICK_RIGHT = 4011, - JOY1_STICK_DOWN = 4012, - JOY1_STICK_UP = 4013, + JOY1_BUTTON_END = JOY1_BUTTON_0 + MAX_JOYSTICK_BTN, // one past end }; - enum { DPAD_LEFT = 0, DPAD_RIGHT, DPAD_UP, DPAD_DOWN }; - - - virtual void enableInput(); virtual void disableInput(); - - Event *addCreatedEvent(Event *event); void clearCreatedEvents(); diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index 986d7fb..48ed1bc 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -807,7 +807,7 @@ void readKeyData() -bool Core::initJoystickLibrary(int numSticks) +bool Core::initJoystickLibrary() { #ifdef BBGE_BUILD_SDL2 @@ -816,13 +816,10 @@ bool Core::initJoystickLibrary(int numSticks) SDL_InitSubSystem(SDL_INIT_JOYSTICK); #endif - if (numSticks > 0) - joystick.init(0); + joystick.init(0); joystickEnabled = true; - - return true; } @@ -2124,8 +2121,6 @@ void Core::pollEvents() { SDL_Quit(); _exit(0); - - } } break; @@ -2141,6 +2136,15 @@ void Core::pollEvents() } } break; + + case SDL_JOYDEVICEADDED: + onJoystickAdded(event.jdevice.which); + break; + + case SDL_JOYDEVICEREMOVED: + onJoystickRemoved(event.jdevice.which); + break; + #else case SDL_MOUSEBUTTONDOWN: { @@ -3419,3 +3423,11 @@ void Core::initLocalization() initCharTranslationTables(trans); } +void Core::onJoystickAdded(int deviceID) +{ + +} + +void Core::onJoystickRemoved(int instanceID) +{ +} diff --git a/BBGE/Core.h b/BBGE/Core.h index fc4e0dc..e74859c 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -40,10 +40,10 @@ BUILD_LINUX #include "DarkLayer.h" - - #include "FrameBuffer.h" #include "Shader.h" +#include "Joystick.h" + class ParticleEffect; @@ -243,44 +243,6 @@ struct Mouse int scrollWheel, scrollWheelChange, lastScrollWheel; }; -const int maxJoyBtns = 64; - -class Joystick -{ -public: - Joystick(); - void init(int stick=0); - void shutdown(); - //Ranges from 0 to 65535 (full speed). - void rumble(float leftMotor, float rightMotor, float time); - void update(float dt); - Vector position, lastPosition; - ButtonState buttons[maxJoyBtns]; - float deadZone1, deadZone2; - float clearRumbleTime; - - void callibrate(Vector &vec, float dead); - - float leftTrigger, rightTrigger; - bool leftThumb, rightThumb, leftShoulder, rightShoulder, dpadLeft, dpadRight, dpadUp, dpadDown; - bool btnStart, btnSelect; - Vector rightStick; - bool inited, xinited; - bool anyButton(); -# ifdef BBGE_BUILD_SDL2 - SDL_GameController *sdl_controller; - SDL_Haptic *sdl_haptic; -# endif - SDL_Joystick *sdl_joy; -#if defined(__LINUX__) && !defined(BBGE_BUILD_SDL2) - int eventfd; - int16_t effectid; -#endif - int stickIndex; - - int s1ax, s1ay, s2ax, s2ay; -}; - enum FollowCameraLock { FCL_NONE = 0, @@ -741,13 +703,16 @@ protected: float baseCullRadius; bool initSoundLibrary(const std::string &defaultDevice); bool initInputLibrary(); - bool initJoystickLibrary(int numSticks=1); + bool 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(); + virtual void onJoystickAdded(int deviceID); + virtual void onJoystickRemoved(int instanceID); + int afterEffectManagerLayer; Vector cameraOffset; std::vector avgFPS; diff --git a/BBGE/Joystick.cpp b/BBGE/Joystick.cpp index 9380cca..8fb780f 100644 --- a/BBGE/Joystick.cpp +++ b/BBGE/Joystick.cpp @@ -42,7 +42,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Joystick::Joystick() { - xinited = false; stickIndex = -1; # ifdef BBGE_BUILD_SDL2 sdl_controller = NULL; @@ -54,20 +53,11 @@ Joystick::Joystick() effectid = -1; #endif inited = false; - for (int i = 0; i < maxJoyBtns; i++) - { - buttons[i] = UP; - } + buttonBitmask = 0; deadZone1 = 0.3; deadZone2 = 0.3; clearRumbleTime= 0; - leftThumb = rightThumb = false; - leftTrigger = rightTrigger = 0; - rightShoulder = leftShoulder = false; - dpadRight = dpadLeft = dpadUp = dpadDown = false; - btnStart = false; - btnSelect = false; s1ax = 0; s1ay = 1; @@ -309,7 +299,11 @@ void Joystick::update(float dt) if (!SDL_JoystickGetAttached(sdl_joy)) { debugLog("Lost Joystick"); - if (sdl_haptic) { SDL_HapticClose(sdl_haptic); sdl_haptic = NULL; } + if (sdl_haptic) + { + SDL_HapticClose(sdl_haptic); + sdl_haptic = NULL; + } if (!sdl_controller) SDL_JoystickClose(sdl_joy); else @@ -371,27 +365,20 @@ void Joystick::update(float dt) callibrate(rightStick, deadZone2); + buttonBitmask = 0; #ifdef BBGE_BUILD_SDL2 if (sdl_controller) { - for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++) - buttons[i] = SDL_GameControllerGetButton(sdl_controller, (SDL_GameControllerButton)i)?DOWN:UP; - for (int i = SDL_CONTROLLER_BUTTON_MAX; i < maxJoyBtns; i++) - buttons[i] = UP; + for (unsigned i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++) + buttonBitmask |= !!SDL_GameControllerGetButton(sdl_controller, (SDL_GameControllerButton)i) << i; } else - { - for (int i = 0; i < maxJoyBtns; i++) - buttons[i] = SDL_JoystickGetButton(sdl_joy, i)?DOWN:UP; - } -#else - for (int i = 0; i < maxJoyBtns; i++) - buttons[i] = SDL_JoystickGetButton(sdl_joy, i)?DOWN:UP; #endif - - - + { + for (unsigned i = 0; i < MAX_JOYSTICK_BTN; i++) + buttonBitmask |= !!SDL_JoystickGetButton(sdl_joy, i) << i; + } } if (clearRumbleTime >= 0) @@ -402,16 +389,9 @@ void Joystick::update(float dt) rumble(0,0,0); } } - - - } bool Joystick::anyButton() { - for (int i = 0; i < maxJoyBtns; i++) - { - if (buttons[i]) return true; - } - return false; + return !!buttonBitmask;; } diff --git a/BBGE/Joystick.h b/BBGE/Joystick.h new file mode 100644 index 0000000..1b4692f --- /dev/null +++ b/BBGE/Joystick.h @@ -0,0 +1,50 @@ +#ifndef BBGE_JOYSTICK_H +#define BBGE_JOYSTICK_H + +#include + +#include "Vector.h" + +#define MAX_JOYSTICK_BTN 32 + +class Joystick +{ +public: + Joystick(); + void init(int stick=0); + void shutdown(); + //Ranges from 0 to 65535 (full speed). + void rumble(float leftMotor, float rightMotor, float time); + void update(float dt); + Vector position, lastPosition; + + float deadZone1, deadZone2; + float clearRumbleTime; + + void callibrate(Vector &vec, float dead); + bool anyButton(); + bool getButton(unsigned id) const { return buttonBitmask & (1u << id); } + + Vector rightStick; + + int stickIndex; + int s1ax, s1ay, s2ax, s2ay; + +private: + bool inited; + unsigned buttonBitmask; // FIXME: this should go + +# ifdef BBGE_BUILD_SDL2 + SDL_GameController *sdl_controller; + SDL_Haptic *sdl_haptic; +# endif + SDL_Joystick *sdl_joy; + +#if defined(__LINUX__) && !defined(BBGE_BUILD_SDL2) + int eventfd; + short effectid; +#endif +}; + + +#endif diff --git a/win/vc90/BBGE.vcproj b/win/vc90/BBGE.vcproj index f1cb420..8dbd098 100644 --- a/win/vc90/BBGE.vcproj +++ b/win/vc90/BBGE.vcproj @@ -296,6 +296,10 @@ RelativePath="..\..\BBGE\Joystick.cpp" > + +