diff --git a/source/common/input.cpp b/source/common/input.cpp index b683157..9602cf4 100644 --- a/source/common/input.cpp +++ b/source/common/input.cpp @@ -21,6 +21,7 @@ Input::Input(std::string file) verbose = true; enabled = true; + gameInputs.clear(); gameInputs.push_back(input_fire_left); gameInputs.push_back(input_fire_center); gameInputs.push_back(input_fire_right); @@ -28,6 +29,12 @@ Input::Input(std::string file) gameInputs.push_back(input_down); gameInputs.push_back(input_left); gameInputs.push_back(input_right); + + buttonInputs.clear(); + buttonInputs.push_back(input_fire_left); + buttonInputs.push_back(input_fire_center); + buttonInputs.push_back(input_fire_right); + buttonInputs.push_back(input_start); } // Actualiza el estado del objeto @@ -195,6 +202,30 @@ bool Input::checkAnyInput(int device, int index) return false; } +// Comprueba si hay algún botón pulsado +bool Input::checkAnyButtonPressed() +{ + const Uint8 *keyStates = SDL_GetKeyboardState(nullptr); + + for (auto bi : buttonInputs) + { + if (keyStates[keyBindings[bi].scancode] != 0) + { + return true; + } + + for (int i = 0; i < numGamepads; ++i) + { + if (SDL_GameControllerGetButton(connectedControllers[i], gameControllerBindings[bi].button) != 0) + { + return true; + } + } + } + + return false; +} + // Busca si hay un mando conectado bool Input::discoverGameController() { @@ -244,7 +275,7 @@ bool Input::discoverGameController() connectedControllers.push_back(pad); const std::string separator(" #"); std::string name = SDL_GameControllerNameForIndex(i); - //name.resize(25); + // name.resize(25); name = name + separator + std::to_string(i); if (verbose) { diff --git a/source/common/input.h b/source/common/input.h index bc3a3bf..1ececdf 100644 --- a/source/common/input.h +++ b/source/common/input.h @@ -24,6 +24,7 @@ enum inputs_e input_fire_left, input_fire_center, input_fire_right, + input_start, // Inputs estandar input_window_fullscreen, @@ -70,6 +71,7 @@ private: std::vector gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos std::vector controllerNames; // Vector con los nombres de los mandos std::vector gameInputs; // Inputs usados para jugar, normalmente direcciones y botones + std::vector buttonInputs; // Inputs asignados al jugador y a botones, excluyendo direcciones int numGamepads; // Numero de mandos conectados std::string dbPath; // Ruta al archivo gamecontrollerdb.txt bool verbose; // Indica si ha de mostrar mensajes @@ -95,6 +97,9 @@ public: // Comprueba si hay almenos un input activo bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0); + // Comprueba si hay algún botón pulsado + bool checkAnyButtonPressed(); + // Busca si hay un mando conectado bool discoverGameController(); diff --git a/source/director.cpp b/source/director.cpp index f1dcb57..55e1b33 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -111,6 +111,7 @@ void Director::initInput() input->bindKey(input_fire_left, SDL_SCANCODE_Q); input->bindKey(input_fire_center, SDL_SCANCODE_W); input->bindKey(input_fire_right, SDL_SCANCODE_E); + input->bindKey(input_start, SDL_SCANCODE_RETURN); // Teclado - Otros input->bindKey(input_accept, SDL_SCANCODE_RETURN); @@ -130,6 +131,7 @@ void Director::initInput() input->bindGameControllerButton(input_fire_left, SDL_CONTROLLER_BUTTON_X); input->bindGameControllerButton(input_fire_center, SDL_CONTROLLER_BUTTON_Y); input->bindGameControllerButton(input_fire_right, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); + input->bindGameControllerButton(input_start, SDL_CONTROLLER_BUTTON_START); // Mando - Otros input->bindGameControllerButton(input_accept, SDL_CONTROLLER_BUTTON_START); diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index 588b514..31eb26c 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -177,7 +177,7 @@ void HiScoreTable::checkInput() section->name = SECTION_PROG_QUIT; } - else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) + else if (input->checkAnyButtonPressed()) { JA_StopMusic(); section->name = SECTION_PROG_TITLE; diff --git a/source/instructions.cpp b/source/instructions.cpp index eaa3d6c..ce4c869 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -309,7 +309,7 @@ void Instructions::checkInput() section->name = SECTION_PROG_QUIT; } - else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) + else if (input->checkAnyButtonPressed()) { JA_StopMusic(); section->name = SECTION_PROG_TITLE; diff --git a/source/intro.cpp b/source/intro.cpp index 6f570aa..a860536 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -200,7 +200,7 @@ void Intro::checkInput() section->name = SECTION_PROG_QUIT; } - else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE)) + else if (input->checkAnyButtonPressed()) { JA_StopMusic(); section->name = SECTION_PROG_TITLE; diff --git a/source/logo.cpp b/source/logo.cpp index 8b96a4e..5ce624d 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -106,7 +106,7 @@ void Logo::checkInput() section->name = SECTION_PROG_QUIT; } - else if (input->checkAnyInput()) + else if (input->checkAnyButtonPressed()) { section->name = SECTION_PROG_TITLE; section->subsection = SUBSECTION_TITLE_1;