Els objectes de Text es precarreguen al inici

This commit is contained in:
2024-11-04 19:08:18 +01:00
parent e0e82ee273
commit 30735f00e8
16 changed files with 80 additions and 30 deletions

View File

@@ -4,13 +4,14 @@
#include "lang.h" // Para getText #include "lang.h" // Para getText
#include "options.h" // Para OptionsController, Options, options #include "options.h" // Para OptionsController, Options, options
#include "param.h" // Para Param, param, ParamGame, ParamTitle #include "param.h" // Para Param, param, ParamGame, ParamTitle
#include "resource.h" // Para Resource
#include "section.h" // Para Name, Options, name, options #include "section.h" // Para Name, Options, name, options
#include "text.h" // Para Text #include "text.h" // Para Text
// Constructor // Constructor
DefineButtons::DefineButtons(std::unique_ptr<Text> text_) DefineButtons::DefineButtons()
: input_(Input::get()), : input_(Input::get()),
text_(std::move(text_)) text_(Resource::get()->getText("8bithud"))
{ {
// Inicializa variables // Inicializa variables
x_ = param.game.width / 2; x_ = param.game.width / 2;

View File

@@ -58,7 +58,7 @@ private:
public: public:
// Constructor // Constructor
explicit DefineButtons(std::unique_ptr<Text> text); DefineButtons();
// Destructor // Destructor
~DefineButtons() = default; ~DefineButtons() = default;

View File

@@ -118,8 +118,7 @@ Director::Director(int argc, const char *argv[])
Resource::init(); Resource::init();
Input::init(Asset::get()->get("gamecontrollerdb.txt")); Input::init(Asset::get()->get("gamecontrollerdb.txt"));
bindInputs(); bindInputs();
auto notifier_text = std::make_shared<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); Notifier::init(std::string(), Resource::get()->getText("8bithud"), Asset::get()->get("notify.wav"));
Notifier::init(std::string(), notifier_text, Asset::get()->get("notify.wav"));
OnScreenHelp::init(); OnScreenHelp::init();
globalInputs::init(); globalInputs::init();
} }

View File

@@ -351,7 +351,7 @@ void Game::updateStage()
createMessage(paths, Resource::get()->getTexture("last_stage")); createMessage(paths, Resource::get()->getTexture("last_stage"));
else else
{ {
auto text = std::make_unique<Text>(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); const std::string caption = std::to_string(10 - current_stage_) + lang::getText(38);
createMessage(paths, text->writeToTexture(caption, 2, -2)); createMessage(paths, text->writeToTexture(caption, 2, -2));
} }

View File

@@ -27,7 +27,7 @@ HiScoreTable::HiScoreTable()
backbuffer_(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)),
fade_(std::make_unique<Fade>()), fade_(std::make_unique<Fade>()),
background_(std::make_unique<Background>()), background_(std::make_unique<Background>()),
text_(std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), text_(Resource::get()->getText("smb2")),
counter_(0), counter_(0),
ticks_(0), ticks_(0),
view_area_({0, 0, param.game.width, param.game.height}), view_area_({0, 0, param.game.width, param.game.height}),

View File

@@ -33,7 +33,7 @@ private:
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego
std::unique_ptr<Text> text_; // Objeto para escribir texto std::shared_ptr<Text> text_; // Objeto para escribir texto
// Variables // Variables
Uint16 counter_; // Contador Uint16 counter_; // Contador

View File

