diff --git a/source/core/input/input.cpp b/source/core/input/input.cpp index 423b472..e81fa1b 100644 --- a/source/core/input/input.cpp +++ b/source/core/input/input.cpp @@ -104,22 +104,22 @@ void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button) { } // Comprueba si un input esta activo -auto Input::checkInput(Uint8 input, Repeat repeat, int device, int index) -> bool { +auto Input::checkInput(Uint8 input, Repeat repeat, Device device, int index) -> bool { if (!enabled_) { return false; } - if (device == INPUT_USE_ANY) { + if (device == Device::ANY) { index = 0; } bool success_keyboard = false; - if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) { + if (device == Device::KEYBOARD || device == Device::ANY) { success_keyboard = checkKeyboardInput(input, repeat); } bool success_game_controller = false; - if ((device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) && gameControllerFound() && index >= 0 && index < (int)connected_controllers_.size()) { + if ((device == Device::GAMECONTROLLER || device == Device::ANY) && gameControllerFound() && index >= 0 && index < (int)connected_controllers_.size()) { success_game_controller = checkGameControllerInput(input, repeat, index); } @@ -156,12 +156,12 @@ auto Input::checkGameControllerInput(Uint8 input, Repeat repeat, int index) -> b } // Comprueba si hay almenos un input activo -auto Input::checkAnyInput(int device, int index) -> bool { - if (device == INPUT_USE_ANY) { +auto Input::checkAnyInput(Device device, int index) -> bool { + if (device == Device::ANY) { index = 0; } - if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) { + if (device == Device::KEYBOARD || device == Device::ANY) { const bool *key_states = SDL_GetKeyboardState(nullptr); if (std::ranges::any_of(key_bindings_, @@ -171,7 +171,7 @@ auto Input::checkAnyInput(int device, int index) -> bool { } if (gameControllerFound() && index >= 0 && index < (int)connected_controllers_.size()) { - if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) { + if (device == Device::GAMECONTROLLER || device == Device::ANY) { for (auto &game_controller_binding : game_controller_bindings_) { if (SDL_GetGamepadButton(connected_controllers_[index], game_controller_binding.button)) { return true; diff --git a/source/core/input/input.h b/source/core/input/input.h index 78bd0b2..ddc3c44 100644 --- a/source/core/input/input.h +++ b/source/core/input/input.h @@ -6,11 +6,6 @@ #include // for string, basic_string #include // for vector -// Métodos de entrada -constexpr int INPUT_USE_KEYBOARD = 0; -constexpr int INPUT_USE_GAMECONTROLLER = 1; -constexpr int INPUT_USE_ANY = 2; - enum InputAction : std::uint8_t { // Inputs obligatorios INVALID, @@ -53,6 +48,12 @@ class Input { ON }; + enum class Device : std::uint8_t { + KEYBOARD, + GAMECONTROLLER, + ANY + }; + // Singleton API static void init(const std::string &game_controller_db_path); // Crea la instancia static void destroy(); // Libera la instancia @@ -64,8 +65,8 @@ class Input { void bindKey(Uint8 input, SDL_Scancode code); // Asigna inputs a teclas void bindGameControllerButton(Uint8 input, SDL_GamepadButton button); // Asigna inputs a botones del mando - auto checkInput(Uint8 input, Repeat repeat = Repeat::ON, int device = INPUT_USE_ANY, int index = 0) -> bool; // Comprueba si un input esta activo - auto checkAnyInput(int device = INPUT_USE_ANY, int index = 0) -> bool; // Comprueba si hay almenos un input activo + auto checkInput(Uint8 input, Repeat repeat = Repeat::ON, Device device = Device::ANY, int index = 0) -> bool; // Comprueba si un input esta activo + auto checkAnyInput(Device device = Device::ANY, int index = 0) -> bool; // Comprueba si hay almenos un input activo auto discoverGameController() -> bool; // Busca si hay un mando conectado diff --git a/source/core/system/director.cpp b/source/core/system/director.cpp index e6a4d4d..31de17a 100644 --- a/source/core/system/director.cpp +++ b/source/core/system/director.cpp @@ -17,7 +17,7 @@ #include // for basic_string, operator+, char_t... #include "core/audio/audio.hpp" // for Audio::init, Audio::destroy -#include "core/input/input.h" // for Input, InputAction, INPUT_USE_GAME... +#include "core/input/input.h" // for Input, InputAction #include "core/input/mouse.hpp" // for Mouse::handleEvent, Mouse::upda... #include "core/locale/lang.h" // for Lang, Lang::Code #include "core/rendering/screen.h" // for Screen diff --git a/source/game/game.cpp b/source/game/game.cpp index c2ad511..9d95a42 100644 --- a/source/game/game.cpp +++ b/source/game/game.cpp @@ -70,7 +70,7 @@ Game::Game(int num_players, int current_stage, SDL_Renderer *renderer, bool demo #endif if (num_players == 1) { // Si solo juega un jugador, permite jugar tanto con teclado como con mando player_one_control_ = Options::inputs[0].device_type; - Options::inputs[0].device_type = INPUT_USE_ANY; + Options::inputs[0].device_type = Input::Device::ANY; } difficulty_ = Options::settings.difficulty; diff --git a/source/game/game.h b/source/game/game.h index 38ba59d..ad9eb9e 100644 --- a/source/game/game.h +++ b/source/game/game.h @@ -372,7 +372,7 @@ class Game { Uint8 difficulty_; // Dificultad del juego float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad Color difficulty_color_; // Color asociado a la dificultad - Uint8 player_one_control_; // Variable para almacenar el valor de las opciones + Input::Device player_one_control_; // Variable para almacenar el valor de las opciones EnemyFormation enemy_formation_[NUMBER_OF_ENEMY_FORMATIONS]; // Vector con todas las formaciones enemigas EnemyPool enemy_pool_[10]; // Variable con los diferentes conjuntos de formaciones enemigas Uint8 last_stage_reached_; // Contiene el numero de la última pantalla que se ha alcanzado diff --git a/source/game/options.cpp b/source/game/options.cpp index 4dd4f52..c679af5 100644 --- a/source/game/options.cpp +++ b/source/game/options.cpp @@ -7,7 +7,7 @@ #include #include -#include "core/input/input.h" // for INPUT_USE_KEYBOARD, INPUT_USE_GAMECONTROLLER +#include "core/input/input.h" // for Input::Device::KEYBOARD, Input::Device::GAMECONTROLLER #include "core/locale/lang.h" // for Lang::Code, Lang::MAX_LANGUAGES #include "external/fkyaml_node.hpp" // for fkyaml::node #include "utils/utils.h" // for boolToString @@ -163,9 +163,9 @@ namespace Options { size_t i = 0; for (const auto &entry : ins) { if (i >= inputs.size()) { break; } - int device_type_int = inputs[i].device_type; + int device_type_int = static_cast(inputs[i].device_type); if (tryGet(entry, "device_type", device_type_int)) { - inputs[i].device_type = static_cast(device_type_int); + inputs[i].device_type = static_cast(device_type_int); } ++i; } @@ -202,13 +202,13 @@ namespace Options { InputDevice kb; kb.id = 0; kb.name = "KEYBOARD"; - kb.device_type = INPUT_USE_KEYBOARD; + kb.device_type = Input::Device::KEYBOARD; inputs.push_back(kb); InputDevice gc; gc.id = 0; gc.name = "GAME CONTROLLER"; - gc.device_type = INPUT_USE_GAMECONTROLLER; + gc.device_type = Input::Device::GAMECONTROLLER; inputs.push_back(gc); } @@ -325,8 +325,8 @@ namespace Options { // INPUT file << "# INPUT DEVICES (device_type: " - << static_cast(INPUT_USE_KEYBOARD) << "=KEYBOARD, " - << static_cast(INPUT_USE_GAMECONTROLLER) << "=GAMECONTROLLER)\n"; + << static_cast(Input::Device::KEYBOARD) << "=KEYBOARD, " + << static_cast(Input::Device::GAMECONTROLLER) << "=GAMECONTROLLER)\n"; file << "input:\n"; for (size_t i = 0; i < inputs.size(); ++i) { file << " - slot: " << i << "\n"; diff --git a/source/game/scenes/title.cpp b/source/game/scenes/title.cpp index 11a3e76..b0689a2 100644 --- a/source/game/scenes/title.cpp +++ b/source/game/scenes/title.cpp @@ -8,7 +8,7 @@ #include "core/audio/audio.hpp" // for Audio #include "core/input/global_inputs.hpp" // for GlobalInputs::handle -#include "core/input/input.h" // for Input, INPUT_USE_GAMECONTROLLER, INPUT_... +#include "core/input/input.h" // for Input, Input::Device::GAMECONTROLLER, INPUT_... #include "core/locale/lang.h" // for Lang, Lang::Code #include "core/rendering/animatedsprite.h" // for AnimatedSprite #include "core/rendering/fade.h" // for Fade @@ -115,12 +115,12 @@ void Title::init() { InputDevice inp; inp.id = 0; inp.name = "KEYBOARD"; - inp.device_type = INPUT_USE_KEYBOARD; + inp.device_type = Input::Device::KEYBOARD; Options::inputs.push_back(inp); inp.id = 0; inp.name = "GAME CONTROLLER"; - inp.device_type = INPUT_USE_GAMECONTROLLER; + inp.device_type = Input::Device::GAMECONTROLLER; Options::inputs.push_back(inp); // Comprueba si hay mandos conectados @@ -676,12 +676,12 @@ void Title::updateMenuLabels() const { i++; // PLAYER 1 CONTROLS - OPTIONS switch (Options::inputs[0].device_type) { - case INPUT_USE_KEYBOARD: + case Input::Device::KEYBOARD: menu_.options->setItemCaption(i, Lang::get()->getText(69)); // KEYBOARD menu_.options->setGreyed(i, false); break; - case INPUT_USE_GAMECONTROLLER: + case Input::Device::GAMECONTROLLER: menu_.options->setItemCaption(i, Lang::get()->getText(70)); // GAME CONTROLLER if (!Input::get()->gameControllerFound()) { menu_.options->setGreyed(i, true); @@ -703,12 +703,12 @@ void Title::updateMenuLabels() const { i++; // PLAYER 2 CONTROLS - OPTIONS switch (Options::inputs[1].device_type) { - case INPUT_USE_KEYBOARD: + case Input::Device::KEYBOARD: menu_.options->setItemCaption(i, Lang::get()->getText(69)); // KEYBOARD menu_.options->setGreyed(i, false); break; - case INPUT_USE_GAMECONTROLLER: + case Input::Device::GAMECONTROLLER: menu_.options->setItemCaption(i, Lang::get()->getText(70)); // GAME CONTROLLER if (!Input::get()->gameControllerFound()) { menu_.options->setGreyed(i, true); @@ -963,11 +963,11 @@ auto Title::updatePlayerInputs(int num_player) -> bool { Options::inputs[0].id = -1; Options::inputs[0].name = "KEYBOARD"; - Options::inputs[0].device_type = INPUT_USE_KEYBOARD; + Options::inputs[0].device_type = Input::Device::KEYBOARD; Options::inputs[1].id = 0; Options::inputs[1].name = "GAME CONTROLLER"; - Options::inputs[1].device_type = INPUT_USE_GAMECONTROLLER; + Options::inputs[1].device_type = Input::Device::GAMECONTROLLER; return true; } // Si hay mas de un dispositivo, se recorre el vector @@ -1059,7 +1059,7 @@ void Title::checkInputDevices() { for (int i = 0; i < NUM_CONTROLLERS; ++i) { temp.id = i; temp.name = Input::get()->getControllerName(i); - temp.device_type = INPUT_USE_GAMECONTROLLER; + temp.device_type = Input::Device::GAMECONTROLLER; available_input_devices_.push_back(temp); if (Options::settings.console) { std::cout << "Device " << (int)available_input_devices_.size() << " - " << temp.name.c_str() << '\n'; @@ -1070,7 +1070,7 @@ void Title::checkInputDevices() { // Añade el teclado al final temp.id = -1; temp.name = "KEYBOARD"; - temp.device_type = INPUT_USE_KEYBOARD; + temp.device_type = Input::Device::KEYBOARD; available_input_devices_.push_back(temp); if (Options::settings.console) { std::cout << "Device " << (int)available_input_devices_.size() << " - " << temp.name.c_str() << '\n'; diff --git a/source/utils/utils.h b/source/utils/utils.h index a291de5..5f43eac 100644 --- a/source/utils/utils.h +++ b/source/utils/utils.h @@ -4,6 +4,8 @@ #include // for string, basic_string +#include "core/input/input.h" // for Input::Device + // Dificultad del juego constexpr int DIFFICULTY_EASY = 0; constexpr int DIFFICULTY_NORMAL = 1; @@ -70,9 +72,9 @@ struct DemoKeys { // Estructura para albergar métodos de control struct InputDevice { - int id; // Identificador en el vector de mandos - std::string name; // Nombre del dispositivo - Uint8 device_type; // Tipo de dispositivo (teclado o mando) + int id; // Identificador en el vector de mandos + std::string name; // Nombre del dispositivo + Input::Device device_type; // Tipo de dispositivo (teclado o mando) }; // Calcula el cuadrado de la distancia entre dos puntos