From c5aa28d738863d830c9b6f808a393f4ee8e11232 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 12 Sep 2022 14:07:59 +0200 Subject: [PATCH] =?UTF-8?q?Trabajando=20en=20el=20fichero=20de=20configura?= =?UTF-8?q?ci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/config.txt | 9 ++ source/director.cpp | 236 ++++++++++++++++++++++++++++++++++++++++++-- source/director.h | 9 ++ source/room.cpp | 7 +- source/screen.cpp | 14 ++- source/screen.h | 5 +- source/utils.cpp | 13 +++ source/utils.h | 7 +- 8 files changed, 275 insertions(+), 25 deletions(-) create mode 100644 data/config.txt diff --git a/data/config.txt b/data/config.txt new file mode 100644 index 0000000..9752ea6 --- /dev/null +++ b/data/config.txt @@ -0,0 +1,9 @@ +### NO TOCAR SI NO SE SABE LO QUE SE ESTÁ HACIENDO +fullScreenMode=0 +windowSize=1 +filter=FILTER_NEAREST +vSync=true +integerScale=true +keepAspect=true +borderEnabled=true +borderSize=0.1f \ No newline at end of file diff --git a/source/director.cpp b/source/director.cpp index ac6d91d..3e9fdb4 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -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); diff --git a/source/director.h b/source/director.h index 998535b..e195446 100644 --- a/source/director.h +++ b/source/director.h @@ -37,6 +37,15 @@ private: std::string executablePath; // Path del ejecutable section_t section; // Sección y subsección actual del programa; + // Carga el fichero de configuración + bool loadConfig(); + + // Guarda el fichero de configuración + bool saveConfig(); + + // Asigna variables a partir de dos cadenas + bool setOptions(options_t *options, std::string var, std::string value); + // Inicializa jail_audio void initJailAudio(); diff --git a/source/room.cpp b/source/room.cpp index 94942ec..11d1836 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -65,14 +65,14 @@ Room::~Room() } // Carga las variables desde un fichero -bool Room::load(std::string _file_path) +bool Room::load(std::string file_path) { // Indicador de éxito en la carga bool success = true; - std::string filename = _file_path.substr(_file_path.find_last_of("\\/") + 1); + std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1); std::string line; - std::ifstream file(_file_path); + std::ifstream file(file_path); // El fichero se puede abrir if (file.good()) @@ -275,6 +275,7 @@ 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 33af512..dbf817e 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -15,9 +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 = true; - borderWidth = 0.1f; - borderHeight = 0.1f; + borderEnabled = false; + borderSize = 0.1f; // Crea la textura donde se dibujan los graficos del juego gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); @@ -83,8 +82,8 @@ void Screen::setVideoMode(int fullScreenMode) { if (borderEnabled) { - const int incWidth = gameCanvasWidth * borderWidth; - const int incHeight = gameCanvasHeight * borderHeight; + const int incWidth = gameCanvasWidth * borderSize; + const int incHeight = gameCanvasHeight * borderSize; screenWidth = gameCanvasWidth + incWidth; screenHeight = gameCanvasHeight + incHeight; dest = {0 + (incWidth / 2), 0 + (incHeight / 2), gameCanvasWidth, gameCanvasHeight}; @@ -189,10 +188,9 @@ void Screen::setBlendMode(SDL_BlendMode blendMode) } // Establece el tamaño del borde -void Screen::setBorderSize(float w, float h) +void Screen::setBorderSize(float s) { - borderWidth = w; - borderHeight = h; + borderSize = s; } // Establece si se ha de ver el borde en el modo ventana diff --git a/source/screen.h b/source/screen.h index f36b646..aa013ee 100644 --- a/source/screen.h +++ b/source/screen.h @@ -33,8 +33,7 @@ private: SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana - float borderWidth; // Porcentaje de borde que se añade a lo ancho - float borderHeight; // Porcentaje de borde que se añade a lo alto + float borderSize; // Porcentaje de borde que se añade a lo ventana public: // Constructor @@ -68,7 +67,7 @@ public: void setBlendMode(SDL_BlendMode blendMode); // Establece el tamaño del borde - void setBorderSize(float w, float h); + void setBorderSize(float s); // Establece si se ha de ver el borde en el modo ventana void setBorderEnabled(bool value); diff --git a/source/utils.cpp b/source/utils.cpp index aace260..0591cc0 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -529,4 +529,17 @@ color_t stringToColor(std::string str) } return {0x00, 0x00, 0x00}; +} + +// Devuelve un color_t a partir de un string +bool stringToBool(std::string str) +{ + if (str == "true") + { + return true; + } + else + { + return false; + } } \ No newline at end of file diff --git a/source/utils.h b/source/utils.h index be647f3..89ea4ce 100644 --- a/source/utils.h +++ b/source/utils.h @@ -84,6 +84,8 @@ struct options_t int screenHeight; // Alto de la pantalla o ventana bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa + bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana + float borderSize; // Porcentaje de borde que se añade a lo ventana }; // Calcula el cuadrado de la distancia entre dos puntos @@ -117,7 +119,7 @@ SDL_Point checkCollision(line_t &l1, 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); +// 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); @@ -128,4 +130,7 @@ 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 +bool stringToBool(std::string str); + #endif \ No newline at end of file