Ya guarda y lee la configuracion

This commit is contained in:
2022-09-12 19:06:15 +02:00
parent c5aa28d738
commit 6b7769ca3c
8 changed files with 116 additions and 114 deletions

View File

@@ -35,11 +35,16 @@ Director::Director(std::string path)
initInput();
screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
screen->setBorderColor(borderColor);
screen->setVideoMode(options->fullScreenMode);
debug = new Debug(renderer, screen, asset);
}
Director::~Director()
{
// Guarda las opciones de configuración
saveConfig();
// Libera la memoria
delete options;
delete asset;
delete input;
@@ -60,8 +65,6 @@ bool Director::loadConfig()
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;
@@ -94,77 +97,21 @@ bool Director::loadConfig()
}
}
}
}
/*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);
}*/
printf("Closing file config.txt\n\n");
file.close();
}
// El fichero no existe
else
{ // Crea el fichero con los valores por defecto
saveConfig();
}
// Aplica opciones
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
return success;
}
@@ -173,32 +120,46 @@ bool Director::loadConfig()
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)
// Crea y abre el fichero de texto
std::ofstream file(asset->get("config.txt"));
// Escribe en el fichero
if (options->fullScreenMode == 0)
{
// 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);
file << "fullScreenMode=0\n";
}
printf("Writing file %s\n", filename.c_str());
else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN)
{
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN\n";
}
// Cierra el fichero
SDL_RWclose(file);
else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN_DESKTOP\n";
}
file << "windowSize=" + std::to_string(options->windowSize) + "\n";
if (options->filter == FILTER_NEAREST)
{
file << "filter=FILTER_NEAREST\n";
}
else
{
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
file << "filter=FILTER_LINEAL\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 << "borderSize=" + std::to_string(options->borderSize) + "\n";
// Cierra el fichero
file.close();
return success;
}
@@ -210,30 +171,38 @@ bool Director::setOptions(options_t *options, std::string var, std::string value
if (var == "fullScreenMode")
{
if (value == "0")
{
options->fullScreenMode = 0;
}
else if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
{
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else if (value == "SDL_WINDOW_FULLSCREEN")
{
options->fullScreenMode = SDL_WINDOW_FULLSCREEN;
}
else
{
options->fullScreenMode = 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_NEAREST")
if (value == "FILTER_LINEAL")
{
options->filter = FILTER_NEAREST;
options->filter = FILTER_LINEAL;
}
else
{
options->filter = FILTER_LINEAL;
options->filter = FILTER_NEAREST;
}
}
@@ -260,6 +229,10 @@ bool Director::setOptions(options_t *options, std::string var, std::string value
else if (var == "borderSize")
{
options->borderSize = std::stof(value);
if (options->borderSize < 0.0f || options->borderSize > 0.5f)
{
options->borderSize = 0.1f;
}
}
else if (var == "")