This commit is contained in:
2025-10-27 18:35:53 +01:00
parent b1dca32a5b
commit 3179a08dac
63 changed files with 686 additions and 693 deletions

View File

@@ -22,13 +22,13 @@ void Input::destroy() {
}
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
Input* Input::get() {
auto Input::get() -> Input* {
return Input::input;
}
// Constructor
Input::Input(const std::string& game_controller_db_path)
: game_controller_db_path_(game_controller_db_path) {
: game_controller_db_path_(std::move(game_controller_db_path)) {
// Busca si hay mandos conectados
discoverGameControllers();
@@ -60,7 +60,7 @@ void Input::bindGameControllerButton(int controller_index, InputAction input_tar
}
// Comprueba si un input esta activo
bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device, int controller_index) {
auto Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device, int controller_index) -> bool {
bool success_keyboard = false;
bool success_controller = false;
const int INPUT_INDEX = static_cast<int>(input);
@@ -117,13 +117,13 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
}
// Comprueba si hay almenos un input activo
bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) {
auto Input::checkAnyInput(InputDeviceToUse device, int controller_index) -> bool {
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY) {
const bool* key_states = SDL_GetKeyboardState(nullptr);
for (int i = 0; i < (int)key_bindings_.size(); ++i) {
if (static_cast<int>(key_states[key_bindings_[i].scancode]) != 0 && !key_bindings_[i].active) {
key_bindings_[i].active = true;
for (auto& key_binding : key_bindings_) {
if (static_cast<int>(key_states[key_binding.scancode]) != 0 && !key_binding.active) {
key_binding.active = true;
return true;
}
}
@@ -144,7 +144,7 @@ bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) {
}
// Busca si hay mandos conectados
bool Input::discoverGameControllers() {
auto Input::discoverGameControllers() -> bool {
bool found = false;
// Asegúrate de que el subsistema de gamepads está inicializado
@@ -155,7 +155,7 @@ bool Input::discoverGameControllers() {
// Carga el mapping de mandos desde archivo
if (SDL_AddGamepadMappingsFromFile(game_controller_db_path_.c_str()) < 0) {
std::cout << "Error, could not load " << game_controller_db_path_.c_str()
<< " file: " << SDL_GetError() << std::endl;
<< " file: " << SDL_GetError() << '\n';
}
// En SDL3 ya no existe SDL_NumJoysticks()
@@ -177,12 +177,12 @@ bool Input::discoverGameControllers() {
}
}
std::cout << "\n** LOOKING FOR GAME CONTROLLERS" << std::endl;
std::cout << "\n** LOOKING FOR GAME CONTROLLERS" << '\n';
if (num_joysticks_ != num_gamepads_) {
std::cout << "Joysticks found: " << num_joysticks_ << std::endl;
std::cout << "Gamepads found : " << num_gamepads_ << std::endl;
std::cout << "Joysticks found: " << num_joysticks_ << '\n';
std::cout << "Gamepads found : " << num_gamepads_ << '\n';
} else {
std::cout << "Gamepads found: " << num_gamepads_ << std::endl;
std::cout << "Gamepads found: " << num_gamepads_ << '\n';
}
if (num_gamepads_ > 0) {
@@ -195,10 +195,10 @@ bool Input::discoverGameControllers() {
connected_controllers_.push_back(pad);
const char* name = SDL_GetGamepadName(pad);
std::cout << "#" << i << ": " << ((name != nullptr) ? name : "Unknown") << std::endl;
controller_names_.push_back((name != nullptr) ? name : "Unknown");
std::cout << "#" << i << ": " << ((name != nullptr) ? name : "Unknown") << '\n';
controller_names_.emplace_back((name != nullptr) ? name : "Unknown");
} else {
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
std::cout << "SDL_GetError() = " << SDL_GetError() << '\n';
}
}
}
@@ -209,21 +209,21 @@ bool Input::discoverGameControllers() {
SDL_free(joystick_ids);
std::cout << "\n** FINISHED LOOKING FOR GAME CONTROLLERS" << std::endl;
std::cout << "\n** FINISHED LOOKING FOR GAME CONTROLLERS" << '\n';
return found;
}
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound() const { return num_gamepads_ > 0; }
auto Input::gameControllerFound() const -> bool { return num_gamepads_ > 0; }
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int controller_index) const { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
auto Input::getControllerName(int controller_index) const -> std::string { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
// Obten el número de mandos conectados
int Input::getNumControllers() const { return num_gamepads_; }
auto Input::getNumControllers() const -> int { return num_gamepads_; }
// Obtiene el indice del controlador a partir de un event.id
int Input::getJoyIndex(SDL_JoystickID id) const {
auto Input::getJoyIndex(SDL_JoystickID id) const -> int {
for (int i = 0; i < num_joysticks_; ++i) {
if (SDL_GetJoystickID(joysticks_[i]) == id) {
return i;
@@ -233,18 +233,18 @@ int Input::getJoyIndex(SDL_JoystickID id) const {
}
// Obtiene el SDL_GamepadButton asignado a un input
SDL_GamepadButton Input::getControllerBinding(int controller_index, InputAction input) const {
auto Input::getControllerBinding(int controller_index, InputAction input) const -> SDL_GamepadButton {
return controller_bindings_[controller_index][static_cast<int>(input)].button;
}
// Obtiene el indice a partir del nombre del mando
int Input::getIndexByName(const std::string& name) const {
auto it = std::find(controller_names_.begin(), controller_names_.end(), name);
auto Input::getIndexByName(const std::string& name) const -> int {
auto it = std::ranges::find(controller_names_, name);
return it != controller_names_.end() ? std::distance(controller_names_.begin(), it) : -1;
}
// Comprueba el eje del mando
bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat) {
auto Input::checkAxisInput(InputAction input, int controller_index, bool repeat) -> bool {
// Umbral para considerar el eje como activo
const Sint16 THRESHOLD = 30000;
bool axis_active_now = false;