INPUT_USE_* → enum class Input::Device

This commit is contained in:
2026-05-16 19:54:52 +02:00
parent d72630523a
commit 40e1140734
8 changed files with 42 additions and 39 deletions
+8 -8
View File
@@ -104,22 +104,22 @@ void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button) {
} }
// Comprueba si un input esta activo // 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_) { if (!enabled_) {
return false; return false;
} }
if (device == INPUT_USE_ANY) { if (device == Device::ANY) {
index = 0; index = 0;
} }
bool success_keyboard = false; 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); success_keyboard = checkKeyboardInput(input, repeat);
} }
bool success_game_controller = false; 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); 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 // Comprueba si hay almenos un input activo
auto Input::checkAnyInput(int device, int index) -> bool { auto Input::checkAnyInput(Device device, int index) -> bool {
if (device == INPUT_USE_ANY) { if (device == Device::ANY) {
index = 0; 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); const bool *key_states = SDL_GetKeyboardState(nullptr);
if (std::ranges::any_of(key_bindings_, 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 (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_) { for (auto &game_controller_binding : game_controller_bindings_) {
if (SDL_GetGamepadButton(connected_controllers_[index], game_controller_binding.button)) { if (SDL_GetGamepadButton(connected_controllers_[index], game_controller_binding.button)) {
return true; return true;
+8 -7
View File
@@ -6,11 +6,6 @@
#include <string> // for string, basic_string #include <string> // for string, basic_string
#include <vector> // for vector #include <vector> // 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 { enum InputAction : std::uint8_t {
// Inputs obligatorios // Inputs obligatorios
INVALID, INVALID,
@@ -53,6 +48,12 @@ class Input {
ON ON
}; };
enum class Device : std::uint8_t {
KEYBOARD,
GAMECONTROLLER,
ANY
};
// Singleton API // Singleton API
static void init(const std::string &game_controller_db_path); // Crea la instancia static void init(const std::string &game_controller_db_path); // Crea la instancia
static void destroy(); // Libera 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 bindKey(Uint8 input, SDL_Scancode code); // Asigna inputs a teclas
void bindGameControllerButton(Uint8 input, SDL_GamepadButton button); // Asigna inputs a botones del mando 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 checkInput(Uint8 input, Repeat repeat = Repeat::ON, Device device = Device::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 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 auto discoverGameController() -> bool; // Busca si hay un mando conectado
+1 -1
View File
@@ -17,7 +17,7 @@
#include <string> // for basic_string, operator+, char_t... #include <string> // for basic_string, operator+, char_t...
#include "core/audio/audio.hpp" // for Audio::init, Audio::destroy #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/input/mouse.hpp" // for Mouse::handleEvent, Mouse::upda...
#include "core/locale/lang.h" // for Lang, Lang::Code #include "core/locale/lang.h" // for Lang, Lang::Code
#include "core/rendering/screen.h" // for Screen #include "core/rendering/screen.h" // for Screen
+1 -1
View File
@@ -70,7 +70,7 @@ Game::Game(int num_players, int current_stage, SDL_Renderer *renderer, bool demo
#endif #endif
if (num_players == 1) { // Si solo juega un jugador, permite jugar tanto con teclado como con mando 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; 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; difficulty_ = Options::settings.difficulty;
+1 -1
View File
@@ -372,7 +372,7 @@ class Game {
Uint8 difficulty_; // Dificultad del juego Uint8 difficulty_; // Dificultad del juego
float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad
Color difficulty_color_; // Color asociado a 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 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 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 Uint8 last_stage_reached_; // Contiene el numero de la última pantalla que se ha alcanzado
+7 -7
View File
@@ -7,7 +7,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#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 "core/locale/lang.h" // for Lang::Code, Lang::MAX_LANGUAGES
#include "external/fkyaml_node.hpp" // for fkyaml::node #include "external/fkyaml_node.hpp" // for fkyaml::node
#include "utils/utils.h" // for boolToString #include "utils/utils.h" // for boolToString
@@ -163,9 +163,9 @@ namespace Options {
size_t i = 0; size_t i = 0;
for (const auto &entry : ins) { for (const auto &entry : ins) {
if (i >= inputs.size()) { break; } if (i >= inputs.size()) { break; }
int device_type_int = inputs[i].device_type; int device_type_int = static_cast<int>(inputs[i].device_type);
if (tryGet<int>(entry, "device_type", device_type_int)) { if (tryGet<int>(entry, "device_type", device_type_int)) {
inputs[i].device_type = static_cast<Uint8>(device_type_int); inputs[i].device_type = static_cast<Input::Device>(device_type_int);
} }
++i; ++i;
} }
@@ -202,13 +202,13 @@ namespace Options {
InputDevice kb; InputDevice kb;
kb.id = 0; kb.id = 0;
kb.name = "KEYBOARD"; kb.name = "KEYBOARD";
kb.device_type = INPUT_USE_KEYBOARD; kb.device_type = Input::Device::KEYBOARD;
inputs.push_back(kb); inputs.push_back(kb);
InputDevice gc; InputDevice gc;
gc.id = 0; gc.id = 0;
gc.name = "GAME CONTROLLER"; gc.name = "GAME CONTROLLER";
gc.device_type = INPUT_USE_GAMECONTROLLER; gc.device_type = Input::Device::GAMECONTROLLER;
inputs.push_back(gc); inputs.push_back(gc);
} }
@@ -325,8 +325,8 @@ namespace Options {
// INPUT // INPUT
file << "# INPUT DEVICES (device_type: " file << "# INPUT DEVICES (device_type: "
<< static_cast<int>(INPUT_USE_KEYBOARD) << "=KEYBOARD, " << static_cast<int>(Input::Device::KEYBOARD) << "=KEYBOARD, "
<< static_cast<int>(INPUT_USE_GAMECONTROLLER) << "=GAMECONTROLLER)\n"; << static_cast<int>(Input::Device::GAMECONTROLLER) << "=GAMECONTROLLER)\n";
file << "input:\n"; file << "input:\n";
for (size_t i = 0; i < inputs.size(); ++i) { for (size_t i = 0; i < inputs.size(); ++i) {
file << " - slot: " << i << "\n"; file << " - slot: " << i << "\n";
+11 -11
View File
@@ -8,7 +8,7 @@
#include "core/audio/audio.hpp" // for Audio #include "core/audio/audio.hpp" // for Audio
#include "core/input/global_inputs.hpp" // for GlobalInputs::handle #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/locale/lang.h" // for Lang, Lang::Code
#include "core/rendering/animatedsprite.h" // for AnimatedSprite #include "core/rendering/animatedsprite.h" // for AnimatedSprite
#include "core/rendering/fade.h" // for Fade #include "core/rendering/fade.h" // for Fade
@@ -115,12 +115,12 @@ void Title::init() {
InputDevice inp; InputDevice inp;
inp.id = 0; inp.id = 0;
inp.name = "KEYBOARD"; inp.name = "KEYBOARD";
inp.device_type = INPUT_USE_KEYBOARD; inp.device_type = Input::Device::KEYBOARD;
Options::inputs.push_back(inp); Options::inputs.push_back(inp);
inp.id = 0; inp.id = 0;
inp.name = "GAME CONTROLLER"; inp.name = "GAME CONTROLLER";
inp.device_type = INPUT_USE_GAMECONTROLLER; inp.device_type = Input::Device::GAMECONTROLLER;
Options::inputs.push_back(inp); Options::inputs.push_back(inp);
// Comprueba si hay mandos conectados // Comprueba si hay mandos conectados
@@ -676,12 +676,12 @@ void Title::updateMenuLabels() const {
i++; i++;
// PLAYER 1 CONTROLS - OPTIONS // PLAYER 1 CONTROLS - OPTIONS
switch (Options::inputs[0].device_type) { 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->setItemCaption(i, Lang::get()->getText(69)); // KEYBOARD
menu_.options->setGreyed(i, false); menu_.options->setGreyed(i, false);
break; break;
case INPUT_USE_GAMECONTROLLER: case Input::Device::GAMECONTROLLER:
menu_.options->setItemCaption(i, Lang::get()->getText(70)); // GAME CONTROLLER menu_.options->setItemCaption(i, Lang::get()->getText(70)); // GAME CONTROLLER
if (!Input::get()->gameControllerFound()) { if (!Input::get()->gameControllerFound()) {
menu_.options->setGreyed(i, true); menu_.options->setGreyed(i, true);
@@ -703,12 +703,12 @@ void Title::updateMenuLabels() const {
i++; i++;
// PLAYER 2 CONTROLS - OPTIONS // PLAYER 2 CONTROLS - OPTIONS
switch (Options::inputs[1].device_type) { 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->setItemCaption(i, Lang::get()->getText(69)); // KEYBOARD
menu_.options->setGreyed(i, false); menu_.options->setGreyed(i, false);
break; break;
case INPUT_USE_GAMECONTROLLER: case Input::Device::GAMECONTROLLER:
menu_.options->setItemCaption(i, Lang::get()->getText(70)); // GAME CONTROLLER menu_.options->setItemCaption(i, Lang::get()->getText(70)); // GAME CONTROLLER
if (!Input::get()->gameControllerFound()) { if (!Input::get()->gameControllerFound()) {
menu_.options->setGreyed(i, true); menu_.options->setGreyed(i, true);
@@ -963,11 +963,11 @@ auto Title::updatePlayerInputs(int num_player) -> bool {
Options::inputs[0].id = -1; Options::inputs[0].id = -1;
Options::inputs[0].name = "KEYBOARD"; 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].id = 0;
Options::inputs[1].name = "GAME CONTROLLER"; Options::inputs[1].name = "GAME CONTROLLER";
Options::inputs[1].device_type = INPUT_USE_GAMECONTROLLER; Options::inputs[1].device_type = Input::Device::GAMECONTROLLER;
return true; return true;
} // Si hay mas de un dispositivo, se recorre el vector } // 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) { for (int i = 0; i < NUM_CONTROLLERS; ++i) {
temp.id = i; temp.id = i;
temp.name = Input::get()->getControllerName(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); available_input_devices_.push_back(temp);
if (Options::settings.console) { if (Options::settings.console) {
std::cout << "Device " << (int)available_input_devices_.size() << " - " << temp.name.c_str() << '\n'; 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 // Añade el teclado al final
temp.id = -1; temp.id = -1;
temp.name = "KEYBOARD"; temp.name = "KEYBOARD";
temp.device_type = INPUT_USE_KEYBOARD; temp.device_type = Input::Device::KEYBOARD;
available_input_devices_.push_back(temp); available_input_devices_.push_back(temp);
if (Options::settings.console) { if (Options::settings.console) {
std::cout << "Device " << (int)available_input_devices_.size() << " - " << temp.name.c_str() << '\n'; std::cout << "Device " << (int)available_input_devices_.size() << " - " << temp.name.c_str() << '\n';
+3 -1
View File
@@ -4,6 +4,8 @@
#include <string> // for string, basic_string #include <string> // for string, basic_string
#include "core/input/input.h" // for Input::Device
// Dificultad del juego // Dificultad del juego
constexpr int DIFFICULTY_EASY = 0; constexpr int DIFFICULTY_EASY = 0;
constexpr int DIFFICULTY_NORMAL = 1; constexpr int DIFFICULTY_NORMAL = 1;
@@ -72,7 +74,7 @@ struct DemoKeys {
struct InputDevice { struct InputDevice {
int id; // Identificador en el vector de mandos int id; // Identificador en el vector de mandos
std::string name; // Nombre del dispositivo std::string name; // Nombre del dispositivo
Uint8 device_type; // Tipo de dispositivo (teclado o mando) Input::Device device_type; // Tipo de dispositivo (teclado o mando)
}; };
// Calcula el cuadrado de la distancia entre dos puntos // Calcula el cuadrado de la distancia entre dos puntos