From 6b7769ca3ce6ef8d6066087e77a0f205cce245de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Mon, 12 Sep 2022 19:06:15 +0200 Subject: [PATCH] Ya guarda y lee la configuracion --- data/config.txt | 7 +- source/director.cpp | 169 +++++++++++++++++++------------------------- source/game.h | 1 - source/room.cpp | 8 ++- source/screen.cpp | 10 +-- source/text.cpp | 12 ++++ source/utils.cpp | 15 +++- source/utils.h | 8 +-- 8 files changed, 116 insertions(+), 114 deletions(-) diff --git a/data/config.txt b/data/config.txt index 9752ea6..56c3ec4 100644 --- a/data/config.txt +++ b/data/config.txt @@ -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 \ No newline at end of file +borderEnabled=false +borderSize=0.100000 diff --git a/source/director.cpp b/source/director.cpp index 3e9fdb4..b0a1a09 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -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 == "") diff --git a/source/game.h b/source/game.h index b8335c7..8ad467d 100644 --- a/source/game.h +++ b/source/game.h @@ -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; diff --git a/source/room.cpp b/source/room.cpp index 11d1836..b19f6c3 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -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 != ""); } } + + // 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 diff --git a/source/screen.cpp b/source/screen.cpp index dbf817e..ee0c164 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -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 diff --git a/source/text.cpp b/source/text.cpp index 16011fe..d7ed473 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -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()); } } diff --git a/source/utils.cpp b/source/utils.cpp index 0591cc0..1a6d53b 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -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") @@ -542,4 +542,17 @@ 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"; + } } \ No newline at end of file diff --git a/source/utils.h b/source/utils.h index 89ea4ce..efe144c 100644 --- a/source/utils.h +++ b/source/utils.h @@ -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 \ No newline at end of file