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

@@ -1,9 +1,8 @@
### NO TOCAR SI NO SE SABE LO QUE SE ESTÁ HACIENDO
fullScreenMode=0
windowSize=1
windowSize=3
filter=FILTER_NEAREST
vSync=true
integerScale=true
keepAspect=true
borderEnabled=true
borderSize=0.1f
borderEnabled=false
borderSize=0.100000

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");
// Cierra el fichero
printf("Closing file config.txt\n\n");
file.close();
}
// 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;
{ // Crea el fichero con los valores por defecto
saveConfig();
}
if ((options->windowSize < 1) || (options->windowSize > 4))
{
options->windowSize = 3;
}
// Cierra el fichero
SDL_RWclose(file);
}*/
// 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 == "")

View File

@@ -42,7 +42,6 @@ private:
section_t section; // Seccion actual dentro del juego
std::string currentRoom; // Fichero de la habitación actual
player_t spawnPoint; // Lugar de la habitación donde aparece el jugador
Uint32 clock; // Cuenta el tiempo que dura la partida
JA_Sound deathSound; // Sonido a reproducir cuando muere el jugador
board_t board; // Estructura con los datos del marcador
Test *test;

View File

@@ -114,7 +114,8 @@ bool Room::load(std::string file_path)
std::getline(file, line);
if (line.find(".tmx") != std::string::npos)
{
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
std::string filename2 = line;
std::ifstream file2(asset->get(filename2)); // Abre el fichero tmx
if (file2.good())
{
bool data_read = false;
@@ -145,6 +146,10 @@ bool Room::load(std::string file_path)
} while (line != "</data>");
}
}
// Cierra el fichero
printf("Closing file %s\n\n", filename2.c_str());
file2.close();
}
}
} while (line != "[/tilemap]");
@@ -275,7 +280,6 @@ bool Room::setVars(std::string var, std::string value)
}
return success;
}
// Asigna variables a una estructura enemy_t

View File

@@ -15,8 +15,8 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, i
// Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00};
borderEnabled = false;
borderSize = 0.1f;
borderEnabled = options->borderEnabled;
borderSize = options->borderSize;
// Crea la textura donde se dibujan los graficos del juego
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
@@ -160,12 +160,14 @@ void Screen::switchVideoMode()
{
if (options->fullScreenMode == 0)
{
setVideoMode(SDL_WINDOW_FULLSCREEN_DESKTOP);
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else
{
setVideoMode(0);
options->fullScreenMode = 0;
}
setVideoMode(options->fullScreenMode);
}
// Cambia el tamaño de la ventana

View File

@@ -142,6 +142,8 @@ int Text::lenght(std::string text, int kerning)
void Text::initOffsetFromFile()
{
std::ifstream rfile(file);
printf("Reading file %s\n", file.c_str());
if (rfile.is_open() && rfile.good())
{
std::string buffer;
@@ -168,6 +170,16 @@ void Text::initOffsetFromFile()
buffer.clear();
line_read++;
};
// Cierra el fichero
printf("Closing file %s\n\n", file.c_str());
rfile.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", file.c_str());
}
}

View File

@@ -531,7 +531,7 @@ color_t stringToColor(std::string str)
return {0x00, 0x00, 0x00};
}
// Devuelve un color_t a partir de un string
// Convierte una cadena en un valor booleano
bool stringToBool(std::string str)
{
if (str == "true")
@@ -543,3 +543,16 @@ bool stringToBool(std::string str)
return false;
}
}
// Convierte un valor booleano en una cadena
std::string boolToString(bool value)
{
if (value)
{
return "true";
}
else
{
return "false";
}
}

View File

@@ -118,9 +118,6 @@ SDL_Point checkCollision(line_t &l1, line_t &l2);
// Detector de colisiones entre dos lineas
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2);
// Detector de colisiones entre una linea diagonal y una vertical
// bool checkCollision(d_line_t &l1, v_line_t &l2);
// Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(SDL_Point &p, d_line_t &l);
@@ -130,7 +127,10 @@ void normalizeLine(d_line_t &l);
// Devuelve un color_t a partir de un string
color_t stringToColor(std::string str);
// Devuelve un color_t a partir de un string
// Convierte una cadena en un valor booleano
bool stringToBool(std::string str);
// Convierte un valor booleano en una cadena
std::string boolToString(bool value);
#endif