diff --git a/src/core/iapplicationEventHandler.hpp b/src/core/iapplicationEventHandler.hpp index a4814f0..7ca066a 100644 --- a/src/core/iapplicationEventHandler.hpp +++ b/src/core/iapplicationEventHandler.hpp @@ -13,6 +13,14 @@ class IApplicationEventHandler virtual void onMouseUp(uint32 mouseButton, uint8 numClicks) {} virtual void onMouseMove(int32 mousePosX, int32 mousePosY, int32 deltaX, int32 deltaY) {} + + virtual int32 onControllerAdded(){} + virtual void onControllerRemoved(int32 controllerID){} + virtual void onControllerDown(int32 controllerID,uint8 controllerButton){} + virtual void onControllerUp(int32 controllerID,uint8 controllerButton){} + virtual void onControllerAxis(int32 controllerID,uint8 controllerAxis, int16 axisValue){} + + private: NULL_COPY_AND_ASSIGN(IApplicationEventHandler); }; diff --git a/src/core/input.hpp b/src/core/input.hpp index 1aceecd..3df41f8 100644 --- a/src/core/input.hpp +++ b/src/core/input.hpp @@ -364,6 +364,36 @@ class Input static const int NUM_KEYS = 512; static const int NUM_MOUSEBUTTONS = 256; + + enum{ + CBUTTON_A=0, + CBUTTON_B=1, + CBUTTON_X=2, + CBUTTON_Y=3, + CBUTTON_BACK=4, + CBUTTON_GUIDE=5, + CBUTTON_START=6, + CBUTTON_LEFTSTICK=7, + CBUTTON_RIGHTSTICK=8, + CBUTTON_LEFTSHOULDER=9, + CBUTTON_RIGHTSHOULDER=10, + CBUTTON_DPAD_UP=11, + CBUTTON_DPAD_DOWN=12, + CBUTTON_DPAD_LEFT=13, + CBUTTON_DPAD_RIGHT=14 + }; + + enum{ + CAXIS_LEFTX=0, + CAXIS_LEFTY=1, + CAXIS_RIGHTX=2, + CAXIS_RIGHTY=3, + CAXIS_TRIGGERLEFT=4, + CAXIS_TRIGGERRIGHT=5 + }; + + static const int NUM_CONTROLLER_BUTTONS = 256; + static const int NUM_CONTROLLER_AXIS= 256; }; #endif diff --git a/src/gameEventHandler.cpp b/src/gameEventHandler.cpp index 70bad78..945e510 100644 --- a/src/gameEventHandler.cpp +++ b/src/gameEventHandler.cpp @@ -45,3 +45,64 @@ void GameEventHandler::updateInput(uint32 inputCode, float dir, bool isRepeat) inputs[inputCode][i].second.addAmt(inputs[inputCode][i].first * dir); } } + +void GameEventHandler::addController(uint32 controllerID) { + controllers[controllerID].id=controllerID; +} + +int32 GameEventHandler::onControllerAdded() { + int32 o=-1; + for(auto& c:controllers) + if(!c.second.connected){ + c.second.connected=true; + return c.second.id; + } + return o; +} + +void GameEventHandler::onControllerRemoved(int32 controllerID) { + controllers[controllerID].connected=false; +} + +void GameEventHandler::addControllerButtonControl(uint32 controllerID,uint8 button, InputControl &inputControl, float weight) { + controllers[controllerID].buttons[button].push_back(std::pair(weight, inputControl)); +} + +void +GameEventHandler::addControllerAxisControl(uint32 controllerID, uint8 axis, InputControl &inputControl, float weight) { + controllers[controllerID].axis[axis].inputs.push_back(std::pair(weight, inputControl)); + controllers[controllerID].axis[axis].lastVal=0; +} + +void GameEventHandler::updateControllerButton(int32 controllerID,uint8 button, float dir) { + for(uint32 i = 0; i < controllers[controllerID].buttons[button] + .size(); i++) { + controllers[controllerID] + .buttons[button][i] + .second + .addAmt( + controllers[controllerID] + .buttons[button][i] + .first * dir); + } +} + +void GameEventHandler::onControllerUp(int32 controllerID, uint8 controllerButton) { + updateControllerButton(controllerID,controllerButton,1.0f); +} + +void GameEventHandler::onControllerDown(int32 controllerID, uint8 controllerButton) { + updateControllerButton(controllerID,controllerButton,-1.0f); +} + +void GameEventHandler::onControllerAxis(int32 controllerID, uint8 controllerAxis, int16 axisValue) { + int16 v=controllers[controllerID].axis[controllerAxis].lastVal; + float w=((float)(axisValue-v)+0.5f)/(32767.5f); + for(uint32 i = 0; i < controllers[controllerID].axis[controllerAxis].inputs.size(); i++){ + controllers[controllerID].axis[controllerAxis].inputs[i].second.addAmt( + controllers[controllerID].axis[controllerAxis].inputs[i].first*w + ); + } + + controllers[controllerID].axis[controllerAxis].lastVal=axisValue; +} diff --git a/src/gameEventHandler.hpp b/src/gameEventHandler.hpp index 6c9f011..0064255 100644 --- a/src/gameEventHandler.hpp +++ b/src/gameEventHandler.hpp @@ -7,6 +7,19 @@ class GameEventHandler : public IApplicationEventHandler { + struct Controller{ + uint32 id; + bool connected; + Map > > buttons; + struct Axis{ + Array > inputs; + int16 lastVal; + }; + Map axis; + Controller(){ + connected=false; + } + }; public: GameEventHandler() {} virtual ~GameEventHandler() {} @@ -17,10 +30,23 @@ class GameEventHandler : public IApplicationEventHandler virtual void onMouseMove(int32 mousePosX, int32 mousePosY, int32 deltaX, int32 deltaY); + virtual int32 onControllerAdded(); + virtual void onControllerRemoved(int32 controllerID); + virtual void onControllerUp(int32 controllerID,uint8 controllerButton); + virtual void onControllerDown(int32 controllerID,uint8 controllerButton); + virtual void onControllerAxis(int32 controllerID,uint8 controllerAxis, int16 axisValue); + void addKeyControl(uint32 keyCode, InputControl& inputControl, float weight = 1.0f); void addMouseControl(uint32 mouseButton, InputControl& inputControl, float weight = 1.0f); + + void addController(uint32 controllerID); + void addControllerButtonControl(uint32 controllerID,uint8 button, InputControl& inputControl, float weight = 1.0f); + void addControllerAxisControl(uint32 controllerID,uint8 axis, InputControl& inputControl, float weight = 1.0f); + private: Map > > inputs; + Map controllers; void updateInput(uint32 inputCode, float dir, bool isRepeat); + void updateControllerButton(int32 controller,uint8 button,float dir); NULL_COPY_AND_ASSIGN(GameEventHandler); }; diff --git a/src/platform/sdl/sdlApplication.cpp b/src/platform/sdl/sdlApplication.cpp index d461f17..780295d 100644 --- a/src/platform/sdl/sdlApplication.cpp +++ b/src/platform/sdl/sdlApplication.cpp @@ -1,12 +1,13 @@ #include "sdlApplication.hpp" #include "core/common.hpp" #include +#include uint32 SDLApplication::numInstances = 0; SDLApplication* SDLApplication::create() { - const uint32 flags = SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS; + const uint32 flags = SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER; uint32 initialized = SDL_WasInit(flags); if(initialized != flags && SDL_Init(flags) != 0) { @@ -19,6 +20,9 @@ SDLApplication* SDLApplication::create() SDLApplication::SDLApplication() { +#ifdef SDL_JOYSTICK_DISABLED + DEBUG_LOG("", "NONE", "well fuck"); +#endif numInstances++; isAppRunning = true; } @@ -38,7 +42,7 @@ void SDLApplication::processMessages(double delta, IApplicationEventHandler& eve while(SDL_PollEvent(&e)) { switch(e.type){ - case SDL_KEYDOWN: + case SDL_KEYDOWN: eventHandler.onKeyDown(e.key.keysym.scancode, e.key.repeat != 0); break; case SDL_KEYUP: @@ -56,8 +60,38 @@ void SDLApplication::processMessages(double delta, IApplicationEventHandler& eve case SDL_QUIT: isAppRunning = false; break; + + //controller "support" by Shinigami072 + case SDL_CONTROLLERDEVICEADDED: { + int resp = eventHandler.onControllerAdded(); + if (resp >= 0) {//if we require more controllers + SDLController s; + s.gameController = SDL_GameControllerOpen(e.cdevice.which); + if (s.gameController == NULL) { + } + s.id=resp; + SDL_Joystick* i=SDL_GameControllerGetJoystick(s.gameController); + + controllers[SDL_JoystickInstanceID(i)] = s; + + } + }break; + case SDL_CONTROLLERDEVICEREMOVED: { + eventHandler.onControllerRemoved(controllers[e.cdevice.which].id); + controllers.erase(e.cdevice.which); + }break; + + case SDL_CONTROLLERBUTTONDOWN: + eventHandler.onControllerDown(controllers[e.cdevice.which].id,e.cbutton.button); + break; + case SDL_CONTROLLERBUTTONUP: + eventHandler.onControllerUp(controllers[e.cdevice.which].id,e.cbutton.button); + break; + case SDL_CONTROLLERAXISMOTION: + eventHandler.onControllerAxis(controllers[e.cdevice.which].id,e.caxis.axis,e.caxis.value); + break; default: - break; + break; }; } } diff --git a/src/platform/sdl/sdlApplication.hpp b/src/platform/sdl/sdlApplication.hpp index d6cba76..77e3638 100644 --- a/src/platform/sdl/sdlApplication.hpp +++ b/src/platform/sdl/sdlApplication.hpp @@ -1,8 +1,44 @@ #pragma once +#include +#include #include "core/common.hpp" #include "core/iapplicationEventHandler.hpp" +struct SDLController{ + SDL_GameController* gameController; + int32 id; + SDLController(){ + id=0; + gameController=nullptr; + } + + SDLController(SDLController&& sdl){ + id=sdl.id; + gameController=sdl.gameController; + + sdl.id=-1; + sdl.gameController=nullptr; + } + ~SDLController(){ + if(gameController!= nullptr){ + // SDL_GameControllerClose(gameController); + gameController=nullptr; + } + } + + SDLController& operator = (const SDLController& controller)= delete; + + SDLController& operator = (SDLController&& controller){ + gameController=controller.gameController; + id=controller.id; + + controller.id=-1; + controller.gameController=nullptr; + return *this; + } +}; + class SDLApplication { public: @@ -14,6 +50,6 @@ class SDLApplication private: bool isAppRunning; static uint32 numInstances; - + std::map controllers; SDLApplication(); };