Trabajando en el fichero de configuración

This commit is contained in:
2022-09-12 14:07:59 +02:00
parent 561f957bca
commit c5aa28d738
8 changed files with 275 additions and 25 deletions

View File

@@ -21,16 +21,8 @@ Director::Director(std::string path)
section.subsection = 0;
}
// Crea el puntero a la estructura y carga el fichero de configuración
options = new options_t;
options->fullScreenMode = 0;
options->windowSize = 3;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
options->integerScale = true;
options->keepAspect = true;
// Inicializa variables desde el fichero de configuración
loadConfig();
// Inicializa SDL
initSDL();
@@ -59,6 +51,229 @@ Director::~Director()
SDL_Quit();
}
// Carga el fichero de configuración
bool Director::loadConfig()
{
// Crea el puntero a la estructura de opciones e inicializa valores
options = new options_t;
options->fullScreenMode = 0;
options->windowSize = 3;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
options->integerScale = true;
options->keepAspect = true;
options->borderEnabled = false;
options->borderSize = 0.1f;
// Indicador de éxito en la carga
bool success = true;
// Variables para manejar el fichero
std::string line;
std::ifstream file(asset->get("config.txt"));
// Si el fichero se puede abrir
if (file.good())
{
// Procesa el fichero linea a linea
printf("Reading file config.txt\n");
while (std::getline(file, line))
{
// Comprueba que la linea no sea un comentario
if (line.substr(0, 1) != "#")
{
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
{
printf("Warning: file %s\n, unknown parameter \"%s\"\n", "config.txt", line.substr(0, pos).c_str());
success = false;
}
}
}
}
/*const std::string p = asset->get("config.txt");
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r");
// El fichero no existe
if (file == NULL)
{
printf("Warning: Unable to open %s file\n", filename.c_str());
// Crea el fichero para escritura
file = SDL_RWFromFile(p.c_str(), "w");
if (file != NULL)
{
printf("New file (%s) created!\n", filename.c_str());
// Escribe los datos
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
SDL_RWwrite(file, &options->borderEnabled, sizeof(options->borderEnabled), 1);
SDL_RWwrite(file, &options->borderSize, sizeof(options->borderSize), 1);
// Cierra el fichero
SDL_RWclose(file);
}
else
{
printf("Error: Unable to create file %s\n", filename.c_str());
success = false;
}
}
// El fichero existe
else
{
// Carga los datos
printf("Reading file %s\n", filename.c_str());
SDL_RWread(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
SDL_RWread(file, &options->windowSize, sizeof(options->windowSize), 1);
SDL_RWread(file, &options->filter, sizeof(options->filter), 1);
SDL_RWread(file, &options->vSync, sizeof(options->vSync), 1);
SDL_RWread(file, &options->screenWidth, sizeof(options->screenWidth), 1);
SDL_RWread(file, &options->screenHeight, sizeof(options->screenHeight), 1);
SDL_RWread(file, &options->integerScale, sizeof(options->integerScale), 1);
SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1);
SDL_RWread(file, &options->borderEnabled, sizeof(options->borderEnabled), 1);
SDL_RWread(file, &options->borderSize, sizeof(options->borderSize), 1);
// Normaliza los valores
const bool a = options->fullScreenMode == 0;
const bool b = options->fullScreenMode == SDL_WINDOW_FULLSCREEN;
const bool c = options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP;
if (!(a || b || c))
{
options->fullScreenMode = 0;
}
if ((options->windowSize < 1) || (options->windowSize > 4))
{
options->windowSize = 3;
}
// Cierra el fichero
SDL_RWclose(file);
}*/
return success;
}
// Guarda el fichero de configuración
bool Director::saveConfig()
{
bool success = true;
const std::string p = asset->get("config.txt");
const std::string filename = p.substr(p.find_last_of("\\/") + 1);
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
if (file != NULL)
{
// Guarda los datos
SDL_RWwrite(file, &options->fullScreenMode, sizeof(options->fullScreenMode), 1);
SDL_RWwrite(file, &options->windowSize, sizeof(options->windowSize), 1);
SDL_RWwrite(file, &options->filter, sizeof(options->filter), 1);
SDL_RWwrite(file, &options->vSync, sizeof(options->vSync), 1);
SDL_RWwrite(file, &options->screenWidth, sizeof(options->screenWidth), 1);
SDL_RWwrite(file, &options->screenHeight, sizeof(options->screenHeight), 1);
SDL_RWwrite(file, &options->integerScale, sizeof(options->integerScale), 1);
SDL_RWwrite(file, &options->keepAspect, sizeof(options->keepAspect), 1);
SDL_RWwrite(file, &options->borderEnabled, sizeof(options->borderEnabled), 1);
SDL_RWwrite(file, &options->borderSize, sizeof(options->borderSize), 1);
printf("Writing file %s\n", filename.c_str());
// Cierra el fichero
SDL_RWclose(file);
}
else
{
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
}
return success;
}
// Asigna variables a partir de dos cadenas
bool Director::setOptions(options_t *options, std::string var, std::string value)
{
// Indicador de éxito en la asignación
bool success = true;
if (var == "fullScreenMode")
{
if (value == "0")
{
options->fullScreenMode = 0;
}
else if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
{
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
}
else if (var == "windowSize")
{
options->windowSize = std::stoi(value);
}
else if (var == "filter")
{
if (value == "FILTER_NEAREST")
{
options->filter = FILTER_NEAREST;
}
else
{
options->filter = FILTER_LINEAL;
}
}
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 == "borderSize")
{
options->borderSize = std::stof(value);
}
else if (var == "")
{
}
else
{
success = false;
}
return success;
}
// Inicia las variables necesarias para arrancar el programa
void Director::initInput()
{
@@ -160,6 +375,7 @@ bool Director::setFileList()
asset->add("/media/font/debug.txt", font);
asset->add("/data/gamecontrollerdb.txt", data);
asset->add("/data/config.txt", data, false);
asset->add("/data/room/01.room", room);
asset->add("/data/room/02.room", room);