@@ -26,7 +26,7 @@ Instructions::Instructions()
: renderer_(Screen::get()->getRenderer()), : renderer_(Screen::get()->getRenderer()),
texture_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), 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)), backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)),
text_(std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), text_(Resource::get()->getText("smb2")),
tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::STATIC)), tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::STATIC)),
fade_(std::make_unique<Fade>()) fade_(std::make_unique<Fade>())
{ {

View File

@@ -35,7 +35,7 @@ private:
std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items
std::vector<std::unique_ptr<Sprite>> sprites_; // Vector con los sprites de los items std::vector<std::unique_ptr<Sprite>> sprites_; // Vector con los sprites de los items
std::unique_ptr<Text> text_; // Objeto para escribir texto std::shared_ptr<Text> text_; // Objeto para escribir texto
std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades std::unique_ptr<Fade> fade_; // Objeto para renderizar fades

View File

@@ -20,7 +20,7 @@
// Constructor // Constructor
Intro::Intro() Intro::Intro()
: texture_(Resource::get()->getTexture("intro.png")), : texture_(Resource::get()->getTexture("intro.png")),
text_(std::make_shared<Text>(Resource::get()->getTexture("nokia.png"), Resource::get()->getTextFile("nokia.txt"))) text_(Resource::get()->getText("nokia"))
{ {
// Inicializa variables // Inicializa variables

View File

@@ -94,7 +94,7 @@ void OnScreenHelp::fillTexture()
SDL_SetRenderTarget(Screen::get()->getRenderer(), texture); SDL_SetRenderTarget(Screen::get()->getRenderer(), texture);
// Crea el objeto para el texto // Crea el objeto para el texto
auto text = std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); auto text = Resource::get()->getText("8bithud");
// Crea la textura con los gráficos // Crea la textura con los gráficos
auto controllersTexture = Resource::get()->getTexture("controllers.png"); auto controllersTexture = Resource::get()->getTexture("controllers.png");
@@ -169,7 +169,7 @@ void OnScreenHelp::toggleState()
// Calcula la longitud en pixels del texto más largo // Calcula la longitud en pixels del texto más largo
auto OnScreenHelp::getLargestStringSize() -> int const auto OnScreenHelp::getLargestStringSize() -> int const
{ {
auto text = std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); auto text = Resource::get()->getText("8bithud");
auto size = 0; auto size = 0;
for (int i = 107; i <= 113; ++i) for (int i = 107; i <= 113; ++i)

View File

@@ -42,6 +42,7 @@ Resource::Resource()
loadAnimations(); loadAnimations();
loadDemoData(); loadDemoData();
addPalettes(); addPalettes();
createText();
createTextures(); createTextures();
std::cout << "\n** RESOURCES LOADED" << std::endl; std::cout << "\n** RESOURCES LOADED" << std::endl;
} }
@@ -106,6 +107,21 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
throw std::runtime_error("TextFile no encontrado: " + name); throw std::runtime_error("TextFile no encontrado: " + name);
} }
// Obtiene el objeto de texto a partir de un nombre
std::shared_ptr<Text> 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 // Obtiene la animación a partir de un nombre
AnimationsFileBuffer &Resource::getAnimation(const std::string &name) AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
{ {
@@ -239,7 +255,7 @@ void Resource::createTextures()
}; };
std::cout << "\n>> CREATING TEXTURES" << std::endl; std::cout << "\n>> CREATING TEXTURES" << std::endl;
auto text = std::make_unique<Text>(getTexture("04b_25.png"), getTextFile("04b_25.txt")); auto text = getText("04b_25");
// Tamaño normal // Tamaño normal
std::vector<NameAndText> strings = { std::vector<NameAndText> strings = {
@@ -270,3 +286,21 @@ void Resource::createTextures()
printWithDots("Texture : ", s.name, "[ DONE ]"); printWithDots("Texture : ", s.name, "[ DONE ]");
} }
} }
// Crea los objetos de texto
void Resource::createText()
{
std::cout << "\n>> CREATING TEXT_OBJECTS" << std::endl;
std::vector<std::pair<std::string, std::string>> 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<Text>(getTexture(resource.second), getTextFile(resource.first + ".txt"))));
printWithDots("Text : ", resource.first, "[ DONE ]");
}
}

View File

