From bd24db6f47af0a22a3f4e30cb7ff4bd06cf7009f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Wed, 26 Oct 2022 22:08:52 +0200 Subject: [PATCH] Movida la carga de recursos al objeto director --- .gitignore | 3 +- source/demo.cpp | 71 +------------------------------------------ source/demo.h | 3 -- source/director.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ source/director.h | 7 +++-- source/enemy.cpp | 2 -- source/game.cpp | 70 +----------------------------------------- source/game.h | 3 -- source/item.cpp | 2 -- source/scoreboard.cpp | 9 +++--- source/scoreboard.h | 4 ++- 11 files changed, 87 insertions(+), 158 deletions(-) diff --git a/.gitignore b/.gitignore index f033058..3337437 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ resources scripts *.opk *.DS_Store -jaildoctor* \ No newline at end of file +jaildoctor* +*plist* \ No newline at end of file diff --git a/source/demo.cpp b/source/demo.cpp index f762ee2..a0a49c8 100644 --- a/source/demo.cpp +++ b/source/demo.cpp @@ -22,12 +22,9 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as this->debug = debug; this->options = options; - // Carga los recursos - loadResources(); - // Crea los objetos itemTracker = new ItemTracker(); - scoreboard = new ScoreBoard(renderer, asset, options, &board); + scoreboard = new ScoreBoard(renderer, resource, asset, options, &board); room = new Room(asset->get(currentRoom), renderer, screen, resource, asset, options, itemTracker, &board.items, debug); eventHandler = new SDL_Event(); text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); @@ -51,8 +48,6 @@ Demo::Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as Demo::~Demo() { // Libera la memoria de los objetos - resource->freeTextures(); - //delete resource; delete itemTracker; delete scoreboard; delete room; @@ -228,68 +223,4 @@ void Demo::checkRoomChange() changeRoom(rooms.at(roomIndex)); } } -} - -// Carga los recursos -void Demo::loadResources() -{ - std::vector textureList; - - // Jugador - textureList.push_back("player.png"); - - // Tilesets - textureList.push_back("standard.png"); - textureList.push_back("standard_zxarne.png"); - - // Enemigos - textureList.push_back("paco.png"); - textureList.push_back("chip.png"); - textureList.push_back("wave.png"); - textureList.push_back("wave_v.png"); - textureList.push_back("sigmasua.png"); - textureList.push_back("diskette.png"); - textureList.push_back("bird.png"); - textureList.push_back("bin.png"); - textureList.push_back("qvoid.png"); - textureList.push_back("batman.png"); - textureList.push_back("tuno.png"); - textureList.push_back("matatunos.png"); - textureList.push_back("abad.png"); - textureList.push_back("jailbattle_human.png"); - textureList.push_back("jailbattle_alien.png"); - textureList.push_back("jailer.png"); - textureList.push_back("jailer2.png"); - textureList.push_back("jailer3.png"); - textureList.push_back("printer.png"); - textureList.push_back("code.png"); - textureList.push_back("demon.png"); - textureList.push_back("dimallas.png"); - textureList.push_back("dimallas_v.png"); - textureList.push_back("heavy.png"); - textureList.push_back("spider.png"); - textureList.push_back("macaronni_ted.png"); - textureList.push_back("mummy.png"); - textureList.push_back("sam.png"); - textureList.push_back("amstrad_character_set.png"); - textureList.push_back("breakout.png"); - textureList.push_back("lamp.png"); - textureList.push_back("bry.png"); - textureList.push_back("tv.png"); - textureList.push_back("tv_panel.png"); - textureList.push_back("arounders_door.png"); - textureList.push_back("arounders_machine.png"); - textureList.push_back("arounder_walk.png"); - textureList.push_back("arounder_stop.png"); - textureList.push_back("arounder_fly.png"); - textureList.push_back("bat.png"); - - // Items - textureList.push_back("items.png"); - - // Texto - textureList.push_back("smb2.png"); - textureList.push_back("debug.png"); - - resource->loadTextures(textureList); } \ No newline at end of file diff --git a/source/demo.h b/source/demo.h index ab0bb1c..70d9e65 100644 --- a/source/demo.h +++ b/source/demo.h @@ -66,9 +66,6 @@ private: // Comprueba si se ha de cambiar de habitación void checkRoomChange(); - // Carga los recursos - void loadResources(); - public: // Constructor Demo(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Debug *debug); diff --git a/source/director.cpp b/source/director.cpp index 0481fd5..c06adc0 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -177,6 +177,73 @@ bool Director::saveConfig() return success; } +// Carga los recursos +void Director::loadResources(section_t section) +{ + if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO) + { + std::vector textureList; + + // Jugador + textureList.push_back("player.png"); + + // Tilesets + textureList.push_back("standard.png"); + textureList.push_back("standard_zxarne.png"); + + // Enemigos + textureList.push_back("paco.png"); + textureList.push_back("chip.png"); + textureList.push_back("wave.png"); + textureList.push_back("wave_v.png"); + textureList.push_back("sigmasua.png"); + textureList.push_back("diskette.png"); + textureList.push_back("bird.png"); + textureList.push_back("bin.png"); + textureList.push_back("qvoid.png"); + textureList.push_back("batman.png"); + textureList.push_back("tuno.png"); + textureList.push_back("matatunos.png"); + textureList.push_back("abad.png"); + textureList.push_back("jailbattle_human.png"); + textureList.push_back("jailbattle_alien.png"); + textureList.push_back("jailer.png"); + textureList.push_back("jailer2.png"); + textureList.push_back("jailer3.png"); + textureList.push_back("printer.png"); + textureList.push_back("code.png"); + textureList.push_back("demon.png"); + textureList.push_back("dimallas.png"); + textureList.push_back("dimallas_v.png"); + textureList.push_back("heavy.png"); + textureList.push_back("spider.png"); + textureList.push_back("macaronni_ted.png"); + textureList.push_back("mummy.png"); + textureList.push_back("sam.png"); + textureList.push_back("amstrad_character_set.png"); + textureList.push_back("breakout.png"); + textureList.push_back("lamp.png"); + textureList.push_back("bry.png"); + textureList.push_back("tv.png"); + textureList.push_back("tv_panel.png"); + textureList.push_back("arounders_door.png"); + textureList.push_back("arounders_machine.png"); + textureList.push_back("arounder_walk.png"); + textureList.push_back("arounder_stop.png"); + textureList.push_back("arounder_fly.png"); + textureList.push_back("bat.png"); + + // Items + textureList.push_back("items.png"); + + // Texto + textureList.push_back("smb2.png"); + textureList.push_back("debug.png"); + + resource->loadTextures(textureList); + } +} + // Asigna variables a partir de dos cadenas bool Director::setOptions(options_t *options, std::string var, std::string value) { @@ -708,18 +775,22 @@ void Director::runCredits() // Ejecuta la seccion de la demo, donde se ven pantallas del juego void Director::runDemo() { + loadResources(section); demo = new Demo(renderer, screen, resource, asset, options, debug); setSection(demo->run()); delete demo; + resource->freeTextures(); } // Ejecuta la seccion de juego donde se juega void Director::runGame() { JA_StopMusic(); + loadResources(section); game = new Game(renderer, screen, resource, asset, options, input, debug); setSection(game->run()); delete game; + resource->freeTextures(); } void Director::run() diff --git a/source/director.h b/source/director.h index ff134a3..f63e108 100644 --- a/source/director.h +++ b/source/director.h @@ -27,6 +27,8 @@ private: SDL_Window *window; // La ventana donde dibujamos SDL_Renderer *renderer; // El renderizador de la ventana Screen *screen; // Objeto encargado de dibujar en pantalla + Resource *resource; // Objeto con los recursos + Asset *asset; // Objeto que gestiona todos los ficheros de recursos Input *input; // Objeto Input para gestionar las entradas Game *game; // Objeto para gestionar la sección del juego Logo *logo; // Objeto para gestionar la sección del logo del programa @@ -34,8 +36,6 @@ private: Intro *intro; // Objeto para gestionar la introducción del juego Credits *credits; // Objeto para gestionar los creditos del juego Demo *demo; // Objeto para gestionar el modo demo, en el que se ven pantallas del juego - Resource *resource; // Objeto con los recursos - Asset *asset; // Objeto que gestiona todos los ficheros de recursos Debug *debug; // Objeto para getsionar la información de debug // Variables @@ -50,6 +50,9 @@ private: // Guarda el fichero de configuración bool saveConfig(); + // Carga los recursos + void loadResources(section_t section); + // Asigna variables a partir de dos cadenas bool setOptions(options_t *options, std::string var, std::string value); diff --git a/source/enemy.cpp b/source/enemy.cpp index 2c85659..945218c 100644 --- a/source/enemy.cpp +++ b/source/enemy.cpp @@ -11,7 +11,6 @@ Enemy::Enemy(enemy_t enemy) renderer = enemy.renderer; // Crea objetos - //texture = new Texture(renderer, asset->get(enemy.tileset)); texture = resource->getTexture(enemy.tileset); sprite = new AnimatedSprite(texture, renderer, asset->get(enemy.animation)); @@ -47,7 +46,6 @@ Enemy::Enemy(enemy_t enemy) // Destructor Enemy::~Enemy() { - //delete texture; delete sprite; } diff --git a/source/game.cpp b/source/game.cpp index 36410cf..88fe0e4 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -26,8 +26,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as // **** // Crea los objetos - loadResources(); - scoreboard = new ScoreBoard(renderer, asset, options, &board); + scoreboard = new ScoreBoard(renderer, resource, asset, options, &board); itemTracker = new ItemTracker(); roomTracker = new RoomTracker(); room = new Room(asset->get(currentRoom), renderer, screen, resource, asset, options, itemTracker, &board.items, debug); @@ -61,9 +60,6 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as Game::~Game() { // Libera la memoria de los objetos - resource->freeTextures(); - //delete resource; - delete scoreboard; delete itemTracker; delete roomTracker; @@ -458,68 +454,4 @@ void Game::renderBlackScreen() screen->clean(); screen->setBorderColor(stringToColor(options->palette, "black")); } -} - -// Carga los recursos -void Game::loadResources() -{ - std::vector textureList; - - // Jugador - textureList.push_back("player.png"); - - // Tilesets - textureList.push_back("standard.png"); - textureList.push_back("standard_zxarne.png"); - - // Enemigos - textureList.push_back("paco.png"); - textureList.push_back("chip.png"); - textureList.push_back("wave.png"); - textureList.push_back("wave_v.png"); - textureList.push_back("sigmasua.png"); - textureList.push_back("diskette.png"); - textureList.push_back("bird.png"); - textureList.push_back("bin.png"); - textureList.push_back("qvoid.png"); - textureList.push_back("batman.png"); - textureList.push_back("tuno.png"); - textureList.push_back("matatunos.png"); - textureList.push_back("abad.png"); - textureList.push_back("jailbattle_human.png"); - textureList.push_back("jailbattle_alien.png"); - textureList.push_back("jailer.png"); - textureList.push_back("jailer2.png"); - textureList.push_back("jailer3.png"); - textureList.push_back("printer.png"); - textureList.push_back("code.png"); - textureList.push_back("demon.png"); - textureList.push_back("dimallas.png"); - textureList.push_back("dimallas_v.png"); - textureList.push_back("heavy.png"); - textureList.push_back("spider.png"); - textureList.push_back("macaronni_ted.png"); - textureList.push_back("mummy.png"); - textureList.push_back("sam.png"); - textureList.push_back("amstrad_character_set.png"); - textureList.push_back("breakout.png"); - textureList.push_back("lamp.png"); - textureList.push_back("bry.png"); - textureList.push_back("tv.png"); - textureList.push_back("tv_panel.png"); - textureList.push_back("arounders_door.png"); - textureList.push_back("arounders_machine.png"); - textureList.push_back("arounder_walk.png"); - textureList.push_back("arounder_stop.png"); - textureList.push_back("arounder_fly.png"); - textureList.push_back("bat.png"); - - // Items - textureList.push_back("items.png"); - - // Texto - textureList.push_back("smb2.png"); - textureList.push_back("debug.png"); - - resource->loadTextures(textureList); } \ No newline at end of file diff --git a/source/game.h b/source/game.h index 3e70ebd..435383f 100644 --- a/source/game.h +++ b/source/game.h @@ -109,9 +109,6 @@ private: // Dibuja la pantalla negra void renderBlackScreen(); - // Carga los recursos - void loadResources(); - public: // Constructor Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, options_t *options, Input *input, Debug *debug); diff --git a/source/item.cpp b/source/item.cpp index 4eea441..46df0fa 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -13,7 +13,6 @@ Item::Item(item_t item) renderer = item.renderer; // Crea objetos - //texture = new Texture(renderer, asset->get(item.tileset)); texture = resource->getTexture(item.tileset); sprite = new Sprite(item.x, item.y, itemSize, itemSize, texture, renderer); @@ -36,7 +35,6 @@ Item::Item(item_t item) // Destructor Item::~Item() { - //delete texture; delete sprite; } diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 4438f9a..6692940 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -3,17 +3,18 @@ #include // Constructor -ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, options_t *options, board_t *board) +ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Resource *resource, Asset *asset, options_t *options, board_t *board) { // Obten punteros a objetos + this->resource = resource; this->asset = asset; this->renderer = renderer; this->board = board; this->options = options; // Reserva memoria para los objetos - playerTexture = new Texture(renderer, asset->get("player.png")); - itemTexture = new Texture(renderer, asset->get("items.png")); + playerTexture = resource->getTexture("player.png"); + itemTexture = resource->getTexture("items.png"); sprite = new AnimatedSprite(playerTexture, renderer, asset->get("player.ani")); sprite->setCurrentAnimation("walk_menu"); text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); @@ -36,8 +37,6 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, options_t *options, // Destructor ScoreBoard::~ScoreBoard() { - delete playerTexture; - delete itemTexture; delete sprite; delete text; } diff --git a/source/scoreboard.h b/source/scoreboard.h index 504500a..1eef3f5 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -3,6 +3,7 @@ #include #include "common/animatedsprite.h" #include "common/asset.h" +#include "common/resource.h" #include "common/text.h" #include "common/utils.h" #include "const.h" @@ -36,6 +37,7 @@ private: Texture *playerTexture; // Textura con los graficos para las vidas AnimatedSprite *sprite; // Sprite para mostrar las vidas en el marcador SDL_Renderer *renderer; // El renderizador de la ventana + Resource *resource; // Objeto con los recursos Asset *asset; // Objeto con la ruta a todos los ficheros de recursos Text *text; // Objeto para escribir texto Texture *itemTexture; // Textura con los graficos para las vidas @@ -56,7 +58,7 @@ private: public: // Constructor - ScoreBoard(SDL_Renderer *renderer, Asset *asset, options_t *options, board_t *board); + ScoreBoard(SDL_Renderer *renderer, Resource *resource, Asset *asset, options_t *options, board_t *board); // Destructor ~ScoreBoard();