From 30735f00e8d27a97bdf0c77004b7ac9af18aba58 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 4 Nov 2024 19:08:18 +0100 Subject: [PATCH] Els objectes de Text es precarreguen al inici --- source/define_buttons.cpp | 19 ++++++++++--------- source/define_buttons.h | 2 +- source/director.cpp | 3 +-- source/game.cpp | 2 +- source/hiscore_table.cpp | 2 +- source/hiscore_table.h | 2 +- source/instructions.cpp | 2 +- source/instructions.h | 2 +- source/intro.cpp | 2 +- source/on_screen_help.cpp | 4 ++-- source/resource.cpp | 36 +++++++++++++++++++++++++++++++++++- source/resource.h | 18 ++++++++++++++++++ source/scoreboard.cpp | 2 +- source/scoreboard.h | 2 +- source/title.cpp | 9 ++++----- source/title.h | 3 +-- 16 files changed, 80 insertions(+), 30 deletions(-) diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index 98096c5..f5d1f33 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -1,16 +1,17 @@ #include "define_buttons.h" -#include // Para move -#include "input.h" // Para Input, InputType -#include "lang.h" // Para getText -#include "options.h" // Para OptionsController, Options, options -#include "param.h" // Para Param, param, ParamGame, ParamTitle -#include "section.h" // Para Name, Options, name, options -#include "text.h" // Para Text +#include // Para move +#include "input.h" // Para Input, InputType +#include "lang.h" // Para getText +#include "options.h" // Para OptionsController, Options, options +#include "param.h" // Para Param, param, ParamGame, ParamTitle +#include "resource.h" // Para Resource +#include "section.h" // Para Name, Options, name, options +#include "text.h" // Para Text // Constructor -DefineButtons::DefineButtons(std::unique_ptr text_) +DefineButtons::DefineButtons() : input_(Input::get()), - text_(std::move(text_)) + text_(Resource::get()->getText("8bithud")) { // Inicializa variables x_ = param.game.width / 2; diff --git a/source/define_buttons.h b/source/define_buttons.h index f69bf54..11b4a45 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -58,7 +58,7 @@ private: public: // Constructor - explicit DefineButtons(std::unique_ptr text); + DefineButtons(); // Destructor ~DefineButtons() = default; diff --git a/source/director.cpp b/source/director.cpp index 0b35c99..70a4d8c 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -118,8 +118,7 @@ Director::Director(int argc, const char *argv[]) Resource::init(); Input::init(Asset::get()->get("gamecontrollerdb.txt")); bindInputs(); - auto notifier_text = std::make_shared(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); - Notifier::init(std::string(), notifier_text, Asset::get()->get("notify.wav")); + Notifier::init(std::string(), Resource::get()->getText("8bithud"), Asset::get()->get("notify.wav")); OnScreenHelp::init(); globalInputs::init(); } diff --git a/source/game.cpp b/source/game.cpp index 4f40b5b..b9ce499 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -351,7 +351,7 @@ void Game::updateStage() createMessage(paths, Resource::get()->getTexture("last_stage")); else { - auto text = std::make_unique(Resource::get()->getTexture("04b_25.png"), Resource::get()->getTextFile("04b_25.txt")); + auto text = Resource::get()->getText("04b_25"); const std::string caption = std::to_string(10 - current_stage_) + lang::getText(38); createMessage(paths, text->writeToTexture(caption, 2, -2)); } diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index f712fcc..d974281 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -27,7 +27,7 @@ HiScoreTable::HiScoreTable() backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), fade_(std::make_unique()), background_(std::make_unique()), - text_(std::make_unique(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), + text_(Resource::get()->getText("smb2")), counter_(0), ticks_(0), view_area_({0, 0, param.game.width, param.game.height}), diff --git a/source/hiscore_table.h b/source/hiscore_table.h index ebf97e5..9ef1a22 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -33,7 +33,7 @@ private: std::unique_ptr fade_; // Objeto para renderizar fades std::unique_ptr background_; // Objeto para dibujar el fondo del juego - std::unique_ptr text_; // Objeto para escribir texto + std::shared_ptr text_; // Objeto para escribir texto // Variables Uint16 counter_; // Contador diff --git a/source/instructions.cpp b/source/instructions.cpp index 594a3d9..8375744 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -26,7 +26,7 @@ Instructions::Instructions() : renderer_(Screen::get()->getRenderer()), texture_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), - text_(std::make_unique(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), + text_(Resource::get()->getText("smb2")), tiled_bg_(std::make_unique((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::STATIC)), fade_(std::make_unique()) { diff --git a/source/instructions.h b/source/instructions.h index 2a727db..5ca2d5e 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -35,7 +35,7 @@ private: std::vector> item_textures_; // Vector con las texturas de los items std::vector> sprites_; // Vector con los sprites de los items - std::unique_ptr text_; // Objeto para escribir texto + std::shared_ptr text_; // Objeto para escribir texto std::unique_ptr tiled_bg_; // Objeto para dibujar el mosaico animado de fondo std::unique_ptr fade_; // Objeto para renderizar fades diff --git a/source/intro.cpp b/source/intro.cpp index bd6a5e0..02e4ca5 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -20,7 +20,7 @@ // Constructor Intro::Intro() : texture_(Resource::get()->getTexture("intro.png")), - text_(std::make_shared(Resource::get()->getTexture("nokia.png"), Resource::get()->getTextFile("nokia.txt"))) + text_(Resource::get()->getText("nokia")) { // Inicializa variables diff --git a/source/on_screen_help.cpp b/source/on_screen_help.cpp index bb9b29c..2f82d2f 100644 --- a/source/on_screen_help.cpp +++ b/source/on_screen_help.cpp @@ -94,7 +94,7 @@ void OnScreenHelp::fillTexture() SDL_SetRenderTarget(Screen::get()->getRenderer(), texture); // Crea el objeto para el texto - auto text = std::make_unique(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); + auto text = Resource::get()->getText("8bithud"); // Crea la textura con los gráficos auto controllersTexture = Resource::get()->getTexture("controllers.png"); @@ -169,7 +169,7 @@ void OnScreenHelp::toggleState() // Calcula la longitud en pixels del texto más largo auto OnScreenHelp::getLargestStringSize() -> int const { - auto text = std::make_unique(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); + auto text = Resource::get()->getText("8bithud"); auto size = 0; for (int i = 107; i <= 113; ++i) diff --git a/source/resource.cpp b/source/resource.cpp index 6bec58b..1c0b1aa 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -42,6 +42,7 @@ Resource::Resource() loadAnimations(); loadDemoData(); addPalettes(); + createText(); createTextures(); std::cout << "\n** RESOURCES LOADED" << std::endl; } @@ -106,6 +107,21 @@ std::shared_ptr Resource::getTextFile(const std::string &name) throw std::runtime_error("TextFile no encontrado: " + name); } +// Obtiene el objeto de texto a partir de un nombre +std::shared_ptr Resource::getText(const std::string &name) +{ + auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t) + { return t.name == name; }); + + if (it != texts_.end()) + { + return it->text; + } + + std::cerr << "Error: Text no encontrado " << name << std::endl; + throw std::runtime_error("Text no encontrado: " + name); +} + // Obtiene la animación a partir de un nombre AnimationsFileBuffer &Resource::getAnimation(const std::string &name) { @@ -239,7 +255,7 @@ void Resource::createTextures() }; std::cout << "\n>> CREATING TEXTURES" << std::endl; - auto text = std::make_unique(getTexture("04b_25.png"), getTextFile("04b_25.txt")); + auto text = getText("04b_25"); // Tamaño normal std::vector strings = { @@ -270,3 +286,21 @@ void Resource::createTextures() printWithDots("Texture : ", s.name, "[ DONE ]"); } } + +// Crea los objetos de texto +void Resource::createText() +{ + std::cout << "\n>> CREATING TEXT_OBJECTS" << std::endl; + + std::vector> resources = { + {"04b_25", "04b_25.png"}, + {"8bithud", "8bithud.png"}, + {"nokia", "nokia.png"}, + {"smb2", "smb2.gif"}}; + + for (const auto &resource : resources) + { + texts_.emplace_back(ResourceText(resource.first, std::make_shared(getTexture(resource.second), getTextFile(resource.first + ".txt")))); + printWithDots("Text : ", resource.first, "[ DONE ]"); + } +} diff --git a/source/resource.h b/source/resource.h index 9e7fad6..14d76fd 100644 --- a/source/resource.h +++ b/source/resource.h @@ -54,6 +54,17 @@ struct ResourceTextFile : name(name), text_file(text_file) {} }; +// Estructura para almacenar objetos Text y su nombre +struct ResourceText +{ + std::string name; // Nombre del objeto + std::shared_ptr text; // Objeto + + // Constructor + ResourceText(const std::string &name, std::shared_ptr text) + : name(name), text(text) {} +}; + // Estructura para almacenar ficheros animaciones y su nombre struct ResourceAnimation { @@ -75,6 +86,7 @@ private: std::vector musics_; // Vector con las musicas std::vector textures_; // Vector con las musicas std::vector text_files_; // Vector con los ficheros de texto + std::vector texts_; // Vector con los objetos de texto std::vector animations_; // Vector con las animaciones std::vector demos_; // Vector con los ficheros de datos para el modo demostración @@ -102,6 +114,9 @@ private: // Crea texturas void createTextures(); + // Crea los objetos de texto + void createText(); + // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera // Constructor @@ -132,6 +147,9 @@ public: // Obtiene el fichero de texto a partir de un nombre std::shared_ptr getTextFile(const std::string &name); + // Obtiene el objeto de texto a partir de un nombre + std::shared_ptr getText(const std::string &name); + // Obtiene la animación a partir de un nombre AnimationsFileBuffer &getAnimation(const std::string &name); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index e49b925..85d408e 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -37,7 +37,7 @@ Scoreboard::Scoreboard() : renderer_(Screen::get()->getRenderer()), game_power_meter_texture_(Resource::get()->getTexture("game_power_meter.png")), power_meter_sprite_(std::make_unique(game_power_meter_texture_)), - text_scoreboard_(std::make_unique(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"))) + text_scoreboard_(Resource::get()->getText("8bithud")) { // Inicializa variables for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) diff --git a/source/scoreboard.h b/source/scoreboard.h index 873cb46..2213c1f 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -52,7 +52,7 @@ private: std::shared_ptr game_power_meter_texture_; // Textura con el marcador de poder de la fase std::unique_ptr power_meter_sprite_; // Sprite para el medidor de poder de la fase - std::unique_ptr text_scoreboard_; // Fuente para el marcador del juego + std::shared_ptr text_scoreboard_; // Fuente para el marcador del juego SDL_Texture *background_ = nullptr; // Textura para dibujar el marcador std::vector panel_texture_; // Texturas para dibujar cada panel diff --git a/source/title.cpp b/source/title.cpp index 5655409..b8c1360 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -28,14 +28,13 @@ // Constructor Title::Title() - : text1_(std::make_unique(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), - text2_(std::make_unique(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"))), + : text_(Resource::get()->getText("smb2")), fade_(std::make_unique()), tiled_bg_(std::make_unique((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::RANDOM)), game_logo_(std::make_unique(param.game.game_area.center_x, param.title.title_c_c_position)), mini_logo_texture_(Resource::get()->getTexture("logo_jailgames_mini.png")), mini_logo_sprite_(std::make_unique(mini_logo_texture_, param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight())), - define_buttons_(std::make_unique(std::move(text2_))), + define_buttons_(std::make_unique()), num_controllers_(Input::get()->getNumControllers()) { // Configura objetos @@ -146,7 +145,7 @@ void Title::render() // 'PRESS TO PLAY' if (counter_ % 50 > 14 && !define_buttons_->isEnabled()) { - text1_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, param.title.press_start_position, lang::getText(23), 1, no_color, 1, shadow); + text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, param.title.press_start_position, lang::getText(23), 1, no_color, 1, shadow); } // Mini logo @@ -156,7 +155,7 @@ void Title::render() mini_logo_sprite_->render(); // Texto con el copyright - text1_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, pos2, TEXT_COPYRIGHT, 1, no_color, 1, shadow); + text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, pos2, TEXT_COPYRIGHT, 1, no_color, 1, shadow); } // Define Buttons diff --git a/source/title.h b/source/title.h index 7d56b0c..6e0f14e 100644 --- a/source/title.h +++ b/source/title.h @@ -40,8 +40,7 @@ class Title { private: // Objetos y punteros - std::unique_ptr text1_; // Objeto de texto para poder escribir textos en pantalla - std::unique_ptr text2_; // Objeto de texto para poder escribir textos en pantalla + std::shared_ptr text_; // Objeto de texto para poder escribir textos en pantalla std::unique_ptr fade_; // Objeto para realizar fundidos en pantalla std::unique_ptr tiled_bg_; // Objeto para dibujar el mosaico animado de fondo std::unique_ptr game_logo_; // Objeto para dibujar el logo con el título del juego