diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-24 10:48:45 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-24 10:48:45 +0200 |
| commit | 02da08e9d84371c558da6347210c00e3966f2dc5 (patch) | |
| tree | 2f755363d363888ffd17e870060c4049195e24a3 | |
| parent | 1c4eada99c6f98eae40c676d261c8d4fc1de6079 (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
| -rw-r--r-- | src/game.c | 17 | ||||
| -rw-r--r-- | src/game.h | 3 | ||||
| -rw-r--r-- | src/input.c | 33 | ||||
| -rw-r--r-- | src/input.h | 5 | ||||
| -rw-r--r-- | src/picow.c | 2 | ||||
| -rw-r--r-- | src/player.c | 7 | ||||
| -rw-r--r-- | src/player.h | 6 |
7 files changed, 58 insertions, 15 deletions
@@ -72,16 +72,20 @@ void pw_game_free(struct pw_game *game) { SDL_free(game); } -bool pw_game_start(struct pw_game *game, struct pw_cart_data *cartdat) { +bool pw_game_start(struct pw_game *game, struct pw_players *players, + struct pw_cart_data *cartdat) { if (!game) { - SDL_InvalidParamError("game"); - return false; + return SDL_InvalidParamError("game"); + } + if (!players) { + return SDL_InvalidParamError("players"); } if (!cartdat) { - SDL_InvalidParamError("cartdat"); - return false; + return SDL_InvalidParamError("cartdat"); } + pw_input_reset(game->input, players); + if (game->cartdat) { pw_cart_data_free(game->cartdat); } @@ -134,8 +138,7 @@ bool pw_game_start(struct pw_game *game, struct pw_cart_data *cartdat) { bool pw_game_event(struct pw_game *game, struct pw_players *players, SDL_Event *event) { if (!game) { - SDL_InvalidParamError("game"); - return false; + return SDL_InvalidParamError("game"); } return pw_input_event(game->input, players, event); @@ -38,7 +38,8 @@ void pw_game_free(struct pw_game *game); /** * Starts a PICO-8 game. */ -bool pw_game_start(struct pw_game *game, struct pw_cart_data *cartdat); +bool pw_game_start(struct pw_game *game, struct pw_players *players, + struct pw_cart_data *cartdat); /** * Handles SDL events for game state. 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: diff --git a/src/input.h b/src/input.h index 7810918..94125ba 100644 --- a/src/input.h +++ b/src/input.h @@ -138,6 +138,11 @@ struct pw_input *pw_input_init(SDL_Window *window); void pw_input_free(struct pw_input *input); /** + * Resets the player state. + */ +bool pw_input_reset(struct pw_input *input, struct pw_players *players); + +/** * Handles an SDL events for PICO-8 input. */ bool pw_input_event(struct pw_input *input, struct pw_players *players, diff --git a/src/picow.c b/src/picow.c index a0e51e4..f171e22 100644 --- a/src/picow.c +++ b/src/picow.c @@ -160,7 +160,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) { return failf("failed to update cart selection menu state"); } if (cartdat) { - if (!pw_game_start(state->game, cartdat)) { + if (!pw_game_start(state->game, state->players, cartdat)) { return failf("failed to start selected cart"); } state->scene = PW_SCENE_GAME; diff --git a/src/player.c b/src/player.c index 3a4757e..a4d7a75 100644 --- a/src/player.c +++ b/src/player.c @@ -82,15 +82,18 @@ bool pw_players_event(struct pw_players *players, SDL_Event *event) { } } -size_t pw_players_count(struct pw_players *players) { +size_t pw_players_status(struct pw_players *players, + bool connected[static PW_PLAYERS_MAX]) { if (!players) { SDL_InvalidParamError("players"); + SDL_memset(connected, 0, sizeof(bool[PW_PLAYERS_MAX])); return 0; } size_t count = 0; for (size_t i = 0; i < PW_PLAYERS_MAX; i++) { - if (players->gamepads[i]) { + connected[i] = players->gamepads[i]; + if (connected[i]) { ++count; } } diff --git a/src/player.h b/src/player.h index b75d875..0572e0e 100644 --- a/src/player.h +++ b/src/player.h @@ -30,9 +30,11 @@ void pw_players_free(struct pw_players *players); bool pw_players_event(struct pw_players *players, SDL_Event *event); /** - * The number of connected players. + * The connection status of each player, as a convenience returns the + * number of connected players. */ -size_t pw_players_count(struct pw_players *players); +size_t pw_players_status(struct pw_players *players, + bool connected[static PW_PLAYERS_MAX]); /** * Finds the player associated with a gamepad. Returns `PW_PLAYERS_MAX` if not |
