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 fullScreenMode=0
windowSize=1 windowSize=3
filter=FILTER_NEAREST filter=FILTER_NEAREST
vSync=true vSync=true
integerScale=true integerScale=true
keepAspect=true keepAspect=true
borderEnabled=true borderEnabled=false
borderSize=0.1f borderSize=0.100000

View File

@@ -35,11 +35,16 @@ Director::Director(std::string path)
initInput(); initInput();
screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
screen->setBorderColor(borderColor); screen->setBorderColor(borderColor);
screen->setVideoMode(options->fullScreenMode);
debug = new Debug(renderer, screen, asset); debug = new Debug(renderer, screen, asset);
} }
Director::~Director() Director::~Director()
{ {
// Guarda las opciones de configuración
saveConfig();
// Libera la memoria
delete options; delete options;
delete asset; delete asset;
delete input; delete input;
@@ -60,8 +65,6 @@ bool Director::loadConfig()
options->windowSize = 3; options->windowSize = 3;
options->filter = FILTER_NEAREST; options->filter = FILTER_NEAREST;
options->vSync = true; options->vSync = true;
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
options->integerScale = true; options->integerScale = true;
options->keepAspect = true; options->keepAspect = true;
options->borderEnabled = false; 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 // 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; return success;
} }
@@ -173,32 +120,46 @@ bool Director::loadConfig()
bool Director::saveConfig() bool Director::saveConfig()
{ {
bool success = true; bool success = true;
const std::string p = asset->get("config.txt");
const std::string filename = p.substr(p.find_last_of("\\/") + 1); // Crea y abre el fichero de texto
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b"); std::ofstream file(asset->get("config.txt"));
if (file != NULL)
// Escribe en el fichero
if (options->fullScreenMode == 0)
{ {
// Guarda los datos file << "fullScreenMode=0\n";
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()); else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN)
{
file << "fullScreenMode=SDL_WINDOW_FULLSCREEN\n";
}
// Cierra el fichero else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
SDL_RWclose(file); {
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 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; return success;
} }
@@ -210,30 +171,38 @@ bool Director::setOptions(options_t *options, std::string var, std::string value
if (var == "fullScreenMode") if (var == "fullScreenMode")
{ {
if (value == "0") if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
{
options->fullScreenMode = 0;
}
else if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
{ {
options->fullScreenMode = 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") else if (var == "windowSize")
{ {
options->windowSize = std::stoi(value); options->windowSize = std::stoi(value);
if ((options->windowSize < 1) || (options->windowSize > 4))
{
options->windowSize = 3;
}
} }
else if (var == "filter") else if (var == "filter")
{ {
if (value == "FILTER_NEAREST") if (value == "FILTER_LINEAL")
{ {
options->filter = FILTER_NEAREST; options->filter = FILTER_LINEAL;
} }
else 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") else if (var == "borderSize")
{ {
options->borderSize = std::stof(value); options->borderSize = std::stof(value);
if (options->borderSize < 0.0f || options->borderSize > 0.5f)
{
options->borderSize = 0.1f;
}
} }
else if (var == "") else if (var == "")

View File

@@ -42,7 +42,6 @@ private:
section_t section; // Seccion actual dentro del juego section_t section; // Seccion actual dentro del juego
std::string currentRoom; // Fichero de la habitación actual std::string currentRoom; // Fichero de la habitación actual
player_t spawnPoint; // Lugar de la habitación donde aparece el jugador 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 JA_Sound deathSound; // Sonido a reproducir cuando muere el jugador
board_t board; // Estructura con los datos del marcador board_t board; // Estructura con los datos del marcador
Test *test; Test *test;

View File

@@ -114,7 +114,8 @@ bool Room::load(std::string file_path)
std::getline(file, line); std::getline(file, line);
if (line.find(".tmx") != std::string::npos) 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()) if (file2.good())
{ {
bool data_read = false; bool data_read = false;
@@ -145,6 +146,10 @@ bool Room::load(std::string file_path)
} while (line != "</data>"); } while (line != "</data>");
} }
} }
// Cierra el fichero
printf("Closing file %s\n\n", filename2.c_str());
file2.close();
} }
} }
} while (line != "[/tilemap]"); } while (line != "[/tilemap]");
@@ -275,7 +280,6 @@ bool Room::setVars(std::string var, std::string value)
} }
return success; return success;
} }
// Asigna variables a una estructura enemy_t // 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 // Define el color del borde para el modo de pantalla completa
borderColor = {0x00, 0x00, 0x00}; borderColor = {0x00, 0x00, 0x00};
borderEnabled = false; borderEnabled = options->borderEnabled;
borderSize = 0.1f; borderSize = options->borderSize;
// Crea la textura donde se dibujan los graficos del juego // Crea la textura donde se dibujan los graficos del juego
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
@@ -160,12 +160,14 @@ void Screen::switchVideoMode()
{ {
if (options->fullScreenMode == 0) if (options->fullScreenMode == 0)
{ {
setVideoMode(SDL_WINDOW_FULLSCREEN_DESKTOP); options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
} }
else else
{ {
setVideoMode(0); options->fullScreenMode = 0;
} }
setVideoMode(options->fullScreenMode);
} }
// Cambia el tamaño de la ventana // Cambia el tamaño de la ventana

View File

@@ -142,6 +142,8 @@ int Text::lenght(std::string text, int kerning)
void Text::initOffsetFromFile() void Text::initOffsetFromFile()
{ {
std::ifstream rfile(file); std::ifstream rfile(file);
printf("Reading file %s\n", file.c_str());
if (rfile.is_open() && rfile.good()) if (rfile.is_open() && rfile.good())
{ {
std::string buffer; std::string buffer;
@@ -168,6 +170,16 @@ void Text::initOffsetFromFile()
buffer.clear(); buffer.clear();
line_read++; 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}; 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) bool stringToBool(std::string str)
{ {
if (str == "true") if (str == "true")
@@ -542,4 +542,17 @@ bool stringToBool(std::string str)
{ {
return false; 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 // Detector de colisiones entre dos lineas
SDL_Point checkCollision(d_line_t &l1, v_line_t &l2); 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 // Detector de colisiones entre un punto y una linea diagonal
bool checkCollision(SDL_Point &p, d_line_t &l); 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 // Devuelve un color_t a partir de un string
color_t stringToColor(std::string str); 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); bool stringToBool(std::string str);
// Convierte un valor booleano en una cadena
std::string boolToString(bool value);
#endif #endif