diff --git a/source/code/gamecontroller.cpp b/source/code/gamecontroller.cpp index 036edf3..72f5f5b 100644 --- a/source/code/gamecontroller.cpp +++ b/source/code/gamecontroller.cpp @@ -31,6 +31,7 @@ static bool verbose = false; struct ControllerStatus { std::map AxisInDeadZone; + int player = 1; }; static std::map controllerStatusMap; @@ -61,8 +62,12 @@ void InitGameControllers() { for (int i = 0; i < SDL_NumJoysticks(); ++i) { if (SDL_IsGameController(i)) { controller = SDL_GameControllerOpen(i); + SDL_Joystick *j = SDL_GameControllerGetJoystick(controller); + SDL_JoystickID instanceId = SDL_JoystickInstanceID(j); + controllerStatusMap[instanceId].player = 1; if (verbose) { std::cout << "Supported game controller detected: " << GameControllerGetName(controller) << ", mapping: " << SDL_GameControllerMapping(controller) << std::endl; + std::cout << "Assigned to player: " << controllerStatusMap[instanceId].player << std::endl; } } } @@ -86,8 +91,25 @@ void setDeadZone(SDL_JoystickID id, int axis, bool value) { controllerStatusMap[id].AxisInDeadZone[axis] = value; } +static bool skipThisPlayer(int playerNumber, const SDL_Event& event) { + + if (event.type == SDL_CONTROLLERBUTTONDOWN) { + ControllerStatus& cs = controllerStatusMap[event.cbutton.which]; + if (cs.player == playerNumber) { + return false; + } + } + if (event.type == SDL_CONTROLLERAXISMOTION ) { + ControllerStatus& cs = controllerStatusMap[event.caxis.which]; + if (cs.player == playerNumber) { + return false; + } + } + return true; +} + bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) { - if (playerNumber != 1) { + if (skipThisPlayer(playerNumber, event)) { return false; } if (event.type == SDL_CONTROLLERBUTTONDOWN) { @@ -96,8 +118,8 @@ bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) { } } if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) { - checkDeadZone(event); const SDL_ControllerAxisEvent& a = event.caxis; + checkDeadZone(event); if (getDeadZone(a.which, a.axis)) { if (event.caxis.value > deadZoneLimit) { setDeadZone(a.which,a.axis,false); @@ -109,7 +131,7 @@ bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) { } bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) { - if (playerNumber != 1) { + if (skipThisPlayer(playerNumber, event)) { return false; } if (event.type == SDL_CONTROLLERBUTTONDOWN) { @@ -131,7 +153,7 @@ bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) { } bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) { - if (playerNumber != 1) { + if (skipThisPlayer(playerNumber, event)) { return false; } if (event.type == SDL_CONTROLLERBUTTONDOWN) { @@ -153,7 +175,7 @@ bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) { } bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) { - if (playerNumber != 1) { + if (skipThisPlayer(playerNumber, event)) { return false; } if (event.type == SDL_CONTROLLERBUTTONDOWN) { @@ -175,7 +197,7 @@ bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) { } bool isPlayerSwitchEvent(int playerNumber, const SDL_Event& event) { - if (playerNumber != 1) { + if (skipThisPlayer(playerNumber, event)) { return false; } if (event.type == SDL_CONTROLLERBUTTONDOWN) { @@ -187,7 +209,7 @@ bool isPlayerSwitchEvent(int playerNumber, const SDL_Event& event) { } bool isPlayerPushEvent(int playerNumber, const SDL_Event& event) { - if (playerNumber != 1) { + if (skipThisPlayer(playerNumber, event)) { return false; } if (event.type == SDL_CONTROLLERBUTTONDOWN) { diff --git a/source/code/main.cpp b/source/code/main.cpp index a936dda..3827e09 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -1698,6 +1698,24 @@ int runGame(int gametype, int level) { if (isPlayerPushEvent(1, event)) { theGame.PushLine(); } + if (isPlayerUpEvent(2, event)) { + theGame2.MoveCursor('N'); + } + if (isPlayerDownEvent(2, event)) { + theGame2.MoveCursor('S'); + } + if (isPlayerLeftEvent(2, event)) { + theGame2.MoveCursor('W'); + } + if (isPlayerRightEvent (2, event)) { + theGame2.MoveCursor('E'); + } + if (isPlayerSwitchEvent(2, event)) { + theGame2.SwitchAtCursor(); + } + if (isPlayerPushEvent(2, event)) { + theGame2.PushLine(); + } static int mouseDownX = 0; static int mouseDownY = 0; if (event.type == SDL_MOUSEBUTTONDOWN) {