@@ -54,6 +54,17 @@ struct ResourceTextFile
: name(name), text_file(text_file) {} : 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> text; // Objeto
// Constructor
ResourceText(const std::string &name, std::shared_ptr<Text> text)
: name(name), text(text) {}
};
// Estructura para almacenar ficheros animaciones y su nombre // Estructura para almacenar ficheros animaciones y su nombre
struct ResourceAnimation struct ResourceAnimation
{ {
@@ -75,6 +86,7 @@ private:
std::vector<ResourceMusic> musics_; // Vector con las musicas std::vector<ResourceMusic> musics_; // Vector con las musicas
std::vector<ResourceTexture> textures_; // Vector con las musicas std::vector<ResourceTexture> textures_; // Vector con las musicas
std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto
std::vector<ResourceText> texts_; // Vector con los objetos de texto
std::vector<ResourceAnimation> animations_; // Vector con las animaciones std::vector<ResourceAnimation> animations_; // Vector con las animaciones
std::vector<DemoData> demos_; // Vector con los ficheros de datos para el modo demostración std::vector<DemoData> demos_; // Vector con los ficheros de datos para el modo demostración
@@ -102,6 +114,9 @@ private:
// Crea texturas // Crea texturas
void createTextures(); 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 // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera
// Constructor // Constructor
@@ -132,6 +147,9 @@ public:
// Obtiene el fichero de texto a partir de un nombre // Obtiene el fichero de texto a partir de un nombre
std::shared_ptr<TextFile> getTextFile(const std::string &name); std::shared_ptr<TextFile> getTextFile(const std::string &name);
// Obtiene el objeto de texto a partir de un nombre
std::shared_ptr<Text> getText(const std::string &name);
// Obtiene la animación a partir de un nombre // Obtiene la animación a partir de un nombre
AnimationsFileBuffer &getAnimation(const std::string &name); AnimationsFileBuffer &getAnimation(const std::string &name);

View File

@@ -37,7 +37,7 @@ Scoreboard::Scoreboard()
: renderer_(Screen::get()->getRenderer()), : renderer_(Screen::get()->getRenderer()),
game_power_meter_texture_(Resource::get()->getTexture("game_power_meter.png")), game_power_meter_texture_(Resource::get()->getTexture("game_power_meter.png")),
power_meter_sprite_(std::make_unique<Sprite>(game_power_meter_texture_)), power_meter_sprite_(std::make_unique<Sprite>(game_power_meter_texture_)),
text_scoreboard_(std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"))) text_scoreboard_(Resource::get()->getText("8bithud"))
{ {
// Inicializa variables // Inicializa variables
for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i)

View File

@@ -52,7 +52,7 @@ private:
std::shared_ptr<Texture> game_power_meter_texture_; // Textura con el marcador de poder de la fase std::shared_ptr<Texture> game_power_meter_texture_; // Textura con el marcador de poder de la fase
std::unique_ptr<Sprite> power_meter_sprite_; // Sprite para el medidor de poder de la fase std::unique_ptr<Sprite> power_meter_sprite_; // Sprite para el medidor de poder de la fase
std::unique_ptr<Text> text_scoreboard_; // Fuente para el marcador del juego std::shared_ptr<Text> text_scoreboard_; // Fuente para el marcador del juego
SDL_Texture *background_ = nullptr; // Textura para dibujar el marcador SDL_Texture *background_ = nullptr; // Textura para dibujar el marcador
std::vector<SDL_Texture *> panel_texture_; // Texturas para dibujar cada panel std::vector<SDL_Texture *> panel_texture_; // Texturas para dibujar cada panel

View File

@@ -28,14 +28,13 @@
// Constructor // Constructor
Title::Title() Title::Title()
: text1_(std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), : text_(Resource::get()->getText("smb2")),
text2_(std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"))),
fade_(std::make_unique<Fade>()), fade_(std::make_unique<Fade>()),
tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::RANDOM)), tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::RANDOM)),
game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)), game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)),
mini_logo_texture_(Resource::get()->getTexture("logo_jailgames_mini.png")), mini_logo_texture_(Resource::get()->getTexture("logo_jailgames_mini.png")),
mini_logo_sprite_(std::make_unique<Sprite>(mini_logo_texture_, param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight())), mini_logo_sprite_(std::make_unique<Sprite>(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<DefineButtons>(std::move(text2_))), define_buttons_(std::make_unique<DefineButtons>()),
num_controllers_(Input::get()->getNumControllers()) num_controllers_(Input::get()->getNumControllers())
{ {
// Configura objetos // Configura objetos
@@ -146,7 +145,7 @@ void Title::render()
// 'PRESS TO PLAY' // 'PRESS TO PLAY'
if (counter_ % 50 > 14 && !define_buttons_->isEnabled()) 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 // Mini logo
@@ -156,7 +155,7 @@ void Title::render()
mini_logo_sprite_->render(); mini_logo_sprite_->render();
// Texto con el copyright // 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 // Define Buttons

View File

@@ -40,8 +40,7 @@ class Title
{ {
private: private:
// Objetos y punteros // Objetos y punteros
std::unique_ptr<Text> text1_; // Objeto de texto para poder escribir textos en pantalla std::shared_ptr<Text> text_; // Objeto de texto para poder escribir textos en pantalla
std::unique_ptr<Text> text2_; // Objeto de texto para poder escribir textos en pantalla
std::unique_ptr<Fade> fade_; // Objeto para realizar fundidos en pantalla std::unique_ptr<Fade> fade_; // Objeto para realizar fundidos en pantalla
std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo
std::unique_ptr<GameLogo> game_logo_; // Objeto para dibujar el logo con el título del juego std::unique_ptr<GameLogo> game_logo_; // Objeto para dibujar el logo con el título del juego