git repos / blockattack-game

commit fbc58f22

Poul Sander · 2022-01-30 15:59
fbc58f22def1aa1de30496736a4a9ac2f692fdd7 patch · browse files
parent ec3f09de91963721b04bc896bee147b296e8d943

Improved game controller code

Changed files

M source/code/gamecontroller.cpp before
diff --git a/source/code/gamecontroller.cpp b/source/code/gamecontroller.cpp index 1bb6a86..69341d7 100644 --- a/source/code/gamecontroller.cpp +++ b/source/code/gamecontroller.cpp
@@ -173,24 +173,7 @@ bool isGameControllerConnectionEvent(const SDL_Event& event) {
return false;
}
-bool isControllerDownEvent(const SDL_Event& event) {
- if (event.type == SDL_CONTROLLERBUTTONDOWN) {
- if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
- return true;
- }
- }
- if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
- 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);
- return true;
- }
- }
- }
- return false;
-}
+
bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) {
if (skipThisPlayer(playerNumber, event)) {
@@ -199,17 +182,17 @@ bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) {
return isControllerDownEvent(event);
}
-bool isControllerUpEvent(const SDL_Event& event) {
+bool isControllerDirectionEvent(const SDL_Event& event, SDL_GameControllerButton dpad_direction, SDL_GameControllerAxis axis, float axis_mod = 1.0f) {
if (event.type == SDL_CONTROLLERBUTTONDOWN) {
- if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) {
+ if (event.cbutton.button == dpad_direction ) {
return true;
}
}
- if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
- checkDeadZone(event);
+ if (event.type == SDL_CONTROLLERAXISMOTION && event.caxis.axis == axis ) {
const SDL_ControllerAxisEvent& a = event.caxis;
+ checkDeadZone(event);
if (getDeadZone(a.which, a.axis)) {
- if (event.caxis.value < -deadZoneLimit) {
+ if (event.caxis.value * axis_mod > deadZoneLimit) {
setDeadZone(a.which,a.axis,false);
return true;
}
@@ -218,6 +201,22 @@ bool isControllerUpEvent(const SDL_Event& event) {
return false;
}
+bool isControllerDownEvent(const SDL_Event& event) {
+ return isControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_AXIS_LEFTY);
+}
+
+bool isControllerUpEvent(const SDL_Event& event) {
+ return isControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_AXIS_LEFTY, -1.0f);
+}
+
+bool isControllerLeftEvent(const SDL_Event& event) {
+ return isControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_AXIS_LEFTX, -1.0f);
+}
+
+bool isControllerRightEvent(const SDL_Event& event) {
+ return isControllerDirectionEvent(event, SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_AXIS_LEFTX);
+}
+
bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) {
if (skipThisPlayer(playerNumber, event)) {
return false;
@@ -225,25 +224,6 @@ bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) {
return isControllerUpEvent(event);
}
-bool isControllerLeftEvent(const SDL_Event& event) {
- if (event.type == SDL_CONTROLLERBUTTONDOWN) {
- if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) {
- 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;
-}
-
bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
if (skipThisPlayer(playerNumber, event)) {
return false;
@@ -251,25 +231,6 @@ bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
return isControllerLeftEvent(event);
}
-bool isControllerRightEvent(const SDL_Event& event) {
- if (event.type == SDL_CONTROLLERBUTTONDOWN) {
- if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
- 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;
-}
-
bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) {
if (skipThisPlayer(playerNumber, event)) {
return false;