input: treballant en la nova estructura per als controladors

This commit is contained in:
2025-07-30 10:16:44 +02:00
parent 989f081a25
commit 69ee847575
6 changed files with 129 additions and 74 deletions

View File

@@ -5,6 +5,7 @@
#include <algorithm> // Para find
#include <cstddef> // Para size_t
#include <iterator> // Para distance
#include <memory> // Para std::unique_ptr
#include <unordered_map> // Para unordered_map, _Node_const_iterator, operat...
#include <utility> // Para pair
@@ -54,7 +55,7 @@ void Input::bindGameControllerButton(int controller_index, Action input_target,
}
// Comprueba si un input esta activo
auto Input::checkInput(Action input, bool repeat, Device device, int controller_index) -> bool {
auto Input::checkAction(Action input, bool repeat, Device device, int controller_index) -> bool {
bool success_keyboard = false;
bool success_controller = false;
const int INPUT_INDEX = static_cast<int>(input);
@@ -123,13 +124,13 @@ auto Input::checkAnyButton(bool repeat) -> int {
// Solo comprueba los botones definidos previamente
for (auto bi : button_inputs_) {
// Comprueba el teclado
if (checkInput(bi, repeat, Device::KEYBOARD)) {
if (checkAction(bi, repeat, Device::KEYBOARD)) {
return 1;
}
// Comprueba los mandos
for (int i = 0; i < num_gamepads_; ++i) {
if (checkInput(bi, repeat, Device::CONTROLLER, i)) {
if (checkAction(bi, repeat, Device::CONTROLLER, i)) {
return i + 1;
}
}
@@ -139,7 +140,7 @@ auto Input::checkAnyButton(bool repeat) -> int {
}
// Busca si hay mandos conectados
auto Input::discoverGameControllers() -> bool {
/*auto Input::discoverGameControllers() -> bool {
bool found = false;
if (SDL_AddGamepadMappingsFromFile(game_controller_db_path_.c_str()) < 0) {
@@ -204,7 +205,7 @@ auto Input::discoverGameControllers() -> bool {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> FINISHED LOOKING FOR GAME CONTROLLERS");
return found;
}
}*/
// Comprueba si hay algun mando conectado
auto Input::gameControllerFound() const -> bool { return num_gamepads_ > 0; }
@@ -335,9 +336,10 @@ void Input::initSDLGamePad() {
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GAMEPAD could not initialize! SDL Error: %s", SDL_GetError());
} else {
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "\n** SDL_GAMEPAD: INITIALIZING");
discoverGameControllers();
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "** SDL_GAMEPAD: INITIALIZATION COMPLETE\n");
//SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "\n** SDL_GAMEPAD: INITIALIZING");
//discoverGameControllers();
//printConnectedGamepads();
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "** Input System initialized successfully\n");
}
}
}
@@ -383,21 +385,50 @@ void Input::update() {
void Input::handleEvent(const SDL_Event &event) {
switch (event.type) {
case SDL_EVENT_GAMEPAD_ADDED: {
printf("¡Mando conectado! ID: %d\n", event.gdevice.which);
SDL_Gamepad *gamepad = SDL_OpenGamepad(event.gdevice.which);
if (gamepad) {
printf("Gamepad abierto correctamente.\n");
} else {
printf("Error al abrir el gamepad: %s\n", SDL_GetError());
}
case SDL_EVENT_GAMEPAD_ADDED:
add_gamepad(event.gdevice.which);
break;
}
case SDL_EVENT_GAMEPAD_REMOVED: {
printf("¡Mando desconectado! Instance ID: %d\n", event.gdevice.which);
// Aquí puedes cerrar el gamepad si lo tenías abierto
// SDL_CloseGamepad(gamepad); ← si tienes el puntero guardado
case SDL_EVENT_GAMEPAD_REMOVED:
remove_gamepad(event.gdevice.which);
break;
}
}
}
}
void Input::add_gamepad(int device_index) {
SDL_Gamepad *pad = SDL_OpenGamepad(device_index);
if (!pad) {
std::cerr << "Error al abrir el gamepad: " << SDL_GetError() << "\n";
return;
}
auto gamepad = std::make_unique<Gamepad>(pad);
std::cout << "Gamepad conectado (ID " << gamepad->instance_id << ")\n";
gamepads.push_back(std::move(gamepad));
}
void Input::remove_gamepad(SDL_JoystickID id) {
auto it = std::remove_if(gamepads.begin(), gamepads.end(), [id](const std::unique_ptr<Gamepad> &gamepad) {
return gamepad->instance_id == id;
});
if (it != gamepads.end()) {
std::cout << "Gamepad desconectado (ID " << id << ")\n";
gamepads.erase(it, gamepads.end());
} else {
std::cerr << "No se encontró el gamepad con ID " << id << "\n";
}
}
void Input::printConnectedGamepads() const {
if (gamepads.empty()) {
std::cout << "No hay gamepads conectados.\n";
return;
}
std::cout << "Gamepads conectados:\n";
for (const auto& gamepad : gamepads) {
const char* name = SDL_GetGamepadName(gamepad->pad);
std::cout << " - ID: " << gamepad->instance_id
<< ", Nombre: " << (name ? name : "Desconocido") << "\n";
}
}