Ja es poden conectar i desconectar mandos en calent, que tot el mon s'entera

This commit is contained in:
2025-08-10 11:02:20 +02:00
parent 8eb8e07e0a
commit 983eb7ee6f
10 changed files with 109 additions and 64 deletions

View File

@@ -308,42 +308,45 @@ void Input::update() {
}
}
auto Input::handleEvent(const SDL_Event &event) -> bool {
auto Input::handleEvent(const SDL_Event &event) -> std::string {
switch (event.type) {
case SDL_EVENT_GAMEPAD_ADDED:
addGamepad(event.gdevice.which);
return true;
return addGamepad(event.gdevice.which);
case SDL_EVENT_GAMEPAD_REMOVED:
removeGamepad(event.gdevice.which);
return true;
return removeGamepad(event.gdevice.which);
}
return false;
return std::string();
}
void Input::addGamepad(int device_index) {
auto Input::addGamepad(int device_index) -> std::string {
SDL_Gamepad *pad = SDL_OpenGamepad(device_index);
if (pad == nullptr) {
std::cerr << "Error al abrir el gamepad: " << SDL_GetError() << std::endl;
return;
return std::string();
}
auto gamepad = std::make_shared<Gamepad>(pad);
std::cout << "Gamepad connected (" << gamepad->name << ")" << std::endl;
auto name = gamepad->name;
std::cout << "Gamepad connected (" << name << ")" << std::endl;
applyGamepadConfig(gamepad);
saveGamepadConfigFromGamepad(gamepad);
gamepads_.push_back(std::move(gamepad));
return name + " CONNECTED";
}
void Input::removeGamepad(SDL_JoystickID id) {
auto it = std::remove_if(gamepads_.begin(), gamepads_.end(), [id](const std::shared_ptr<Gamepad> &gamepad) {
auto Input::removeGamepad(SDL_JoystickID id) -> std::string {
auto it = std::find_if(gamepads_.begin(), gamepads_.end(), [id](const std::shared_ptr<Gamepad> &gamepad) {
return gamepad->instance_id == id;
});
if (it != gamepads_.end()) {
std::cout << "Gamepad disconnected (" << (*it)->name << ")" << std::endl;
gamepads_.erase(it, gamepads_.end());
std::string name = (*it)->name;
std::cout << "Gamepad disconnected (" << name << ")" << std::endl;
gamepads_.erase(it);
return name + " DISCONNECTED";
} else {
std::cerr << "No se encontró el gamepad con ID " << id << std::endl;
return {};
}
}