diff --git a/source/director.cpp b/source/director.cpp index 414358e..dd7e2ca 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -1,6 +1,6 @@ #include "const.h" -#include "utils.h" #include "director.h" +#include "utils.h" #include #include @@ -8,40 +8,37 @@ Director::Director(std::string path) { + // Inicializa variables + section.name = PROG_SECTION_INTRO; + section.subsection = 0; + // Crea el objeto que controla los ficheros de recursos asset = new Asset(path.substr(0, path.find_last_of("\\/")) + "/../"); - - // Establece la lista de ficheros - Uint8 section = PROG_SECTION_LOGO; - if (!setFileList()) - {// Si falta algún fichero no inicies el programa - section = PROG_SECTION_QUIT; - } - // Crea el objeto de idioma - lang = new Lang(asset); + // Establece la lista de ficheros + if (!setFileList()) + { // Si falta algún fichero no inicia el programa + section.name = PROG_SECTION_QUIT; + } // Crea el puntero a la estructura y carga el fichero de configuración options = new options_t; loadConfigFile(); - // Crea los objetos - input = new Input(asset->get("controllerdb.txt")); - // Inicializa SDL initSDL(); - // Crea el objeto para dibujar en pantalla (Requiere initSDL) - screen = new Screen(window, renderer, options, GAME_WIDTH, GAME_HEIGHT); - // Inicializa JailAudio initJailAudio(); - // Aplica las opciones + // Crea los objetos + lang = new Lang(asset); lang->setLang(options->language); - // Inicializa el resto de variables - init(section); + input = new Input(asset->get("controllerdb.txt")); + initInput(); + + screen = new Screen(window, renderer, options, GAME_WIDTH, GAME_HEIGHT); } Director::~Director() @@ -49,39 +46,21 @@ Director::~Director() saveConfigFile(); delete asset; - asset = nullptr; - delete input; - input = nullptr; - delete screen; - screen = nullptr; - delete lang; - lang = nullptr; - delete options; - options = nullptr; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); - renderer = nullptr; - window = nullptr; SDL_Quit(); } -// Inicia las variables necesarias para arrancar el programa -void Director::init(Uint8 name) +// Inicializa el objeto input +void Director::initInput() { - // Sección - section.name = name; - section.subsection = 0; - - // Textos - lang->setLang(options->language); - - // Controles + // Teclado input->bindKey(INPUT_UP, SDL_SCANCODE_UP); input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN); input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT); @@ -94,6 +73,7 @@ void Director::init(Uint8 name) input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); // PAUSE input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); // ESCAPE + // Mando 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); @@ -103,7 +83,7 @@ void Director::init(Uint8 name) input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_X); input->bindGameControllerButton(INPUT_BUTTON_2, SDL_CONTROLLER_BUTTON_Y); input->bindGameControllerButton(INPUT_BUTTON_3, SDL_CONTROLLER_BUTTON_B); - input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE + input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); // PAUSE input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); // ESCAPE } @@ -282,7 +262,7 @@ bool Director::loadConfigFile() // Crea el fichero para escritura file = SDL_RWFromFile(p.c_str(), "w+b"); if (file != nullptr) - { + { // Ha podido crear el fichero printf("New file (%s) created!\n", filename.c_str()); // Escribe los datos @@ -303,7 +283,7 @@ bool Director::loadConfigFile() SDL_RWclose(file); } else - { + { // No ha podido crear el fichero printf("Error: Unable to create file %s\n", filename.c_str()); success = false; } @@ -327,14 +307,23 @@ bool Director::loadConfigFile() SDL_RWread(file, &options->keepAspect, sizeof(options->keepAspect), 1); // Normaliza los valores - if (!((options->fullScreenMode == 0) || - (options->fullScreenMode == SDL_WINDOW_FULLSCREEN) || - (options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP))) + 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)) + } + + if (options->windowSize < 1 || options->windowSize > 4) + { options->windowSize = 3; - if ((options->language < 0) || (options->language > MAX_LANGUAGES)) + } + + if (options->language < 0 || options->language > MAX_LANGUAGES) + { options->language = en_UK; + } // Cierra el fichero SDL_RWclose(file); @@ -374,22 +363,12 @@ bool Director::saveConfigFile() else { printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError()); + success = false; } + return success; } -// Obtiene el valor de la variable -Uint8 Director::getSubsection() -{ - return section.subsection; -} - -// Obtiene el valor de la variable -Uint8 Director::getSection() -{ - return section.name; -} - // Establece el valor de la variable void Director::setSection(section_t section) { @@ -419,16 +398,8 @@ void Director::runTitle() void Director::runGame() { - if (section.subsection == GAME_SECTION_PLAY_1P) - { - game = new Game(1, 0, renderer, screen, asset, lang, input, false, options); - } - - else if (section.subsection == GAME_SECTION_PLAY_2P) - { - game = new Game(2, 0, renderer, screen, asset, lang, input, false, options); - } - + const int numPlayers = section.subsection == GAME_SECTION_PLAY_1P ? 1 : 2; + game = new Game(numPlayers, 0, renderer, screen, asset, lang, input, false, options); setSection(game->run()); delete game; } @@ -436,9 +407,9 @@ void Director::runGame() void Director::run() { // Bucle principal - while (!(getSection() == PROG_SECTION_QUIT)) + while (!section.name == PROG_SECTION_QUIT) { - switch (getSection()) + switch (section.name) { case PROG_SECTION_LOGO: runLogo(); diff --git a/source/director.h b/source/director.h index a6d90fc..6b8410a 100644 --- a/source/director.h +++ b/source/director.h @@ -4,7 +4,6 @@ #include "asset.h" #include "balloon.h" #include "bullet.h" -#include "coffeedrop.h" #include "const.h" #include "fade.h" #include "game.h" @@ -34,6 +33,7 @@ class Director { private: + // Objetos SDL_Window *window; // La ventana donde dibujamos SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla @@ -44,6 +44,8 @@ private: Input *input; // Objeto Input para gestionar las entradas Lang *lang; // Objeto para gestionar los textos en diferentes idiomas Asset *asset; // Objeto que gestiona todos los ficheros de recursos + + // Variables struct options_t *options; // Variable con todas las opciones del programa section_t section; // Sección y subsección actual del programa; @@ -53,6 +55,9 @@ private: // Arranca SDL y crea la ventana bool initSDL(); + // Inicializa el objeto input + void initInput(); + // Crea el indice de ficheros bool setFileList(); @@ -62,12 +67,6 @@ private: // Guarda el fichero de configuración bool saveConfigFile(); - // Obtiene el valor de la variable - Uint8 getSubsection(); - - // Obtiene el valor de la variable - Uint8 getSection(); - // Establece el valor de la variable void setSection(section_t section); @@ -90,9 +89,6 @@ public: // Destructor ~Director(); - // Inicia las variables necesarias para arrancar el programa - void init(Uint8 name); - // Bucle principal void run(); }; diff --git a/source/main.cpp b/source/main.cpp index da9b9e4..b902b37 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,5 @@ /* + Código fuente creado por JailDesigner (2020) Empezado en Castalla el 15/07/2020. @@ -32,6 +33,10 @@ Los objetos globo tienen varios contadores para alternar de un estado a otro. En los vectores que contienen objetos, se considera activos los objetos que tienen un tipo asociado diferente a NO_KIND + +*** TODO ESTO ya no es valido, pero lo dejo como referencia +Reescribiendo el código el 27/09/2022 + */ #include "director.h" @@ -42,14 +47,13 @@ int main(int argc, char *args[]) printf("Starting the game...\n\n"); // Crea el objeto Director - Director *mDirector = new Director(args[0]); + Director *director = new Director(args[0]); // Bucle principal - mDirector->run(); + director->run(); // Destruye el objeto Director - delete mDirector; - mDirector = nullptr; + delete director; printf("\nShutting down the game...\n");