Reestructurant la classe Options

This commit is contained in:
2025-02-23 18:12:02 +01:00
parent 3ba4293e8a
commit 2ee0c70319
48 changed files with 898 additions and 984 deletions

View File

@@ -5,72 +5,33 @@
#include <iostream> // Para basic_ostream, operator<<, cout
// Variables
options_t options;
Options options;
bool setOptions(std::string var, std::string value);
bool setOptions(const std::string &var, const std::string &value);
// Crea e inicializa las opciones del programa
void initOptions()
{
// Version del archivo de configuración
options.configVersion = "v1.06.1";
options = Options();
// Opciones de control
options.keys = ctrl_cursor;
// Opciones de video
options.gameWidth = GAMECANVAS_WIDTH;
options.gameHeight = GAMECANVAS_HEIGHT;
options.videoMode = 0;
options.windowSize = 3;
options.filter = FILTER_NEAREST;
options.shaders = false;
options.vSync = true;
options.integerScale = true;
options.keepAspect = true;
options.borderEnabled = true;
options.borderWidth = 32;
options.borderHeight = 24;
options.palette = p_zxspectrum;
#ifdef GAME_CONSOLE
options.windowSize = 2;
#endif
// Estos valores no se guardan en el fichero de configuración
options.console = false;
#ifdef DEBUG
options.section = SectionState(Section::TITLE, Subsection::NONE);
options.console = true;
#endif
options.cheat.infiniteLives = false;
options.cheat.invincible = false;
options.cheat.jailEnabled = false;
options.cheat.altSkin = false;
options.stats.rooms = 0;
options.stats.items = 0;
// Opciones de las notificaciones
options.notifications.posV = pos_top;
options.notifications.posH = pos_left;
options.notifications.sound = true;
options.notifications.color = {48, 48, 48};
#ifdef DEBUG
options.section.name = SECTION_TITLE;
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
#else
options.section.name = SECTION_LOGO;
options.section.subsection = SUBSECTION_LOGO_TO_INTRO;
options.section = SectionState(Section::LOGO, Subsection::LOGO_TO_INTRO);
options.console = false;
#endif
}
// Carga las opciones desde un fichero
bool loadOptionsFromFile(const std::string &file_path)
{
// Indicador de éxito en la carga
bool success = true;
// Versión actual del fichero
const std::string configVersion = options.configVersion;
options.configVersion = "";
const std::string configVersion = options.version;
options.version = "";
// Variables para manejar el fichero
std::string line;
@@ -114,137 +75,70 @@ bool loadOptionsFromFile(const std::string &file_path)
// El fichero no existe
else
{ // Crea el fichero con los valores por defecto
{
// Crea el fichero con los valores por defecto
saveOptionsToFile(file_path);
}
// Si la versión de fichero no coincide, crea un fichero nuevo con los valores por defecto
if (configVersion != options.configVersion)
if (configVersion != options.version)
{
initOptions();
saveOptionsToFile(file_path);
}
// Normaliza los valores
const bool a = options.videoMode == 0;
const bool b = options.videoMode == SDL_WINDOW_FULLSCREEN;
const bool c = options.videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
if (!(a || b || c))
if (options.video.mode != 0 &&
options.video.mode != SDL_WINDOW_FULLSCREEN &&
options.video.mode != SDL_WINDOW_FULLSCREEN_DESKTOP)
{
options.videoMode = 0;
}
if (options.windowSize < 1 || options.windowSize > 4)
{
options.windowSize = 3;
options.video.mode = 0;
}
return success;
}
// Guarda las opciones en un fichero
bool saveOptionsToFile(const std::string &file_path)
{
bool success = true;
// Crea y abre el fichero de texto
std::ofstream file(file_path);
bool success = file.is_open(); // Verifica si el archivo se abrió correctamente
if (file.good())
if (!success) // Si no se pudo abrir el archivo, muestra un mensaje de error y devuelve false
{
if (options.console)
{
std::cout << file_path << " open for writing" << std::endl;
std::cerr << "Error: Unable to open file " << file_path << " for writing." << std::endl;
}
return false;
}
else
if (options.console)
{
if (options.console)
{
std::cout << file_path << " can't be opened" << std::endl;
}
std::cout << file_path << " open for writing" << std::endl;
}
// Escribe en el fichero
file << "## VERSION\n";
file << "configVersion=" + options.configVersion + "\n";
file << "version=" << options.version << "\n";
file << "\n## CONTROL OPTIONS\n";
file << "\n## CONTROL\n";
file << "## keys = CURSOR | OPQA | WASD\n";
if (options.keys == ctrl_cursor)
{
file << "keys=CURSOR\n";
}
else if (options.keys == ctrl_opqa)
{
file << "keys=OPQA\n";
}
else if (options.keys == ctrl_wasd)
{
file << "keys=WASD\n";
}
file << "keys=" << static_cast<int>(options.keys) << "\n";
file << "\n## VISUAL OPTIONS\n";
if (options.videoMode == 0)
{
file << "videoMode=0\n";
}
else if (options.videoMode == SDL_WINDOW_FULLSCREEN)
{
file << "videoMode=SDL_WINDOW_FULLSCREEN\n";
}
else if (options.videoMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
file << "videoMode=SDL_WINDOW_FULLSCREEN_DESKTOP\n";
}
file << "windowSize=" + std::to_string(options.windowSize) + "\n";
if (options.filter == FILTER_NEAREST)
{
file << "filter=FILTER_NEAREST\n";
}
else
{
file << "filter=FILTER_LINEAR\n";
}
file << "shaders=" + boolToString(options.shaders) + "\n";
file << "vSync=" + boolToString(options.vSync) + "\n";
file << "integerScale=" + boolToString(options.integerScale) + "\n";
file << "keepAspect=" + boolToString(options.keepAspect) + "\n";
file << "borderEnabled=" + boolToString(options.borderEnabled) + "\n";
file << "borderWidth=" + std::to_string(options.borderWidth) + "\n";
file << "borderHeight=" + std::to_string(options.borderHeight) + "\n";
file << "palette=" + std::to_string(options.palette) + "\n";
file << "\n## NOTIFICATION OPTIONS\n";
file << "## notifications.posV = pos_top | pos_bottom\n";
if (options.notifications.posV == pos_top)
{
file << "notifications.posV=pos_top\n";
}
else
{
file << "notifications.posV=pos_bottom\n";
}
file << "## notifications.posH = pos_left | pos_middle | pos_right\n";
if (options.notifications.posH == pos_left)
{
file << "notifications.posH=pos_left\n";
}
else if (options.notifications.posH == pos_middle)
{
file << "notifications.posH=pos_middle\n";
}
else
{
file << "notifications.posH=pos_right\n";
}
file << "notifications.sound=" + boolToString(options.notifications.sound) + "\n";
file << "\n## VIDEO\n";
file << "video.mode=" << options.video.mode << "\n";
file << "window.zoom=" << options.window.zoom << "\n";
file << "video.filter=" << static_cast<int>(options.video.filter) << "\n";
file << "video.shaders=" << boolToString(options.video.shaders) << "\n";
file << "video.vertical_sync=" << boolToString(options.video.vertical_sync) << "\n";
file << "video.integer_scale=" << boolToString(options.video.integer_scale) << "\n";
file << "video.keep_aspect=" << boolToString(options.video.keep_aspect) << "\n";
file << "video.border.enabled=" << boolToString(options.video.border.enabled) << "\n";
file << "video.border.width=" << options.video.border.width << "\n";
file << "video.border.height=" << options.video.border.height << "\n";
file << "video.palette=" << static_cast<int>(options.video.palette) << "\n";
// Cierra el fichero
file.close();
@@ -252,160 +146,27 @@ bool saveOptionsToFile(const std::string &file_path)
return success;
}
bool setOptions(std::string var, std::string value)
{
// Indicador de éxito en la asignación
bool success = true;
if (var == "configVersion")
{
options.configVersion = value;
// Establece las opciones
bool setOptions(const std::string &var, const std::string &value) {
static const std::unordered_map<std::string, std::function<void(std::string)>> optionHandlers = {
{"version", [](std::string v) { options.version = v; }},
{"keys", [](std::string v) { options.keys = static_cast<ControlScheme>(safeStoi(v, static_cast<int>(ControlScheme::CURSOR))); }},
{"video.mode", [](std::string v) { options.video.mode = safeStoi(v, 0); }},
{"window.zoom", [](std::string v) { options.window.zoom = safeStoi(v, 1); }},
{"video.shaders", [](std::string v) { options.video.shaders = stringToBool(v); }},
{"video.vertical_sync", [](std::string v) { options.video.vertical_sync = stringToBool(v); }},
{"video.integer_scale", [](std::string v) { options.video.integer_scale = stringToBool(v); }},
{"video.keep_aspect", [](std::string v) { options.video.keep_aspect = stringToBool(v); }},
{"video.border.enabled", [](std::string v) { options.video.border.enabled = stringToBool(v); }},
{"video.border.width", [](std::string v) { options.video.border.width = safeStoi(v, 32); }},
{"video.border.height", [](std::string v) { options.video.border.height = safeStoi(v, 24); }},
{"video.palette", [](std::string v) { options.video.palette = static_cast<Palette>(safeStoi(v, static_cast<int>(DEFAULT_PALETTE))); }}
};
auto it = optionHandlers.find(var);
if (it != optionHandlers.end()) {
it->second(value);
return true;
}
else if (var == "keys")
{
if (value == "OPQA")
{
options.keys = ctrl_opqa;
}
else if (value == "WASD")
{
options.keys = ctrl_wasd;
}
else
{
options.keys = ctrl_cursor;
}
}
else if (var == "videoMode")
{
if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
{
options.videoMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else if (value == "SDL_WINDOW_FULLSCREEN")
{
options.videoMode = SDL_WINDOW_FULLSCREEN;
}
else
{
options.videoMode = 0;
}
}
else if (var == "windowSize")
{
options.windowSize = std::stoi(value);
if ((options.windowSize < 1) || (options.windowSize > 4))
{
options.windowSize = 3;
}
}
else if (var == "filter")
{
if (value == "FILTER_LINEAR")
{
options.filter = FILTER_LINEAR;
}
else
{
options.filter = FILTER_NEAREST;
}
}
else if (var == "shaders")
{
options.shaders = stringToBool(value);
}
else if (var == "vSync")
{
options.vSync = stringToBool(value);
}
else if (var == "integerScale")
{
options.integerScale = stringToBool(value);
}
else if (var == "keepAspect")
{
options.keepAspect = stringToBool(value);
}
else if (var == "borderEnabled")
{
options.borderEnabled = stringToBool(value);
}
else if (var == "borderWidth")
{
options.borderWidth = std::stoi(value);
}
else if (var == "borderHeight")
{
options.borderHeight = std::stoi(value);
}
else if (var == "palette")
{
const int pal = std::stoi(value);
if (pal == 0)
{
options.palette = p_zxspectrum;
}
else if (pal == 1)
{
options.palette = p_zxarne;
}
}
else if (var == "notifications.posH")
{
if (value == "pos_left")
{
options.notifications.posH = pos_left;
}
else if (value == "pos_middle")
{
options.notifications.posH = pos_middle;
}
else
{
options.notifications.posH = pos_right;
}
}
else if (var == "notifications.posV")
{
if (value == "pos_top")
{
options.notifications.posV = pos_top;
}
else
{
options.notifications.posV = pos_bottom;
}
}
else if (var == "notifications.sound")
{
options.notifications.sound = stringToBool(value);
}
else if (var == "" || var.substr(0, 1) == "#")
{
}
else
{
success = false;
}
return success;
return false;
}