From fa82758ce15931c44c2dfbd61551c59ed4e2c3d3 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 28 Sep 2024 13:49:00 +0200 Subject: [PATCH] Don melitonada la classe Asset --- source/asset.cpp | 27 +++++++++++++++++++++++++++ source/asset.h | 19 +++++++++++++++++-- source/background.cpp | 4 ++-- source/background.h | 2 +- source/director.cpp | 25 +++++++++++++------------ source/game.cpp | 8 ++++---- source/game.h | 2 +- source/game_logo.cpp | 4 ++-- source/game_logo.h | 2 +- source/hiscore_table.cpp | 6 +++--- source/hiscore_table.h | 2 +- source/instructions.cpp | 6 +++--- source/instructions.h | 2 +- source/intro.cpp | 6 +++--- source/intro.h | 2 +- source/logo.cpp | 6 +++--- source/logo.h | 2 +- source/scoreboard.cpp | 4 ++-- source/scoreboard.h | 2 +- source/screen.cpp | 10 +++++----- source/screen.h | 6 +++--- source/title.cpp | 6 +++--- source/title.h | 2 +- 23 files changed, 99 insertions(+), 56 deletions(-) diff --git a/source/asset.cpp b/source/asset.cpp index 9e4ee1b..530ee47 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -1,6 +1,27 @@ #include "asset.h" #include +// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado +Asset *Asset::asset = nullptr; + +// [SINGLETON] Crearemos el objeto asset con esta función estática +void Asset::init(std::string executablePath) +{ + Asset::asset = new Asset(executablePath); +} + +// [SINGLETON] Destruiremos el objeto asset con esta función estática +void Asset::destroy() +{ + delete Asset::asset; +} + +// [SINGLETON] Con este método obtenemos el objeto asset y podemos trabajar con él +Asset *Asset::get() +{ + return Asset::asset; +} + // Constructor Asset::Asset(std::string executablePath) { @@ -13,6 +34,12 @@ Asset::Asset(std::string executablePath) #endif } +// Destructor +Asset::~Asset() +{ + +} + // Añade un elemento a la lista void Asset::add(std::string file, enum assetType type, bool required, bool absolute) { diff --git a/source/asset.h b/source/asset.h index 36c100f..d2d0b89 100644 --- a/source/asset.h +++ b/source/asset.h @@ -22,13 +22,16 @@ enum assetType class Asset { private: + // [SINGLETON] Objeto asset privado para Don Melitón + static Asset *asset; + // Estructura para definir un item struct item_t { std::string file; // Ruta del fichero desde la raiz del directorio enum assetType type; // Indica el tipo de recurso bool required; // Indica si es un fichero que debe de existir - //bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta + // bool absolute; // Indica si la ruta que se ha proporcionado es una ruta absoluta }; // Variables @@ -43,10 +46,22 @@ private: // Devuelve el nombre del tipo de recurso std::string getTypeName(int type); -public: // Constructor Asset(std::string path); + // Destructor + ~Asset(); + +public: + // [SINGLETON] Crearemos el objeto screen con esta función estática + static void init(std::string path); + + // [SINGLETON] Destruiremos el objeto screen con esta función estática + static void destroy(); + + // [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él + static Asset *get(); + // Añade un elemento a la lista void add(std::string file, enum assetType type, bool required = true, bool absolute = false); diff --git a/source/background.cpp b/source/background.cpp index 832069a..949b7f7 100644 --- a/source/background.cpp +++ b/source/background.cpp @@ -2,11 +2,11 @@ #include "param.h" // Constructor -Background::Background(SDL_Renderer *renderer, Asset *asset) +Background::Background(SDL_Renderer *renderer) { // Copia los punteros this->renderer = renderer; - this->asset = asset; + asset = Asset::get(); // Carga las texturas buildingsTexture = new Texture(renderer, asset->get("game_buildings.png")); diff --git a/source/background.h b/source/background.h index 51b8ff9..f926733 100644 --- a/source/background.h +++ b/source/background.h @@ -105,7 +105,7 @@ private: public: // Constructor - Background(SDL_Renderer *renderer, Asset *asset); + Background(SDL_Renderer *renderer); // Destructor ~Background(); diff --git a/source/director.cpp b/source/director.cpp index bfdeb1f..2dc048f 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -33,7 +33,8 @@ Director::Director(int argc, char *argv[]) createSystemFolder("jailgames/coffee_crisis_arcade_edition"); // Crea el objeto que controla los ficheros de recursos - asset = new Asset(executablePath); + Asset::init(executablePath); + asset = Asset::get(); // Si falta algún fichero no inicia el programa if (!setFileList()) @@ -72,7 +73,7 @@ Director::Director(int argc, char *argv[]) input = new Input(asset->get("gamecontrollerdb.txt")); initInput(); - Screen::Init(window, renderer, asset, input); + Screen::init(window, renderer, input); screen = Screen::get(); // Carga los sonidos del juego @@ -86,9 +87,9 @@ Director::~Director() { saveOptionsFile(asset->get("config.txt")); - delete asset; delete input; - Screen::Destroy(); + Asset::destroy(); + Screen::destroy(); deleteSounds(); deleteMusics(); @@ -597,7 +598,7 @@ void Director::deleteMusics() // Ejecuta la sección con el logo void Director::runLogo() { - logo = new Logo(asset, input); + logo = new Logo(input); logo->run(); delete logo; } @@ -605,7 +606,7 @@ void Director::runLogo() // Ejecuta la sección con la secuencia de introducción void Director::runIntro() { - intro = new Intro(asset, input, getMusic(musics, "intro.ogg")); + intro = new Intro(input, getMusic(musics, "intro.ogg")); intro->run(); delete intro; } @@ -613,7 +614,7 @@ void Director::runIntro() // Ejecuta la sección con el titulo del juego void Director::runTitle() { - title = new Title(asset, input, getMusic(musics, "title.ogg")); + title = new Title(input, getMusic(musics, "title.ogg")); title->run(); delete title; } @@ -621,9 +622,9 @@ void Director::runTitle() // Ejecuta la sección donde se juega al juego void Director::runGame() { - const int playerID = section::options; + const int playerID = section::options == section::OPTIONS_GAME_PLAY_1P ? 1 : 2; const int currentStage = 0; - game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, asset, input, getMusic(musics, "playing.ogg")); + game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, input, getMusic(musics, "playing.ogg")); game->run(); delete game; } @@ -631,7 +632,7 @@ void Director::runGame() // Ejecuta la sección donde se muestran las instrucciones void Director::runInstructions() { - instructions = new Instructions(asset, input, getMusic(musics, "title.ogg")); + instructions = new Instructions(input, getMusic(musics, "title.ogg")); instructions->run(); delete instructions; } @@ -639,7 +640,7 @@ void Director::runInstructions() // Ejecuta la sección donde se muestra la tabla de puntuaciones void Director::runHiScoreTable() { - hiScoreTable = new HiScoreTable(asset, input, getMusic(musics, "title.ogg")); + hiScoreTable = new HiScoreTable(input, getMusic(musics, "title.ogg")); hiScoreTable->run(); delete hiScoreTable; } @@ -649,7 +650,7 @@ void Director::runDemoGame() { const int playerID = (rand() % 2) + 1; const int currentStage = 0; - demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, asset, input, nullptr); + demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, input, nullptr); demoGame->run(); delete demoGame; } diff --git a/source/game.cpp b/source/game.cpp index 8d7a49b..8d2660d 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -5,11 +5,11 @@ #define GAME_OVER_COUNTER 350 // Constructor -Game::Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input, JA_Music_t *music) +Game::Game(int playerID, int currentStage, bool demo, Input *input, JA_Music_t *music) { // Copia los punteros screen = Screen::get(); - this->asset = asset; + asset = Asset::get(); this->input = input; this->music = music; renderer = screen->getRenderer(); @@ -23,8 +23,8 @@ Game::Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input // Crea los objetos fade = new Fade(renderer); eventHandler = new SDL_Event(); - scoreboard = new Scoreboard(renderer, asset); - background = new Background(renderer, asset); + scoreboard = new Scoreboard(renderer); + background = new Background(renderer); explosions = new Explosions(); enemyFormations = new EnemyFormations(); diff --git a/source/game.h b/source/game.h index eb9e8b6..b8222e1 100644 --- a/source/game.h +++ b/source/game.h @@ -447,7 +447,7 @@ private: public: // Constructor - Game(int playerID, int currentStage, bool demo, Asset *asset, Input *input, JA_Music_t *music); + Game(int playerID, int currentStage, bool demo, Input *input, JA_Music_t *music); // Destructor ~Game(); diff --git a/source/game_logo.cpp b/source/game_logo.cpp index 10487a5..12c5cca 100644 --- a/source/game_logo.cpp +++ b/source/game_logo.cpp @@ -2,12 +2,12 @@ #include "param.h" // Constructor -GameLogo::GameLogo(Asset *asset, int x, int y) +GameLogo::GameLogo(int x, int y) { // Copia los punteros screen = Screen::get(); renderer = screen->getRenderer(); - this->asset = asset; + asset = Asset::get(); this->x = x; this->y = y; diff --git a/source/game_logo.h b/source/game_logo.h index e444fb8..ac424c9 100644 --- a/source/game_logo.h +++ b/source/game_logo.h @@ -60,7 +60,7 @@ private: public: // Constructor - GameLogo(Asset *asset, int x, int y); + GameLogo(int x, int y); // Destructor ~GameLogo(); diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index 8b3684a..e6db35f 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -4,11 +4,11 @@ #include // Constructor -HiScoreTable::HiScoreTable(Asset *asset, Input *input, JA_Music_t *music) +HiScoreTable::HiScoreTable(Input *input, JA_Music_t *music) { // Copia punteros screen = Screen::get(); - this->asset = asset; + asset = Asset::get(); this->input = input; this->music = music; renderer = screen->getRenderer(); @@ -16,7 +16,7 @@ HiScoreTable::HiScoreTable(Asset *asset, Input *input, JA_Music_t *music) // Objetos eventHandler = new SDL_Event(); fade = new Fade(renderer); - background = new Background(renderer, asset); + background = new Background(renderer); text = new Text(asset->get("smb2.gif"), asset->get("smb2.txt"), renderer); // Crea un backbuffer para el renderizador diff --git a/source/hiscore_table.h b/source/hiscore_table.h index 2d17005..e411706 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -79,7 +79,7 @@ private: public: // Constructor - HiScoreTable(Asset *asset, Input *input, JA_Music_t *music); + HiScoreTable(Input *input, JA_Music_t *music); // Destructor ~HiScoreTable(); diff --git a/source/instructions.cpp b/source/instructions.cpp index 9a545ce..2fc26bc 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -4,13 +4,13 @@ #include // Constructor -Instructions::Instructions(Asset *asset, Input *input, JA_Music_t *music) +Instructions::Instructions(Input *input, JA_Music_t *music) { // Copia los punteros - screen = Screen::get(); - this->asset = asset; this->input = input; this->music = music; + screen = Screen::get(); + asset = Asset::get(); renderer = screen->getRenderer(); // Crea objetos diff --git a/source/instructions.h b/source/instructions.h index f30c37c..0a2961b 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -86,7 +86,7 @@ private: public: // Constructor - Instructions(Asset *asset, Input *input, JA_Music_t *music); + Instructions(Input *input, JA_Music_t *music); // Destructor ~Instructions(); diff --git a/source/intro.cpp b/source/intro.cpp index ba853f0..3ce6fba 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -3,13 +3,13 @@ #include "options.h" // Constructor -Intro::Intro(Asset *asset, Input *input, JA_Music_t *music) +Intro::Intro(Input *input, JA_Music_t *music) { // Copia los punteros - screen = Screen::get(); - this->asset = asset; this->input = input; this->music = music; + asset = Asset::get(); + screen = Screen::get(); SDL_Renderer *renderer = screen->getRenderer(); // Reserva memoria para los objetos diff --git a/source/intro.h b/source/intro.h index ffd381c..29ca6be 100644 --- a/source/intro.h +++ b/source/intro.h @@ -60,7 +60,7 @@ private: public: // Constructor - Intro(Asset *asset, Input *input, JA_Music_t *music); + Intro(Input *input, JA_Music_t *music); // Destructor ~Intro(); diff --git a/source/logo.cpp b/source/logo.cpp index 788d1b4..b223d6e 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -4,12 +4,12 @@ #include // Constructor -Logo::Logo(Asset *asset, Input *input) +Logo::Logo(Input *input) { // Copia la dirección de los objetos - screen = Screen::get(); - this->asset = asset; this->input = input; + screen = Screen::get(); + asset = Asset::get(); SDL_Renderer *renderer = screen->getRenderer(); // Reserva memoria para los punteros diff --git a/source/logo.h b/source/logo.h index 966214b..59bf6e2 100644 --- a/source/logo.h +++ b/source/logo.h @@ -71,7 +71,7 @@ private: public: // Constructor - Logo(Asset *asset, Input *input); + Logo(Input *input); // Destructor ~Logo(); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 17300a2..c1cc9b0 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -2,11 +2,11 @@ #include // Constructor -Scoreboard::Scoreboard(SDL_Renderer *renderer, Asset *asset) +Scoreboard::Scoreboard(SDL_Renderer *renderer) { // Copia los punteros this->renderer = renderer; - this->asset = asset; + asset = Asset::get(); // Inicializa punteros gamePowerMeterTexture = nullptr; diff --git a/source/scoreboard.h b/source/scoreboard.h index 87b4dab..d2d216f 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -91,7 +91,7 @@ private: public: // Constructor - Scoreboard(SDL_Renderer *renderer, Asset *asset); + Scoreboard(SDL_Renderer *renderer); // Destructor ~Scoreboard(); diff --git a/source/screen.cpp b/source/screen.cpp index 0653152..6384270 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -13,13 +13,13 @@ Screen *Screen::screen = nullptr; // [SINGLETON] Crearemos el objeto screen con esta función estática -void Screen::Init(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input) +void Screen::init(SDL_Window *window, SDL_Renderer *renderer, Input *input) { - Screen::screen = new Screen(window, renderer, asset, input); + Screen::screen = new Screen(window, renderer, input); } // [SINGLETON] Destruiremos el objeto screen con esta función estática -void Screen::Destroy() +void Screen::destroy() { delete Screen::screen; } @@ -31,13 +31,13 @@ Screen *Screen::get() } // Constructor -Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input) +Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Input *input) { // Copia punteros this->window = window; this->renderer = renderer; - this->asset = asset; this->input = input; + asset = Asset::get(); // Inicializa variables SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight); diff --git a/source/screen.h b/source/screen.h index 092a363..a8aa8fe 100644 --- a/source/screen.h +++ b/source/screen.h @@ -81,17 +81,17 @@ private: // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera // Constructor - Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input); + Screen(SDL_Window *window, SDL_Renderer *renderer, Input *input); // Destructor ~Screen(); public: // [SINGLETON] Crearemos el objeto screen con esta función estática - static void Init(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input); + static void init(SDL_Window *window, SDL_Renderer *renderer, Input *input); // [SINGLETON] Destruiremos el objeto screen con esta función estática - static void Destroy(); + static void destroy(); // [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él static Screen *get(); diff --git a/source/title.cpp b/source/title.cpp index 896774a..5f43a5d 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -3,12 +3,12 @@ #include "options.h" // Constructor -Title::Title(Asset *asset, Input *input, JA_Music_t *music) +Title::Title(Input *input, JA_Music_t *music) { // Copia las direcciones de los punteros y objetos this->input = input; - this->asset = asset; this->music = music; + asset = Asset::get(); screen = Screen::get(); SDL_Renderer *renderer = screen->getRenderer(); @@ -26,7 +26,7 @@ Title::Title(Asset *asset, Input *input, JA_Music_t *music) tiledbg = new Tiledbg(asset->get("title_bg_tile.png"), {0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM); - gameLogo = new GameLogo(asset, param.game.gameArea.centerX, param.title.titleCCPosition); + gameLogo = new GameLogo(param.game.gameArea.centerX, param.title.titleCCPosition); gameLogo->enable(); defineButtons = new DefineButtons(input, text2); diff --git a/source/title.h b/source/title.h index a2fc574..8128246 100644 --- a/source/title.h +++ b/source/title.h @@ -101,7 +101,7 @@ private: public: // Constructor - Title(Asset *asset, Input *input, JA_Music_t *music); + Title(Input *input, JA_Music_t *music); // Destructor ~Title();