From 80140434e71c8c15c855171ff0554c603849506e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 14 May 2026 09:08:45 +0200 Subject: [PATCH] refactor(input): elimina InputState vestigial --- CMakeLists.txt | 1 - source/core/input/gamepad_manager.hpp | 5 ++- source/core/input/input_state.cpp | 39 ----------------------- source/core/input/input_state.hpp | 46 --------------------------- source/game/entities/player.cpp | 33 +++++++------------ source/game/ui/service_menu.cpp | 9 ------ 6 files changed, 14 insertions(+), 119 deletions(-) delete mode 100644 source/core/input/input_state.cpp delete mode 100644 source/core/input/input_state.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b8747d5..b6cc044 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,7 +74,6 @@ set(APP_SOURCES source/core/ui/ui_config.cpp source/core/input/key_config.cpp source/core/input/global_inputs.cpp - source/core/input/input_state.cpp source/core/input/gamepad_manager.cpp source/core/input/gamepad_bindings.cpp source/core/input/define_buttons.cpp diff --git a/source/core/input/gamepad_manager.hpp b/source/core/input/gamepad_manager.hpp index a040560..2237165 100644 --- a/source/core/input/gamepad_manager.hpp +++ b/source/core/input/gamepad_manager.hpp @@ -14,9 +14,8 @@ // // El polling real de botons i eixos NO es fa a través d'aquesta classe: // els consumidors obtenen el `SDL_Gamepad*` primari (o l'enèsim per a -// multi-jugador futur) i el passen a `InputState::getGamepadButton/Axis`, -// que ho filtra pel flag de bloqueig de la UI (mateixa estratègia que -// amb el teclat). +// multi-jugador futur) i el passen directament a +// `SDL_GetGamepadButton`/`SDL_GetGamepadAxis`. class GamepadManager { public: static auto get() -> GamepadManager&; diff --git a/source/core/input/input_state.cpp b/source/core/input/input_state.cpp deleted file mode 100644 index 05fb6c2..0000000 --- a/source/core/input/input_state.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "core/input/input_state.hpp" - -#include - -namespace InputState { - - namespace { - - bool blocked = false; - - // Buffer retornat quan el bloqueig és actiu. `SDL_GetKeyboardState` - // retorna un pointer d'una mida igual a `SDL_SCANCODE_COUNT`; el - // nostre buffer ha de ser com a mínim igual de gran perquè els - // consumidors puguin indexar-lo amb qualsevol scancode sense - // llegir fora de rang. - auto zeroKeyboard() -> const bool* { - static const std::array ZEROS{}; - return ZEROS.data(); - } - - } // namespace - - void setBlocked(bool value) { blocked = value; } - - auto isBlocked() -> bool { return blocked; } - - auto getKeyboardState() -> const bool* { - return blocked ? zeroKeyboard() : SDL_GetKeyboardState(nullptr); - } - - auto getGamepadButton(SDL_Gamepad* gp, SDL_GamepadButton b) -> bool { - return (blocked || gp == nullptr) ? false : SDL_GetGamepadButton(gp, b); - } - - auto getGamepadAxis(SDL_Gamepad* gp, SDL_GamepadAxis ax) -> Sint16 { - return (blocked || gp == nullptr) ? static_cast(0) : SDL_GetGamepadAxis(gp, ax); - } - -} // namespace InputState diff --git a/source/core/input/input_state.hpp b/source/core/input/input_state.hpp deleted file mode 100644 index a34b932..0000000 --- a/source/core/input/input_state.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -// Wrapper de l'estat d'input (polling) amb gating de bloqueig. -// -// El service menu (i qualsevol UI modal futura) ha de poder "amagar" l'estat -// d'entrada als consumidors de polling — el jugador, entitats controlades -// per l'usuari, etc. — mentre la UI captura l'atenció, sense interrompre la -// resta del joc. Per això tots els consumidors de polling han de llegir a -// través d'aquest mòdul i no cridar `SDL_GetKeyboardState` directament. Si -// `setBlocked(true)` està actiu, el wrapper retorna un buffer tot-a-zero; -// altrament, la crida és transparent (mateix pointer que SDL). -// -// El menú NO passa per aquí: consumeix events (`SDL_EVENT_KEY_DOWN`) abans -// que l'escena els vegi, així que la seva navegació segueix funcionant per -// molt que el flag estigui posat. -// -// El mateix patró aplica al gamepad: `getGamepadButton`/`getGamepadAxis` -// retornen zero/false quan el bloqueig és actiu. Els mandos concrets els -// obtén el caller via `GamepadManager::get().getPrimary()` o similars. - -#include - -namespace InputState { - - // Activa/desactiva el bloqueig global de polling. El ServiceMenu el - // crida a `toggle()`/`close()`. No es toca des de cap altre lloc. - void setBlocked(bool value); - - [[nodiscard]] auto isBlocked() -> bool; - - // Equivalent a `SDL_GetKeyboardState(nullptr)`: retorna un buffer de - // `SDL_SCANCODE_COUNT` bools. Si l'input està bloquejat, tots zero — - // el consumidor veu com si cap tecla estigués premuda, sense branques - // explícites. El buffer és estable mentre dura la sessió; no cal - // guardar-lo frame a frame. - [[nodiscard]] auto getKeyboardState() -> const bool*; - - // Versió filtrada de `SDL_GetGamepadButton`. Retorna false si el bloqueig - // és actiu o si `gp` és nullptr (cas habitual: no hi ha cap mand). - [[nodiscard]] auto getGamepadButton(SDL_Gamepad* gp, SDL_GamepadButton b) -> bool; - - // Versió filtrada de `SDL_GetGamepadAxis`. Retorna 0 si el bloqueig - // és actiu o si `gp` és nullptr. - [[nodiscard]] auto getGamepadAxis(SDL_Gamepad* gp, SDL_GamepadAxis ax) -> Sint16; - -} // namespace InputState diff --git a/source/game/entities/player.cpp b/source/game/entities/player.cpp index 67a12f3..69e6c35 100644 --- a/source/game/entities/player.cpp +++ b/source/game/entities/player.cpp @@ -11,7 +11,6 @@ #include "core/audio/audio.hpp" #include "core/input/gamepad_bindings.hpp" #include "core/input/gamepad_manager.hpp" -#include "core/input/input_state.hpp" #include "core/rendering/canvas.hpp" #include "core/rendering/effects/shadow.hpp" #include "core/rendering/screen.hpp" @@ -46,12 +45,12 @@ namespace { void accumulateGamepadMovement(int player_id, float& vx, float& vy) { SDL_Gamepad* pad = GamepadManager::get().getForPlayer(player_id); if (pad == nullptr) { return; } - if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT)) { vx -= SPEED_PX_PER_S; } - if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) { vx += SPEED_PX_PER_S; } - if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP)) { vy -= SPEED_PX_PER_S; } - if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN)) { vy += SPEED_PX_PER_S; } - const Sint16 LX = InputState::getGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX); - const Sint16 LY = InputState::getGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY); + if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT)) { vx -= SPEED_PX_PER_S; } + if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) { vx += SPEED_PX_PER_S; } + if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP)) { vy -= SPEED_PX_PER_S; } + if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN)) { vy += SPEED_PX_PER_S; } + const Sint16 LX = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX); + const Sint16 LY = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY); if (LX < -STICK_DEADZONE) { vx -= SPEED_PX_PER_S; } if (LX > STICK_DEADZONE) { vx += SPEED_PX_PER_S; } if (LY < -STICK_DEADZONE) { vy -= SPEED_PX_PER_S; } @@ -657,11 +656,7 @@ void Player::tick(float delta_time) { // (`KeyRemap::update` que llig `Options::keys_game` cada frame). refreshControlsFromOptions(); - // Via `InputState` en lloc de `SDL_GetKeyboardState` directe: quan el - // service menu (o qualsevol UI modal) està obert, el wrapper retorna - // un buffer tot-a-zero perquè el jugador no es mogui mentre l'usuari - // navega el menú. Polling-blocked; veure `core/input/input_state.hpp`. - const bool* keys = InputState::getKeyboardState(); + const bool* keys = SDL_GetKeyboardState(nullptr); float vx = 0.0F; float vy = 0.0F; if (keys[controls_.left]) { vx -= SPEED_PX_PER_S; } @@ -669,9 +664,7 @@ void Player::tick(float delta_time) { if (keys[controls_.up]) { vy -= SPEED_PX_PER_S; } if (keys[controls_.down]) { vy += SPEED_PX_PER_S; } - // Cruceta i stick esquerre del mando assignat (si n'hi ha). També - // respecten el bloqueig d'`InputState`, així que amb el menú obert - // els botons tornen false/0 i no contribueixen a la velocitat. + // Cruceta i stick esquerre del mando assignat (si n'hi ha). accumulateGamepadMovement(id_, vx, vy); input_vx_ = vx; @@ -749,7 +742,7 @@ auto Player::consumeJumpEdge() -> bool { // dos passa de no-premut a premut aquest frame, comptem com a salt. // Sense detecció de flanc, mantenir la tecla provocaria saltar // immediatament en aterrar. - const bool* keys = InputState::getKeyboardState(); + const bool* keys = SDL_GetKeyboardState(nullptr); const bool KEY_NOW = keys[controls_.jump]; bool edge = (KEY_NOW && !jump_held_keyboard_); jump_held_keyboard_ = KEY_NOW; @@ -758,8 +751,7 @@ auto Player::consumeJumpEdge() -> bool { const auto& slot = (id_ == 0) ? Options::controls().gamepad_p1 : Options::controls().gamepad_p2; const int JUMP_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::JUMP); - const bool PAD_NOW = (pad != nullptr) && !InputState::isBlocked() && - GamepadBindings::isPressed(pad, JUMP_BTN); + const bool PAD_NOW = (pad != nullptr) && GamepadBindings::isPressed(pad, JUMP_BTN); if (PAD_NOW && !jump_held_gamepad_) { edge = true; } jump_held_gamepad_ = PAD_NOW; @@ -770,7 +762,7 @@ auto Player::consumeAttackEdge() -> bool { // Mateix patró que `consumeJumpEdge` però per al botó d'atac. El botó // físic ve d'`GamepadBindings`, persistit per dispositiu — l'usuari // l'ha pogut remapejar des del menú de servei. - const bool* keys = InputState::getKeyboardState(); + const bool* keys = SDL_GetKeyboardState(nullptr); const bool KEY_NOW = keys[controls_.attack]; bool edge = (KEY_NOW && !attack_held_keyboard_); attack_held_keyboard_ = KEY_NOW; @@ -779,8 +771,7 @@ auto Player::consumeAttackEdge() -> bool { const auto& slot = (id_ == 0) ? Options::controls().gamepad_p1 : Options::controls().gamepad_p2; const int ATK_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::ATTACK); - const bool PAD_NOW = (pad != nullptr) && !InputState::isBlocked() && - GamepadBindings::isPressed(pad, ATK_BTN); + const bool PAD_NOW = (pad != nullptr) && GamepadBindings::isPressed(pad, ATK_BTN); if (PAD_NOW && !attack_held_gamepad_) { edge = true; } attack_held_gamepad_ = PAD_NOW; diff --git a/source/game/ui/service_menu.cpp b/source/game/ui/service_menu.cpp index dfcc4be..4972197 100644 --- a/source/game/ui/service_menu.cpp +++ b/source/game/ui/service_menu.cpp @@ -9,7 +9,6 @@ #include "core/audio/audio.hpp" #include "core/input/gamepad_bindings.hpp" #include "core/input/gamepad_manager.hpp" -#include "core/input/input_state.hpp" #include "core/input/key_config.hpp" #include "core/locale/locale.hpp" #include "core/rendering/canvas.hpp" @@ -246,11 +245,6 @@ void ServiceMenu::toggle() { open_ = true; closing_ = false; animated_h_ = 0.0F; // Comença des de 0 perquè la caixa crega. - // Bloqueja l'input de polling: els consumidors com `Player` veuran - // l'estat del teclat tot-a-zero mentre el menú sigui obert. Els - // events (KEY_DOWN, etc.) ja els consumeix `processMenuInput`, - // així que la navegació del menú no es veu afectada. - InputState::setBlocked(true); } } @@ -259,9 +253,6 @@ void ServiceMenu::close() { open_ = false; closing_ = true; // `open_anim_` decaurà a update; la caixa es veu mentre dura. capturing_ = false; - // Allibera el polling de seguida — la closing animation és purament - // visual i no ha d'impedir que el jugador es mogui un cop ha tancat. - InputState::setBlocked(false); } // `isVisible` inclou l'animació de tancament: retorna true mentre open_anim_>0