Options: load i save ja no necesiten el fitxer com a parametre

This commit is contained in:
2025-06-14 17:57:47 +02:00
parent eee398802f
commit c8999bff68
3 changed files with 29 additions and 25 deletions

View File

@@ -88,7 +88,7 @@ void Director::init()
// Configuración inicial de recursos
Asset::init(executable_path_); // Inicializa el sistema de gestión de archivos
setFileList(); // Crea el índice de archivos
Options::loadFromFile(Asset::get()->get("config.txt")); // Carga el archivo de configuración
Options::loadFromFile(); // Carga el archivo de configuración
loadParams(); // Carga los parámetros del programa
loadScoreFile(); // Carga el archivo de puntuaciones
@@ -108,7 +108,7 @@ void Director::init()
void Director::close()
{
// Guarda las opciones actuales en el archivo de configuración
Options::saveToFile(Asset::get()->get("config.txt"));
Options::saveToFile();
// Libera los singletons y recursos en orden inverso al de inicialización
Notifier::destroy(); // Libera el sistema de notificaciones
@@ -617,8 +617,8 @@ void Director::runDemoGame()
// Reinicia objetos y vuelve a la sección inicial
void Director::reset()
{
Options::saveToFile(Asset::get()->get("config.txt"));
Options::loadFromFile(Asset::get()->get("config.txt"));
Options::saveToFile();
Options::loadFromFile();
Lang::setLanguage(Options::settings.language);
Audio::get()->stopMusic();
Audio::get()->stopAllSounds();

View File

@@ -7,6 +7,7 @@
#include "input.h" // Para InputDeviceToUse
#include "lang.h" // Para Code
#include "utils.h" // Para boolToString, stringToBool, getFileName
#include "asset.h"
namespace Options
{
@@ -30,6 +31,7 @@ namespace Options
// Inicializa las opciones del programa
void init()
{
// Opciones de ventana
window.caption = "Coffee Crisis Arcade Edition";
window.size = 2;
@@ -48,12 +50,13 @@ namespace Options
audio.sound.enabled = true;
audio.sound.volume = 50;
// Opciones de juego
// Opciones de configuracion
settings.difficulty = DifficultyCode::NORMAL;
settings.language = Lang::Code::VALENCIAN;
settings.autofire = true;
settings.shutdown_enabled = false;
settings.clearLastHiScoreEntries();
settings.config_file = Asset::get()->get("config.txt");
// Opciones de control
controllers.clear();
@@ -69,7 +72,7 @@ namespace Options
}
// Carga el fichero de configuración
bool loadFromFile(std::string file_path)
bool loadFromFile()
{
// Inicializa las opciones del programa
init();
@@ -78,13 +81,13 @@ namespace Options
bool success = true;
// Variables para manejar el fichero
std::ifstream file(file_path);
std::ifstream file(settings.config_file);
// Si el fichero se puede abrir
if (file.good())
{
// Procesa el fichero línea a línea
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(file_path).c_str());
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(settings.config_file).c_str());
std::string line;
while (std::getline(file, line))
{
@@ -107,7 +110,7 @@ namespace Options
else
{
// Crea el fichero con los valores por defecto
saveToFile(file_path);
saveToFile();
}
// Normaliza los valores
@@ -122,17 +125,17 @@ namespace Options
}
// Guarda el fichero de configuración
bool saveToFile(std::string file_path)
bool saveToFile()
{
std::ofstream file(file_path);
std::ofstream file(settings.config_file);
if (!file.good())
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: %s can't be opened", getFileName(file_path).c_str());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: %s can't be opened", getFileName(settings.config_file).c_str());
return false;
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file: %s", getFileName(file_path).c_str());
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file: %s", getFileName(settings.config_file).c_str());
applyPendingChanges();

View File

@@ -83,6 +83,7 @@ namespace Options
bool shutdown_enabled; // Especifica si se puede apagar el sistema
std::vector<HiScoreEntry> hi_score_table; // Tabla de mejores puntuaciones
std::vector<int> last_hi_score_entry = {-1, -1}; // Últimas posiciones de entrada en la tabla
std::string config_file; // Ruta al fichero donde guardar la configuracion y las opciones del juego
// Reinicia las últimas entradas de puntuación
void clearLastHiScoreEntries()
@@ -128,8 +129,8 @@ namespace Options
// --- Funciones de configuración ---
void init(); // Inicializa las opciones del programa
bool loadFromFile(std::string file_path); // Carga el fichero de configuración
bool saveToFile(std::string file_path); // Guarda el fichero de configuración
bool loadFromFile(); // Carga el fichero de configuración
bool saveToFile(); // Guarda el fichero de configuración
void setKeyboardToPlayer(int player_id); // Asigna el teclado al jugador
void swapKeyboard(); // Intercambia el teclado de jugador
void swapControllers(); // Intercambia los jugadores asignados a los dos primeros mandos