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

@@ -2,6 +2,8 @@
#include <SDL3/SDL.h> // Para SDL_GamepadButton, Uint8, SDL_Gamepad, SDL_Joystick, SDL_JoystickID, SDL_Scancode, Sint16
#include <iostream>
#include <memory> // Para std::unique_ptr
#include <string> // Para basic_string, string
#include <vector> // Para vector
@@ -81,13 +83,14 @@ class Input {
void bindGameControllerButton(int controller_index, Action input_target, Action input_source); // Asigna inputs a otros inputs del mando
// --- Métodos de consulta de entrada ---
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
auto checkInput(Action input, bool repeat = true, Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si un input está activo
auto checkAnyInput(Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si hay al menos un input activo
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> int; // Comprueba si hay algún botón pulsado
void update(); // Comprueba fisicamente los botones y teclas que se han pulsado
auto checkAction(Action input, bool repeat = true, Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si un input está activo
auto checkAnyInput(Device device = Device::ANY, int controller_index = 0) -> bool; // Comprueba si hay al menos un input activo
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> int; // Comprueba si hay algún botón pulsado
// --- Métodos de gestión de mandos ---
auto discoverGameControllers() -> bool; // Busca si hay mandos conectados
//auto discoverGameControllers() -> bool; // Busca si hay mandos conectados
//void discoverGameControllers();
[[nodiscard]] auto gameControllerFound() const -> bool; // Comprueba si hay algún mando conectado
[[nodiscard]] auto getNumControllers() const -> int; // Obtiene el número de mandos conectados
[[nodiscard]] auto getControllerName(int controller_index) const -> std::string; // Obtiene el nombre de un mando de juego
@@ -106,6 +109,9 @@ class Input {
// --- Eventos ---
void handleEvent(const SDL_Event &event); // Comprueba si se conecta algun mando
void printConnectedGamepads() const;
private:
// --- Estructuras internas ---
struct KeyBindings {
@@ -127,6 +133,21 @@ class Input {
: button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
};
struct Gamepad {
SDL_Gamepad *pad;
SDL_JoystickID instance_id;
Gamepad(SDL_Gamepad *p)
: pad(p), instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(p))) {}
~Gamepad() {
if (pad) {
SDL_CloseGamepad(pad);
std::cout << "Gamepad cerrado (ID " << instance_id << ")\n";
}
}
};
// --- Constantes ---
static constexpr Sint16 AXIS_THRESHOLD = 30000;
@@ -140,10 +161,13 @@ class Input {
int num_joysticks_ = 0; // Número de joysticks conectados
int num_gamepads_ = 0; // Número de mandos conectados
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
std::vector<std::unique_ptr<Gamepad>> gamepads;
// --- Métodos internos ---
void initSDLGamePad(); // Inicializa SDL para la gestión de mandos
auto checkAxisInput(Action input, int controller_index, bool repeat) -> bool; // Comprueba el eje del mando
void add_gamepad(int device_index);
void remove_gamepad(SDL_JoystickID id);
// --- Constructor y destructor ---
explicit Input(std::string game_controller_db_path); // Constructor privado