From c35889a84086b8d3b6aac757b1a4624297d4e1e3 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 22 Mar 2026 23:04:45 +0100 Subject: [PATCH] RW2026 - Abans d'anar li vaig dir a Claude que arreglara coses de input i game pq pareixia que al disparar es perdien inputs --- .gitignore | 5 +++-- source/input.cpp | 23 ++++++++++++++++++++++- source/input.hpp | 1 + source/sections/game.cpp | 3 ++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index e097a98..c954fe1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -.vscode -.claude +.vscode/ +.claude/ +.cache/ build/ data/config/config.txt *.DS_Store diff --git a/source/input.cpp b/source/input.cpp index 33c75ad..cda9a82 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -325,6 +325,17 @@ void Input::initSDLGamePad() { } } +void Input::resetJustPressed() { + for (auto& key : keyboard_.bindings) { + key.second.just_pressed = false; + } + for (auto& gamepad : gamepads_) { + for (auto& binding : gamepad->bindings) { + binding.second.just_pressed = false; + } + } +} + void Input::resetInputStates() { // Resetear todos los KeyBindings.active a false for (auto& key : keyboard_.bindings) { @@ -349,7 +360,7 @@ void Input::update() { bool key_is_down_now = key_states[binding.second.scancode]; // El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo - binding.second.just_pressed = key_is_down_now && !binding.second.is_held; + binding.second.just_pressed = binding.second.just_pressed || (key_is_down_now && !binding.second.is_held); binding.second.is_held = key_is_down_now; } @@ -367,6 +378,16 @@ void Input::update() { auto Input::handleEvent(const SDL_Event& event) -> std::string { switch (event.type) { + case SDL_EVENT_KEY_DOWN: + if (!event.key.repeat) { + for (auto& [action, binding] : keyboard_.bindings) { + if (binding.scancode == event.key.scancode) { + binding.just_pressed = true; + break; + } + } + } + break; case SDL_EVENT_GAMEPAD_ADDED: return addGamepad(event.gdevice.which); case SDL_EVENT_GAMEPAD_REMOVED: diff --git a/source/input.hpp b/source/input.hpp index ce89806..56d1d48 100644 --- a/source/input.hpp +++ b/source/input.hpp @@ -175,6 +175,7 @@ class Input { // --- Métodos de reseteo de estado de entrada --- void resetInputStates(); + void resetJustPressed(); // --- Eventos --- auto handleEvent(const SDL_Event& event) -> std::string; diff --git a/source/sections/game.cpp b/source/sections/game.cpp index d683ebe..d54f9f0 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -984,9 +984,9 @@ void Game::run() { while (Section::name == Section::Name::GAME) { const float DELTA_TIME = calculateDeltaTime(); + handleEvents(); checkInput(); update(DELTA_TIME); - handleEvents(); // Tiene que ir antes del render render(); } } @@ -1128,6 +1128,7 @@ auto Game::allPlayersAreNotPlaying() -> bool { // Comprueba los eventos que hay en cola void Game::handleEvents() { + input_->resetJustPressed(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) {