commit mañanero de canvi de pc

This commit is contained in:
2025-07-31 07:17:14 +02:00
parent dbecd9a22b
commit 2d9a6e744e
6 changed files with 257 additions and 89 deletions

View File

@@ -81,10 +81,11 @@ Director::~Director() {
// Inicializa todo // Inicializa todo
void Director::init() { void Director::init() {
// Configuración inicial de recursos // Configuración inicial de parametros
Asset::init(executable_path_); // Inicializa el sistema de gestión de archivos Asset::init(executable_path_); // Inicializa el sistema de gestión de archivos
setFileList(); // Crea el índice de archivos setFileList(); // Crea el índice de archivos
Options::setFile(Asset::get()->get("config.txt")); // Establece el fichero de configuración Options::setConfigFile(Asset::get()->get("config.txt")); // Establece el fichero de configuración
Options::setControllersFile(Asset::get()->get("controllers.json")); // Establece el fichero de configuración de mandos
Options::loadFromFile(); // Carga el archivo de configuración Options::loadFromFile(); // Carga el archivo de configuración
loadParams(); // Carga los parámetros del programa loadParams(); // Carga los parámetros del programa
loadScoreFile(); // Carga el archivo de puntuaciones loadScoreFile(); // Carga el archivo de puntuaciones
@@ -255,6 +256,7 @@ void Director::setFileList() {
// Ficheros de configuración // Ficheros de configuración
Asset::get()->add(system_folder_ + "/config.txt", AssetType::DATA, false, true); Asset::get()->add(system_folder_ + "/config.txt", AssetType::DATA, false, true);
Asset::get()->add(system_folder_ + "/controllers.json", AssetType::DATA, false, true);
Asset::get()->add(system_folder_ + "/score.bin", AssetType::DATA, false, true); Asset::get()->add(system_folder_ + "/score.bin", AssetType::DATA, false, true);
Asset::get()->add(PREFIX + "/data/config/param_320x240.txt", AssetType::DATA); Asset::get()->add(PREFIX + "/data/config/param_320x240.txt", AssetType::DATA);
Asset::get()->add(PREFIX + "/data/config/param_320x256.txt", AssetType::DATA); Asset::get()->add(PREFIX + "/data/config/param_320x256.txt", AssetType::DATA);

View File

@@ -0,0 +1,191 @@
#include <external/json.hpp>
#pragma once
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "input.h" // Para Input
// Mapas para convertir entre enums y strings
std::unordered_map<Input::Action, std::string> actionToString = {
{Input::Action::FIRE_LEFT, "FIRE_LEFT"},
{Input::Action::FIRE_CENTER, "FIRE_CENTER"},
{Input::Action::FIRE_RIGHT, "FIRE_RIGHT"},
{Input::Action::START, "START"},
{Input::Action::SERVICE, "SERVICE"}};
std::unordered_map<std::string, Input::Action> stringToAction = {
{"FIRE_LEFT", Input::Action::FIRE_LEFT},
{"FIRE_CENTER", Input::Action::FIRE_CENTER},
{"FIRE_RIGHT", Input::Action::FIRE_RIGHT},
{"START", Input::Action::START},
{"SERVICE", Input::Action::SERVICE}};
std::unordered_map<SDL_GamepadButton, std::string> buttonToString = {
{SDL_GAMEPAD_BUTTON_WEST, "WEST"},
{SDL_GAMEPAD_BUTTON_NORTH, "NORTH"},
{SDL_GAMEPAD_BUTTON_EAST, "EAST"},
{SDL_GAMEPAD_BUTTON_SOUTH, "SOUTH"},
{SDL_GAMEPAD_BUTTON_START, "START"},
{SDL_GAMEPAD_BUTTON_BACK, "BACK"},
{SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, "LEFT_SHOULDER"},
{SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, "RIGHT_SHOULDER"}
// Añade todos los botones que necesites
};
std::unordered_map<std::string, SDL_GamepadButton> stringToButton = {
{"WEST", SDL_GAMEPAD_BUTTON_WEST},
{"NORTH", SDL_GAMEPAD_BUTTON_NORTH},
{"EAST", SDL_GAMEPAD_BUTTON_EAST},
{"SOUTH", SDL_GAMEPAD_BUTTON_SOUTH},
{"START", SDL_GAMEPAD_BUTTON_START},
{"BACK", SDL_GAMEPAD_BUTTON_BACK},
{"LEFT_SHOULDER", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER},
{"RIGHT_SHOULDER", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER}
// Añade todos los botones que necesites
};
struct GamepadConfig {
std::string name; // Nombre del dispositivo
std::unordered_map<Input::Action, SDL_GamepadButton> bindings; // Asociación acción-botón
GamepadConfig(std::string name = "")
: name(name),
bindings{
{Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_WEST},
{Input::Action::FIRE_CENTER, SDL_GAMEPAD_BUTTON_NORTH},
{Input::Action::FIRE_RIGHT, SDL_GAMEPAD_BUTTON_EAST},
{Input::Action::START, SDL_GAMEPAD_BUTTON_START},
{Input::Action::SERVICE, SDL_GAMEPAD_BUTTON_BACK}} {}
// Reasigna un botón a una acción
void rebindAction(Input::Action action, SDL_GamepadButton new_button) {
bindings[action] = new_button;
}
};
class GamepadConfigManager {
public:
// Escribir vector de GamepadConfig a archivo JSON
static bool writeToJson(const std::vector<GamepadConfig>& configs, const std::string& filename) {
try {
nlohmann::json j;
j["gamepads"] = nlohmann::json::array();
for (const auto& config : configs) {
nlohmann::json gamepadJson;
gamepadJson["name"] = config.name;
gamepadJson["bindings"] = nlohmann::json::object();
// Convertir bindings a JSON
for (const auto& [action, button] : config.bindings) {
std::string actionStr = actionToString[action];
std::string buttonStr = buttonToString[button];
gamepadJson["bindings"][actionStr] = buttonStr;
}
j["gamepads"].push_back(gamepadJson);
}
// Escribir al archivo
std::ofstream file(filename);
if (!file.is_open()) {
return false;
}
file << j.dump(4); // Formato con indentación de 4 espacios
file.close();
return true;
} catch (const std::exception& e) {
// Log del error si tienes sistema de logging
return false;
}
}
// Leer vector de GamepadConfig desde archivo JSON
static bool readFromJson(std::vector<GamepadConfig>& configs, const std::string& filename) {
try {
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}
nlohmann::json j;
file >> j;
file.close();
configs.clear();
if (!j.contains("gamepads") || !j["gamepads"].is_array()) {
return false;
}
for (const auto& gamepadJson : j["gamepads"]) {
if (!gamepadJson.contains("name") || !gamepadJson.contains("bindings")) {
continue; // Saltar configuraciones malformadas
}
GamepadConfig config(gamepadJson["name"]);
// Limpiar bindings por defecto para cargar los del archivo
config.bindings.clear();
// Cargar bindings desde JSON
for (const auto& [actionStr, buttonStr] : gamepadJson["bindings"].items()) {
auto actionIt = stringToAction.find(actionStr);
auto buttonIt = stringToButton.find(buttonStr);
if (actionIt != stringToAction.end() && buttonIt != stringToButton.end()) {
config.bindings[actionIt->second] = buttonIt->second;
}
}
configs.push_back(config);
}
return true;
} catch (const std::exception& e) {
// Log del error si tienes sistema de logging
return false;
}
}
// Método auxiliar para verificar si un archivo existe
static bool fileExists(const std::string& filename) {
std::ifstream file(filename);
return file.good();
}
};
/*
// Ejemplo de uso
void ejemploUso() {
std::vector<GamepadConfig> configs;
// Crear algunas configuraciones de ejemplo
GamepadConfig config1("Xbox Controller");
config1.rebindAction(Input::Action::FIRE_LEFT, SDL_GAMEPAD_BUTTON_SOUTH);
GamepadConfig config2("PS4 Controller");
config2.rebindAction(Input::Action::START, SDL_GAMEPAD_BUTTON_OPTIONS);
configs.push_back(config1);
configs.push_back(config2);
// Escribir a archivo
if (GamepadConfigManager::writeToJson(configs, "gamepad_config.json")) {
std::cout << "Configuración guardada exitosamente" << std::endl;
}
// Leer desde archivo
std::vector<GamepadConfig> loadedConfigs;
if (GamepadConfigManager::readFromJson(loadedConfigs, "gamepad_config.json")) {
std::cout << "Configuración cargada exitosamente" << std::endl;
std::cout << "Número de configuraciones: " << loadedConfigs.size() << std::endl;
}
}
*/

View File

@@ -43,14 +43,14 @@ void Input::bindKey(Action input, SDL_Scancode code) {
// Asigna inputs a botones del mando // Asigna inputs a botones del mando
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button) { void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button) {
if (gamepad != nullptr) { if (gamepad != nullptr) {
gamepad->bindings.at(static_cast<int>(input)).button = button; gamepad->button_states.at(static_cast<int>(input)).button = button;
} }
} }
// Asigna inputs a botones del mando // Asigna inputs a botones del mando
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source) { void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source) {
if (gamepad != nullptr) { if (gamepad != nullptr) {
gamepad->bindings.at(static_cast<int>(input_target)).button = gamepad->bindings.at(static_cast<int>(input_source)).button; gamepad->button_states.at(static_cast<int>(input_target)).button = gamepad->button_states.at(static_cast<int>(input_source)).button;
} }
} }
@@ -74,9 +74,9 @@ auto Input::checkAction(Action input, bool repeat, Device device, std::shared_pt
if (!success_controller) { if (!success_controller) {
if (repeat) { // El usuario quiere saber si está pulsada (estado mantenido) if (repeat) { // El usuario quiere saber si está pulsada (estado mantenido)
success_controller = gamepad->bindings.at(INPUT_INDEX).is_held; success_controller = gamepad->button_states.at(INPUT_INDEX).is_held;
} else { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma) } else { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
success_controller = gamepad->bindings.at(INPUT_INDEX).just_pressed; success_controller = gamepad->button_states.at(INPUT_INDEX).just_pressed;
} }
} }
} }
@@ -108,7 +108,7 @@ auto Input::checkAnyInput(Device device, std::shared_ptr<Gamepad> gamepad) -> bo
// Iteramos sobre todas las acciones, no sobre el número de mandos. // Iteramos sobre todas las acciones, no sobre el número de mandos.
for (int i = 0; i < NUM_ACTIONS; ++i) { for (int i = 0; i < NUM_ACTIONS; ++i) {
// Leemos el estado pre-calculado para el mando y la acción específicos. // Leemos el estado pre-calculado para el mando y la acción específicos.
if (gamepad->bindings.at(i).just_pressed) { if (gamepad->button_states.at(i).just_pressed) {
return true; // Se encontró una acción recién pulsada en el mando. return true; // Se encontró una acción recién pulsada en el mando.
} }
} }
@@ -160,7 +160,7 @@ auto Input::getJoyIndex(SDL_JoystickID id) const -> int {
// Obtiene el SDL_GamepadButton asignado a un input // Obtiene el SDL_GamepadButton asignado a un input
auto Input::getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton { auto Input::getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton {
return gamepad->bindings.at(static_cast<int>(input)).button; return gamepad->button_states.at(static_cast<int>(input)).button;
} }
// Convierte un InputAction a std::string // Convierte un InputAction a std::string
@@ -217,7 +217,7 @@ auto Input::checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool
} }
// Referencia al binding correspondiente // Referencia al binding correspondiente
auto &binding = gamepad->bindings.at(static_cast<int>(input)); auto &binding = gamepad->button_states.at(static_cast<int>(input));
if (repeat) { if (repeat) {
// Si se permite repetir, simplemente devolvemos el estado actual // Si se permite repetir, simplemente devolvemos el estado actual
@@ -258,7 +258,7 @@ void Input::resetInputStates() {
} }
// Resetear todos los ControllerBindings.active a false // Resetear todos los ControllerBindings.active a false
for (auto &gamepad : gamepads_) { for (auto &gamepad : gamepads_) {
for (auto &binding : gamepad->bindings) { for (auto &binding : gamepad->button_states) {
binding.is_held = false; binding.is_held = false;
binding.just_pressed = false; binding.just_pressed = false;
} }
@@ -279,7 +279,7 @@ void Input::update() {
// --- MANDOS --- // --- MANDOS ---
for (auto gamepad : gamepads_) { for (auto gamepad : gamepads_) {
for (auto &binding : gamepad->bindings) { for (auto &binding : gamepad->button_states) {
bool button_is_down_now = static_cast<int>(SDL_GetGamepadButton(gamepad->pad, binding.button)) != 0; bool button_is_down_now = static_cast<int>(SDL_GetGamepadButton(gamepad->pad, binding.button)) != 0;
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo // El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo

View File

@@ -73,22 +73,22 @@ class Input {
}; };
// --- Estructuras --- // --- Estructuras ---
struct KeyBindings { struct KeyState {
Uint8 scancode; // Scancode asociado Uint8 scancode; // Scancode asociado
bool is_held; // Está pulsada ahora mismo bool is_held; // Está pulsada ahora mismo
bool just_pressed; // Se acaba de pulsar en este fotograma bool just_pressed; // Se acaba de pulsar en este fotograma
KeyBindings(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false) KeyState(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false)
: scancode(scancode), is_held(is_held), just_pressed(just_pressed) {} : scancode(scancode), is_held(is_held), just_pressed(just_pressed) {}
}; };
struct ControllerBindings { struct ButtonState {
SDL_GamepadButton button; // GameControllerButton asociado SDL_GamepadButton button; // GameControllerButton asociado
bool is_held; // Está pulsada ahora mismo bool is_held; // Está pulsada ahora mismo
bool just_pressed; // Se acaba de pulsar en este fotograma bool just_pressed; // Se acaba de pulsar en este fotograma
bool axis_active; // Estado del eje bool axis_active; // Estado del eje
ControllerBindings(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false) ButtonState(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false)
: button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {} : button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
}; };
@@ -96,7 +96,7 @@ class Input {
SDL_Gamepad *pad; SDL_Gamepad *pad;
SDL_JoystickID instance_id; SDL_JoystickID instance_id;
std::string name; std::string name;
std::vector<ControllerBindings> bindings; std::vector<ButtonState> button_states;
Gamepad(SDL_Gamepad *gamepad) Gamepad(SDL_Gamepad *gamepad)
: pad(gamepad), : pad(gamepad),
@@ -154,7 +154,7 @@ class Input {
// --- Variables internas --- // --- Variables internas ---
std::vector<std::shared_ptr<Gamepad>> gamepads_; // Mandos conectados std::vector<std::shared_ptr<Gamepad>> gamepads_; // Mandos conectados
std::vector<KeyBindings> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos std::vector<KeyState> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
std::vector<Action> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones std::vector<Action> button_inputs_; // Inputs asignados al jugador y a botones, excluyendo direcciones
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt

View File

@@ -25,12 +25,16 @@ VideoOptions video; // Opciones de vídeo
AudioOptions audio; // Opciones de audio AudioOptions audio; // Opciones de audio
std::vector<GamepadOptions> controllers; // Opciones de mando para cada jugador std::vector<GamepadOptions> controllers; // Opciones de mando para cada jugador
PendingChanges pending_changes; // Opciones que se aplican al cerrar PendingChanges pending_changes; // Opciones que se aplican al cerrar
std::vector<GamepadConfig> gamepad_configs; // Lista con las configuraciones registradas para cada mando
// Declaraciones // Declaraciones
auto set(const std::string& var, const std::string& value) -> bool; auto set(const std::string& var, const std::string& value) -> bool;
// Establece el fichero de configuración // Establece el fichero de configuración
void setFile(const std::string& file_path) { settings.config_file = file_path; }; void setConfigFile(const std::string& file_path) { settings.config_file = file_path; };
// Establece el fichero de configuración de mandos
void setControllersFile(const std::string& file_path) { settings.controllers_file = file_path; };
// Inicializa las opciones del programa // Inicializa las opciones del programa
void init() { void init() {

View File

@@ -5,9 +5,11 @@
#include <algorithm> // Para copy #include <algorithm> // Para copy
#include <array> // Para array #include <array> // Para array
#include <string> // Para allocator, string #include <string> // Para allocator, string
#include <unordered_map>
#include <vector> // Para vector #include <vector> // Para vector
#include "difficulty.h" // Para Code #include "difficulty.h" // Para Code
#include "gamepad_config_manager.h"
#include "input.h" // Para InputAction, InputDevice #include "input.h" // Para InputAction, InputDevice
#include "lang.h" // Para Code #include "lang.h" // Para Code
#include "manage_hiscore_table.h" // Para HiScoreEntry #include "manage_hiscore_table.h" // Para HiScoreEntry
@@ -77,6 +79,7 @@ struct SettingsOptions {
std::vector<HiScoreEntry> hi_score_table; // Tabla de mejores puntuaciones std::vector<HiScoreEntry> hi_score_table; // Tabla de mejores puntuaciones
std::vector<int> last_hi_score_entry; // Últimas posiciones de entrada en la tabla std::vector<int> last_hi_score_entry; // Últimas posiciones de entrada en la tabla
std::string config_file; // Ruta al fichero donde guardar la configuración y las opciones del juego std::string config_file; // Ruta al fichero donde guardar la configuración y las opciones del juego
std::string controllers_file; // Ruta al fichero con las configuraciones de los mandos
// Constructor por defecto con valores iniciales // Constructor por defecto con valores iniciales
SettingsOptions() SettingsOptions()
@@ -89,49 +92,14 @@ struct SettingsOptions {
} }
}; };
// --- Opciones de mando ---
struct GamepadOptions { struct GamepadOptions {
Input::Gamepad gamepad; // Mando Input::Gamepad gamepad = nullptr; // Mando
int player_id; // Jugador asociado al mando int player_id; // Jugador asociado al mando
Input::Device type{Input::Device::CONTROLLER}; // Indica si se usará teclado, mando o ambos Input::Device type = Input::Device::CONTROLLER; // Tipo de dispositivo
std::string name; // Nombre del dispositivo GamepadConfig config;
bool plugged{false}; // Indica si el mando está conectado
std::vector<Input::Action> inputs; // Listado de acciones asignadas
std::vector<SDL_GamepadButton> buttons; // Listado de botones asignados a cada acción
// Constructor por defecto GamepadOptions(int custom_player_id = INVALID_INDEX)
GamepadOptions() : player_id(custom_player_id) {}
: gamepad(nullptr),
player_id(INVALID_INDEX),
inputs{
Input::Action::FIRE_LEFT,
Input::Action::FIRE_CENTER,
Input::Action::FIRE_RIGHT,
Input::Action::START,
Input::Action::SERVICE},
buttons{
SDL_GAMEPAD_BUTTON_WEST,
SDL_GAMEPAD_BUTTON_NORTH,
SDL_GAMEPAD_BUTTON_EAST,
SDL_GAMEPAD_BUTTON_START,
SDL_GAMEPAD_BUTTON_BACK} {}
// Constructor con player_id
GamepadOptions(int custom_player_id)
: gamepad(nullptr),
player_id(custom_player_id),
inputs{
Input::Action::FIRE_LEFT,
Input::Action::FIRE_CENTER,
Input::Action::FIRE_RIGHT,
Input::Action::START,
Input::Action::SERVICE},
buttons{
SDL_GAMEPAD_BUTTON_WEST,
SDL_GAMEPAD_BUTTON_NORTH,
SDL_GAMEPAD_BUTTON_EAST,
SDL_GAMEPAD_BUTTON_START,
SDL_GAMEPAD_BUTTON_BACK} {}
}; };
// --- Opciones pendientes de aplicar --- // --- Opciones pendientes de aplicar ---
@@ -151,10 +119,12 @@ extern VideoOptions video; // Opciones de vídeo
extern AudioOptions audio; // Opciones de audio extern AudioOptions audio; // Opciones de audio
extern std::vector<GamepadOptions> controllers; // Opciones de mando para cada jugador extern std::vector<GamepadOptions> controllers; // Opciones de mando para cada jugador
extern PendingChanges pending_changes; // Opciones que se aplican al cerrar extern PendingChanges pending_changes; // Opciones que se aplican al cerrar
extern std::vector<GamepadConfig> gamepad_configs; // Lista con las configuraciones registradas para cada mando
// --- Funciones de configuración --- // --- Funciones de configuración ---
void init(); // Inicializa las opciones del programa void init(); // Inicializa las opciones del programa
void setFile(const std::string &file_path); // Establece el fichero de configuración void setConfigFile(const std::string &file_path); // Establece el fichero de configuración
void setControllersFile(const std::string &file_path); // Establece el fichero de configuración de mandos
auto loadFromFile() -> bool; // Carga el fichero de configuración auto loadFromFile() -> bool; // Carga el fichero de configuración
auto saveToFile() -> bool; // Guarda el fichero de configuración auto saveToFile() -> bool; // Guarda el fichero de configuración
void setKeyboardToPlayer(int player_id); // Asigna el teclado al jugador void setKeyboardToPlayer(int player_id); // Asigna el teclado al jugador
@@ -163,4 +133,5 @@ void swapControllers(); // Intercambia los jugadores asigna
auto getPlayerWhoUsesKeyboard() -> int; // Averigua quién está usando el teclado auto getPlayerWhoUsesKeyboard() -> int; // Averigua quién está usando el teclado
void applyPendingChanges(); // Aplica los cambios pendientes copiando los valores a sus variables void applyPendingChanges(); // Aplica los cambios pendientes copiando los valores a sus variables
void checkPendingChanges(); // Verifica si hay cambios pendientes void checkPendingChanges(); // Verifica si hay cambios pendientes
} // namespace Options } // namespace Options