From 241c63340c03acec792177ac6fe261c21c5f0caf Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 21 Sep 2022 18:22:18 +0200 Subject: [PATCH] =?UTF-8?q?Ya=20guarda=20el=20fichero=20de=20configuraci?= =?UTF-8?q?=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/config/config.txt | 8 ++ data/map/01.map | 32 +---- data/map/01.tmx | 8 +- source/prog.cpp | 291 +++++++++++++++++++++++++++++++++++------ source/prog.h | 14 +- 5 files changed, 276 insertions(+), 77 deletions(-) create mode 100644 data/config/config.txt diff --git a/data/config/config.txt b/data/config/config.txt new file mode 100644 index 0000000..56c3ec4 --- /dev/null +++ b/data/config/config.txt @@ -0,0 +1,8 @@ +fullScreenMode=0 +windowSize=3 +filter=FILTER_NEAREST +vSync=true +integerScale=true +keepAspect=true +borderEnabled=false +borderSize=0.100000 diff --git a/data/map/01.map b/data/map/01.map index f8e722b..c0b3cd5 100644 --- a/data/map/01.map +++ b/data/map/01.map @@ -1,6 +1,6 @@ tileset_img=surface.png enemy_file=01.ene -bgColor1=234,171,159 +bgColor1=0,171,159 bgColor2=144,225,231 room_up=0 @@ -14,36 +14,6 @@ room_right=02.map [actors] -[moving platform] -tileset=moving_platform.png -animation=moving_platform.ani -width=16 -height=8 -x=9 -y=11 -vx=0.3 -vy=0 -x1=9 -y1=11 -x2=15 -y2=11 -[/moving platform] - -[moving platform] -tileset=moving_platform.png -animation=moving_platform.ani -width=16 -height=8 -x=20 -y=14 -vx=0 -vy=0.3 -x1=20 -y1=14 -x2=20 -y2=21 -[/moving platform] - [diamond] tileset=diamond.png animation=diamond.ani diff --git a/data/map/01.tmx b/data/map/01.tmx index d484a60..b7bf4f9 100644 --- a/data/map/01.tmx +++ b/data/map/01.tmx @@ -8,10 +8,10 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2031,2032,2033,2034,2035,2036,2037,2038,2039,2040,2041,2042,2043,2044,2045,2046,2047,2048,2049,2050,2051,2052,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2106,2107,2108,2109,2110,2111,2112,2113,2114,2115,2116,2117,2118,2119,2120,2121,2122,2123,2124,2125,2126,2127,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2181,2182,2183,2184,2185,2186,2187,2188,2189,2190,2191,2192,2193,2194,2195,2196,2197,2198,2199,2200,2201,2202,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2256,2257,2258,2259,2260,2261,2262,2263,2264,2265,2266,2267,2268,2269,2270,2271,2272,2273,2274,2275,2276,2277,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2331,2332,2333,2334,2335,2336,2337,2338,2339,2340,2341,2342,2343,2344,2345,2346,2347,2348,2349,2350,2351,2352,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2406,2407,2408,2409,2410,2411,2412,2413,2414,2415,2416,2417,2418,2419,2420,2421,2422,2423,2424,2425,2426,2427,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2481,2482,2483,2484,2485,2486,2487,2488,2489,2490,2491,2492,2493,2494,2495,2496,2497,2498,2499,2500,2501,2502,0, +0,0,0,0,0,0,0,2176,2177,2178,2179,0,0,0,0,0,0,2256,2257,2258,2259,2260,2261,2262,2263,2264,2265,2266,2267,2268,2269,2270,2271,2272,2273,2274,2275,2276,2277,0, +0,0,0,0,0,0,0,2251,2252,2253,2254,0,0,0,0,0,0,2331,2332,2333,2334,2335,2336,2337,2338,2339,2340,2341,2342,2343,2344,2345,2346,2347,2348,2349,2350,2351,2352,0, +0,0,0,0,0,0,0,2326,2327,2328,2329,0,0,0,0,0,0,2406,2407,2408,2409,2410,2411,2412,2413,2414,2415,2416,2417,2418,2419,2420,2421,2422,2423,2424,2425,2426,2427,0, +0,0,0,0,0,0,0,2401,2402,2403,2404,0,0,0,0,0,0,2481,2482,2483,2484,2485,2486,2487,2488,2489,2490,2491,2492,2493,2494,2495,2496,2497,2498,2499,2500,2501,2502,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2557,2558,2559,2560,2561,2562,2563,2564,2565,2566,2567,2568,2569,2570,2571,2572,2573,2574,2575,2576,2577,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, diff --git a/source/prog.cpp b/source/prog.cpp index 4a57dc2..1fd964f 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -1,25 +1,12 @@ #include "prog.h" // Constructor -Prog::Prog(std::string executablePath) +Prog::Prog(std::string path) { - // Establece las opciones por defecto - options = new options_t; - options->fullScreenMode = 0; - options->windowSize = 2; - 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; + // Crea el objeto que controla los ficheros de recursos + asset = new Asset(path.substr(0, path.find_last_of("\\/"))); - // Inicia las librerias - initSDL(); - initJailAudio(); - - // Crea los objetos - asset = new Asset(executablePath.substr(0, executablePath.find_last_of("\\/"))); + // Si falta algún fichero no inicia el programa if (!setFileList()) { section.name = SECTION_PROG_QUIT; @@ -27,37 +14,31 @@ Prog::Prog(std::string executablePath) else { section.name = SECTION_PROG_GAME; + section.subsection = 0; } + + // Inicializa variables desde el fichero de configuración + loadConfig(); + + // Inicializa SDL + initSDL(); + + // Inicializa JailAudio + initJailAudio(); + + // Crea los objetos input = new Input(asset->get("gamecontrollerdb.txt")); + initInput(); screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); screen->setBorderColor(borderColor); - - // Controles - input->bindKey(INPUT_UP, SDL_SCANCODE_UP); - input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN); - input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT); - input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT); - input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN); - input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE); - input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE); - input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D); - input->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_R); - input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); - input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); - - input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP); - input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN); - input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT); - input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); - input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B); - input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A); - input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B); - input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); - input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); + screen->setVideoMode(options->fullScreenMode); } Prog::~Prog() { + // Guarda las opciones de configuración + saveConfig(); + delete options; delete asset; delete input; @@ -68,6 +49,207 @@ Prog::~Prog() SDL_Quit(); } +// Carga el fichero de configuración +bool Prog::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->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; + } + } + } + + // Cierra el fichero + 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 + if (options->borderEnabled) + { + const int incWidth = GAMECANVAS_WIDTH * options->borderSize; + const int incHeight = GAMECANVAS_HEIGHT * options->borderSize; + options->screenWidth = (GAMECANVAS_WIDTH + incWidth) * options->windowSize; + options->screenHeight = (GAMECANVAS_HEIGHT + incHeight) * options->windowSize; + } + else + { + options->screenWidth = GAMECANVAS_WIDTH * options->windowSize; + options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize; + } + + return success; +} + +// Guarda el fichero de configuración +bool Prog::saveConfig() +{ + bool success = true; + + // Crea y abre el fichero de texto + std::ofstream file(asset->get("config.txt")); + + // Escribe en el fichero + if (options->fullScreenMode == 0) + { + file << "fullScreenMode=0\n"; + } + + else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN) + { + file << "fullScreenMode=SDL_WINDOW_FULLSCREEN\n"; + } + + 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 + { + 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; +} + +// Asigna variables a partir de dos cadenas +bool Prog::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 == "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_LINEAL") + { + options->filter = FILTER_LINEAL; + } + else + { + options->filter = FILTER_NEAREST; + } + } + + 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); + if (options->borderSize < 0.0f || options->borderSize > 0.5f) + { + options->borderSize = 0.1f; + } + } + + else if (var == "") + { + } + + else + { + success = false; + } + + return success; +} + // Inicializa JailAudio void Prog::initJailAudio() { @@ -135,6 +317,33 @@ bool Prog::initSDL() return success; } +// Inicia las variables necesarias para arrancar el programa +void Prog::initInput() +{ + // Inicializa los controles + input->bindKey(INPUT_UP, SDL_SCANCODE_UP); + input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN); + input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT); + input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT); + input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN); + input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE); + input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE); + input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D); + input->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_R); + input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); + input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); + + input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP); + input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN); + input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT); + input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); + input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B); + input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A); + input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B); + input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); + input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); +} + // Crea el indice de ficheros de recursos bool Prog::setFileList() { @@ -165,7 +374,7 @@ bool Prog::setFileList() asset->add("/data/map/surface.png", bitmap); // Ficheros de configuración - asset->add("/data/config.bin", data, false); + asset->add("/data/config/config.txt", data, false); asset->add("/data/input/gamecontrollerdb.txt", data); // Ficheros del jugador diff --git a/source/prog.h b/source/prog.h index 0e50060..97ac181 100644 --- a/source/prog.h +++ b/source/prog.h @@ -32,12 +32,24 @@ private: section_t section; // Sección y subsección actual del programa; struct options_t *options; // Contiene las opciones 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(); // Arranca SDL y crea la ventana bool initSDL(); + // Inicializa el objeto Input + void initInput(); + // Crea el indice de ficheros bool setFileList(); @@ -64,7 +76,7 @@ private: public: // Constructor - Prog(std::string executablePath); + Prog(std::string path); // Destructor ~Prog();