diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp index bf81178..c8a7ec7 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/ Source information and contacts persons can be found at -http://blockattack.net +http://www.blockattack.net =========================================================================== */ @@ -25,6 +25,7 @@ http://blockattack.net #include "MenuSystem.h" #include "common.h" #include "global.hpp" +#include "gamecontroller.h" static int mousex; static int mousey; @@ -190,6 +191,16 @@ bool isUpEvent(const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value < -deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } @@ -204,6 +215,16 @@ bool isDownEvent(const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value > deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } @@ -218,6 +239,16 @@ bool isLeftEvent(const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value < -deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } @@ -232,6 +263,16 @@ bool isRightEvent(const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value > deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } diff --git a/source/code/gamecontroller.cpp b/source/code/gamecontroller.cpp index 443ac9a..40b89e1 100644 --- a/source/code/gamecontroller.cpp +++ b/source/code/gamecontroller.cpp @@ -25,9 +25,17 @@ http://www.blockattack.net #include "SDL_gamecontroller.h" #include "sago/platform_folders.h" #include +#include static bool verbose = false; +struct ControllerStatus { + std::map AxisInDeadZone; +}; + +static std::map controllerStatusMap; + + void GameControllerSetVerbose(bool value) { verbose = value; } @@ -60,6 +68,24 @@ void InitGameControllers() { } } +void checkDeadZone(const SDL_Event& event) { + if (event.type != SDL_CONTROLLERAXISMOTION) { + return; //assert? + } + int value = event.caxis.value; + if (value > -deadZoneLimit && value < deadZoneLimit) { + controllerStatusMap[event.caxis.which].AxisInDeadZone[event.caxis.axis] = true; + } +} + +bool getDeadZone(SDL_JoystickID id, int axis) { + return controllerStatusMap[id].AxisInDeadZone[axis]; +} + +void setDeadZone(SDL_JoystickID id, int axis, bool value) { + controllerStatusMap[id].AxisInDeadZone[axis] = value; +} + bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) { if (playerNumber != 1) { return false; @@ -69,6 +95,16 @@ bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value > deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } @@ -81,6 +117,16 @@ bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value < -deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } @@ -93,6 +139,16 @@ bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value < -deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } @@ -105,6 +161,16 @@ bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) { return true; } } + if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) { + checkDeadZone(event); + const SDL_ControllerAxisEvent &a = event.caxis; + if (getDeadZone(a.which, a.axis)) { + if (event.caxis.value > deadZoneLimit) { + setDeadZone(a.which,a.axis,false); + return true; + } + } + } return false; } diff --git a/source/code/gamecontroller.h b/source/code/gamecontroller.h index 4bb2281..8c02c58 100644 --- a/source/code/gamecontroller.h +++ b/source/code/gamecontroller.h @@ -23,6 +23,9 @@ http://www.blockattack.net #include "SDL.h" + +const int deadZoneLimit = 20000; + void InitGameControllers(); bool isPlayerDownEvent(int playerNumber, const SDL_Event& event); @@ -31,4 +34,28 @@ bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event); bool isPlayerRightEvent(int playerNumber, const SDL_Event& event); bool isPlayerSwitchEvent(int playerNumber, const SDL_Event& event); bool isPlayerPushEvent(int playerNumber, const SDL_Event& event); -void GameControllerSetVerbose(bool value); \ No newline at end of file +void GameControllerSetVerbose(bool value); + +/** + * Checks that the given event is in the dead zone. + * If it is in the dead zone. Then the dead zone variable for that axis will be set to true; + * Otherwise nothing is done + * @param event An SDL + */ +void checkDeadZone(const SDL_Event& event); + +/** + * Checks that the given axis on a given gamepad was in a dead zone last time checked. + * @param id The gamepad + * @param axis The axis on the gamepad + * @return true if the axis has been in the dead zone + */ +bool getDeadZone(SDL_JoystickID id, int axis); + +/** + * Sets dead zone status on a given axis on a given gamepad + * @param id The gamepad + * @param axis The axis on the gamepad + * @param value Value to set. Should normally be false as true will be set by checkDeadZone + */ +void setDeadZone(SDL_JoystickID id, int axis, bool value); \ No newline at end of file