From 5b8ea728ca91bf29ccd39e1a6332daa6752fa91e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 20 Jun 2024 13:09:00 +0200 Subject: [PATCH] Los parametros del juego ya se pueden cargar desde un fichero de texto. Falta empezar a sacar parametros hard-coded a ese fichero --- data/config/param.txt | 6 +++ source/common/utils.h | 13 ++++-- source/director.cpp | 31 ++++++------ source/director.h | 4 +- source/fade.cpp | 18 +++---- source/fade.h | 2 + source/load_param.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++ source/load_param.h | 13 ++++++ 8 files changed, 162 insertions(+), 31 deletions(-) create mode 100644 data/config/param.txt create mode 100644 source/load_param.cpp create mode 100644 source/load_param.h diff --git a/data/config/param.txt b/data/config/param.txt new file mode 100644 index 0000000..2e5e9dc --- /dev/null +++ b/data/config/param.txt @@ -0,0 +1,6 @@ +gameWidth 320 +gameHeight 240 +numSquaresWidth 80 +numSquaresHeight 60 +fadeRandomSquaresDelay 1 +fadeRandomSquaresMult 8 \ No newline at end of file diff --git a/source/common/utils.h b/source/common/utils.h index b3b5214..0b52d9b 100644 --- a/source/common/utils.h +++ b/source/common/utils.h @@ -112,12 +112,12 @@ struct op_window_t // Estructura con opciones para el video struct op_video_t { + int gameWidth; // Ancho de la resolucion nativa del juego + int gameHeight; // Alto de la resolucion nativa del juego op_window_t window; // Opciones para la ventana del programa Uint32 mode; // Contiene el valor del modo de pantalla completa Uint32 filter; // Filtro usado para el escalado de la imagen bool vSync; // Indica si se quiere usar vsync o no - int gameWidth; // Ancho de la resolucion nativa del juego - int gameHeight; // Alto de la resolucion nativa del juego 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 op_border_t border; // Opciones para el borde la pantalla de juego @@ -158,10 +158,15 @@ struct options_t struct param_t { - int gameWidth; // Ancho del juego - int gameHeight; // Alto del juego + int gameWidth; // Ancho de la resolucion nativa del juego + int gameHeight; // Alto de la resolucion nativa del juego SDL_Rect scoreboard; // Posición y tamaño del marcador + + int numSquaresWidth; + int numSquaresHeight; + int fadeRandomSquaresDelay; + int fadeRandomSquaresMult; }; // Calcula el cuadrado de la distancia entre dos puntos diff --git a/source/director.cpp b/source/director.cpp index 660f921..3fe68de 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -14,16 +14,10 @@ // Constructor Director::Director(int argc, char *argv[]) { - // Carga los parametros para configurar el juego - loadParams(); - // Inicializa variables section = new section_t(); section->name = SECTION_PROG_TITLE; - // Inicializa las opciones del programa - initOptions(); - // Comprueba los parametros del programa checkProgramArguments(argc, argv); @@ -35,6 +29,9 @@ Director::Director(int argc, char *argv[]) createSystemFolder("jailgames/coffee_crisis_arcade_edition_debug"); #endif + // Inicializa las opciones del programa + initOptions(); + // Crea el objeto que controla los ficheros de recursos asset = new Asset(executablePath); asset->setVerbose(options->console); @@ -45,6 +42,9 @@ Director::Director(int argc, char *argv[]) exit(EXIT_FAILURE); } + // Carga los parametros para configurar el juego + loadParams(); + // Carga el fichero de configuración loadConfigFile(); @@ -178,7 +178,7 @@ bool Director::initSDL() incW = options->video.border.width * 2; incH = options->video.border.height * 2; } - window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (options->video.gameWidth + incW) * options->video.window.size, (options->video.gameHeight + incH) * options->video.window.size, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); + window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (param->gameWidth + incW) * options->video.window.size, (param->gameHeight + incH) * options->video.window.size, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); if (window == nullptr) { if (options->console) @@ -211,7 +211,7 @@ bool Director::initSDL() SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); // Establece el tamaño del buffer de renderizado - SDL_RenderSetLogicalSize(renderer, options->video.gameWidth, options->video.gameHeight); + SDL_RenderSetLogicalSize(renderer, param->gameWidth, param->gameHeight); // Establece el modo de mezcla SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); @@ -238,6 +238,7 @@ bool Director::setFileList() // Ficheros de configuración asset->add(systemFolder + "/config.txt", t_data, false, true); asset->add(systemFolder + "/score.bin", t_data, false, true); + asset->add(prefix + "/data/config/param.txt", t_data); asset->add(prefix + "/data/config/demo.bin", t_data); asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data); @@ -363,10 +364,12 @@ void Director::loadParams() { param = new param_t; - param->gameWidth = 320; - param->gameHeight = 240; + loadParam(param, asset->get("param.txt")); - param->scoreboard = {0, 240-32, 320, 32}; + options->video.window.width = options->video.window.size * param->gameWidth; + options->video.window.height = options->video.window.size * param->gameHeight; + options->video.gameWidth = param->gameWidth; + options->video.gameHeight = param->gameHeight; } // Inicializa las opciones del programa @@ -390,12 +393,10 @@ void Director::initOptions() options->input.push_back(inp); // Opciones de video - options->video.gameWidth = param->gameWidth; - options->video.gameHeight = param->gameHeight; options->video.mode = 0; options->video.window.size = 3; - options->video.window.width = options->video.window.size * options->video.gameWidth; - options->video.window.height = options->video.window.size * options->video.gameHeight; + //options->video.window.width = options->video.window.size * param->gameWidth; + //options->video.window.height = options->video.window.size * param->gameHeight; options->video.filter = FILTER_NEAREST; options->video.vSync = true; options->video.integerScale = true; diff --git a/source/director.h b/source/director.h index 7d5afef..9f123c8 100644 --- a/source/director.h +++ b/source/director.h @@ -22,6 +22,7 @@ #include "logo.h" #include "player.h" #include "title.h" +#include "load_param.h" #ifndef DIRECTOR_H #define DIRECTOR_H @@ -84,9 +85,6 @@ private: // Crea la carpeta del sistema donde guardar datos void createSystemFolder(std::string folder); - // Establece el valor de la variable - void setSection(section_t section); - // Ejecuta la seccion de juego con el logo void runLogo(); diff --git a/source/fade.cpp b/source/fade.cpp index 4a90b1e..23464e9 100644 --- a/source/fade.cpp +++ b/source/fade.cpp @@ -34,8 +34,10 @@ void Fade::init() r = 0; g = 0; b = 0; - numSquaresWidth = 80; - numSquaresHeight = 60; + numSquaresWidth = param->numSquaresWidth; + numSquaresHeight = param->numSquaresHeight; + fadeRandomSquaresDelay = param->fadeRandomSquaresDelay; + fadeRandomSquaresMult = param->fadeRandomSquaresMult; } // Pinta una transición en pantalla @@ -118,9 +120,7 @@ void Fade::update() case FADE_RANDOM_SQUARE: { - const int delay = 1; - const int mult = 8; - if (counter % delay == 0) + if (counter % fadeRandomSquaresDelay == 0) { // Dibujamos sobre el backbuffer SDL_Texture *temp = SDL_GetRenderTarget(renderer); @@ -128,17 +128,17 @@ void Fade::update() // Dibuja el cuadrado correspondiente SDL_SetRenderDrawColor(renderer, r, g, b, 255); - const int index = std::min(counter / delay, (numSquaresWidth * numSquaresHeight) - 1); - for (int i = 0; i < mult; ++i) + const int index = std::min(counter / fadeRandomSquaresDelay, (numSquaresWidth * numSquaresHeight) - 1); + for (int i = 0; i < fadeRandomSquaresMult; ++i) { - SDL_RenderFillRect(renderer, &square[index * mult + i]); + SDL_RenderFillRect(renderer, &square[index * fadeRandomSquaresMult + i]); } // Volvemos a usar el renderizador de forma normal SDL_SetRenderTarget(renderer, temp); } - if (counter * mult / delay >= numSquaresWidth * numSquaresHeight) + if (counter * fadeRandomSquaresMult / fadeRandomSquaresDelay >= numSquaresWidth * numSquaresHeight) { finished = true; } diff --git a/source/fade.h b/source/fade.h index eb98ee0..6a18211 100644 --- a/source/fade.h +++ b/source/fade.h @@ -32,6 +32,8 @@ private: int numSquaresHeight; // Cantidad total de cuadraditos en vertical para el FADE_RANDOM_SQUARE param_t *param; // Puntero con todos los parametros del programa std::vector square; // Vector con los indices de los cuadrados para el FADE_RANDOM_SQUARE + int fadeRandomSquaresDelay; + int fadeRandomSquaresMult; // Inicializa las variables void init(); diff --git a/source/load_param.cpp b/source/load_param.cpp new file mode 100644 index 0000000..365189d --- /dev/null +++ b/source/load_param.cpp @@ -0,0 +1,106 @@ +#include "load_param.h" +#include +#include + +// Asigna variables a partir de dos cadenas +bool setOptions(param_t *param, std::string var, std::string value); + +// Establece valores por defecto a las variables +void initParam(param_t *param) +{ + // Tamaño original del juego + param->gameWidth = 320; + param->gameHeight = 240; + + // Tamaño para el marcador + param->scoreboard = {0, 240 - 32, 320, 32}; + + // Valores para el FADE_RANDOM_SQUARE + param->numSquaresWidth = 80; + param->numSquaresHeight = 60; + param->fadeRandomSquaresDelay = 1; + param->fadeRandomSquaresMult = 8; +} + +// Establece valores para los parametros a partir de un fichero de texto +bool loadParam(param_t *param, std::string filePath) +{ + // Pone valores por defecto a las variables + initParam(param); + + // Indicador de éxito en la carga + bool success = true; + + // Variables para manejar el fichero + std::string line; + std::ifstream file(filePath); + + // Si el fichero se puede abrir + if (file.good()) + { + 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(param, line.substr(0, pos), line.substr(pos + 1, line.length()))) + { + success = false; + } + } + } + + // Cierra el fichero + file.close(); + } + + return success; +} + +// Asigna variables a partir de dos cadenas +bool setOptions(param_t *param, std::string var, std::string value) +{ + // Indicador de éxito en la asignación + bool success = true; + + // Opciones de video + if (var == "gameWidth") + { + param->gameWidth = std::stoi(value); + } + + else if (var == "gameHeight") + { + param->gameHeight = std::stoi(value); + } + + else if (var == "numSquaresWidth") + { + param->numSquaresWidth = std::stoi(value); + } + + else if (var == "numSquaresHeight") + { + param->numSquaresHeight = std::stoi(value); + } + + else if (var == "fadeRandomSquaresDelay") + { + param->fadeRandomSquaresDelay = std::stoi(value); + } + + else if (var == "fadeRandomSquaresMult") + { + param->fadeRandomSquaresMult = std::stoi(value); + } + + else + { + success = false; + } + + return success; +} \ No newline at end of file diff --git a/source/load_param.h b/source/load_param.h new file mode 100644 index 0000000..d866d02 --- /dev/null +++ b/source/load_param.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include "common/utils.h" +#include "const.h" + +#ifndef LOAD_PARAM +#define LOAD_PARAM + +// Establece valores para los parametros a partir de un fichero de texto +bool loadParam(param_t *param, std::string filePath); + +#endif \ No newline at end of file