aboutsummaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-24 10:48:45 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-24 10:48:45 +0200
commit02da08e9d84371c558da6347210c00e3966f2dc5 (patch)
tree2f755363d363888ffd17e870060c4049195e24a3 /src/input.c
parent1c4eada99c6f98eae40c676d261c8d4fc1de6079 (diff)
Improve Gamepad Handling
There were a few cases where we would have stale input information becaues the `pw_input` only receives events while the game is running. This fixes that by: - Reseting the player count on game start - Clearing button states for disconnected players - Improve handling of the POOM player 2 support in case a second player connects
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/input.c b/src/input.c
index e2c5298..faf4758 100644
--- a/src/input.c
+++ b/src/input.c
@@ -29,9 +29,38 @@ void pw_input_free(struct pw_input *input) {
SDL_free(input);
}
+bool pw_input_reset(struct pw_input *input, struct pw_players *players) {
+ if (!input) {
+ return SDL_InvalidParamError("input");
+ }
+ if (!players) {
+ return SDL_InvalidParamError("players");
+ }
+
+ bool connected[PW_PLAYERS_MAX];
+ input->nplayers = pw_players_status(players, connected);
+ SDL_zeroa(input->players);
+
+ return true;
+}
+
static bool handle_connection(struct pw_input *input,
struct pw_players *players) {
- input->nplayers = pw_players_count(players);
+ size_t previous = input->nplayers;
+
+ bool connected[PW_PLAYERS_MAX];
+ input->nplayers = pw_players_status(players, connected);
+ for (size_t i = 0; i < SDL_arraysize(connected); ++i) {
+ input->players[i] *= (uint8_t)connected[i];
+ }
+
+ // We need to clear the button states of player 2 in case it was
+ // set because of the special POOM handling, and a new controller
+ // connected.
+ if (previous == 1 && input->nplayers > 1) {
+ input->players[1] = 0;
+ }
+
return players;
}
@@ -66,7 +95,7 @@ static bool handle_axis(struct pw_input *input, struct pw_players *players,
// Some special POOM handling, which uses both player's inputs for controls.
// This allows one player's directions be used for turning, and another for
// strafing.
- if (!i && pw_players_count(players) == 1) {
+ if (!i && input->nplayers == 1) {
enum pw_button *p2 = &input->players[1];
switch (event->axis) {
case SDL_GAMEPAD_AXIS_RIGHTX: