From 52a0c2b91f4202e0af194686269f17c13b33d6b8 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 25 Jan 2025 21:17:45 +0100 Subject: [PATCH] Treballant en la nova tabla de records: ja pinta amb sprites --- source/director.cpp | 2 +- source/hiscore_table.cpp | 59 ++++++++++++++++++++++++++-------------- source/hiscore_table.h | 13 +++++++-- source/text.cpp | 23 ++++++++++++++-- source/text.h | 3 ++ 5 files changed, 74 insertions(+), 26 deletions(-) diff --git a/source/director.cpp b/source/director.cpp index 808d55a..a55d398 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -52,7 +52,7 @@ Director::Director(int argc, const char *argv[]) section::name = section::Name::GAME; section::options = section::Options::GAME_PLAY_1P; #elif DEBUG - section::name = section::Name::LOGO; + section::name = section::Name::HI_SCORE_TABLE; #else // NORMAL GAME section::name = section::Name::LOGO; section::attract_mode = section::AttractMode::TITLE_TO_DEMO; diff --git a/source/hiscore_table.cpp b/source/hiscore_table.cpp index b0f700b..3c5fee4 100644 --- a/source/hiscore_table.cpp +++ b/source/hiscore_table.cpp @@ -51,6 +51,9 @@ HiScoreTable::HiScoreTable() fade_->setMode(fade_mode_); fade_->activate(); + // Crea los sprites con los textos + createSprites(); + // Crea el contenido de la textura con la lista de puntuaciones fillTexture(); } @@ -108,13 +111,6 @@ void HiScoreTable::update() // Crea el contenido de la textura con la lista de puntuaciones void HiScoreTable::fillTexture() { - // hay 27 letras - 7 de puntos quedan 20 caracteres 20 - name_lenght 0 num_dots - constexpr auto max_names = 10; - constexpr auto space_between_header = 32; - const auto space_between_lines = text_->getCharacterSize() * 2.0f; - const auto size = space_between_header + space_between_lines * (max_names - 1) + text_->getCharacterSize(); - const auto first_line = (param.game.height - size) / 2; - // Pinta en el backbuffer el texto y los sprites auto temp = SDL_GetRenderTarget(renderer_); SDL_SetRenderTarget(renderer_, backbuffer_); @@ -122,22 +118,12 @@ void HiScoreTable::fillTexture() SDL_RenderClear(renderer_); // Escribe el texto: Mejores puntuaciones - text_->writeDX(TEXT_CENTER | TEXT_COLOR | TEXT_SHADOW, param.game.game_area.center_x, first_line, lang::getText(42), 1, orange_color, 1, shdw_txt_color); + header_->render(); // Escribe los nombres de la tabla de puntuaciones - for (int i = 0; i < max_names; ++i) + for (auto const &entry : entry_names_) { - const auto name_lenght = options.game.hi_score_table[i].name.length(); - const auto score = format(options.game.hi_score_table[i].score); - const auto score_lenght = score.size(); - const auto num_dots = 25 - name_lenght - score_lenght; - std::string dots; - for (int j = 0; j < (int)num_dots; ++j) - { - dots = dots + "."; - } - const auto line = options.game.hi_score_table[i].name + dots + score; - text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, (i * space_between_lines) + first_line + space_between_header, line, 1, orange_color, 1, shdw_txt_color); + entry->render(); } // Cambia el destino de renderizado @@ -274,4 +260,37 @@ std::string HiScoreTable::format(int number) } return result; +} + +// Crea los sprites con los textos +void HiScoreTable::createSprites() +{ + // Hay 27 letras - 7 de puntos quedan 20 caracteres 20 - name_lenght 0 num_dots + constexpr int max_names = 10; + constexpr int space_between_header = 32; + const int space_between_lines = text_->getCharacterSize() * 2; + const int size = space_between_header + space_between_lines * (max_names - 1) + text_->getCharacterSize(); + const int first_line = (param.game.height - size) / 2; + + // Crea el sprite para el texto de cabecera + header_ = std::make_unique(text_->writeDXToTexture(TEXT_COLOR | TEXT_SHADOW, lang::getText(42), 1, orange_color, 1, shdw_txt_color)); + header_->setPosition(param.game.game_area.center_x - (header_->getWidth() / 2), first_line); + + // Crea los sprites para las entradas en la tabla de puntuaciones + for (int i = 0; i < max_names; ++i) + { + const auto name_lenght = options.game.hi_score_table.at(i).name.length(); + const auto score = format(options.game.hi_score_table.at(i).score); + const auto score_lenght = score.size(); + const auto num_dots = 25 - name_lenght - score_lenght; + std::string dots; + for (int j = 0; j < (int)num_dots; ++j) + { + dots = dots + "."; + } + const auto line = options.game.hi_score_table.at(i).name + dots + score; + + entry_names_.emplace_back(std::make_shared(text_->writeDXToTexture(TEXT_SHADOW, line, 1, orange_color, 1, shdw_txt_color))); + entry_names_.back()->setPosition(param.game.game_area.center_x - (entry_names_.back()->getWidth() / 2), (i * space_between_lines) + first_line + space_between_header); + } } \ No newline at end of file diff --git a/source/hiscore_table.h b/source/hiscore_table.h index 9ef1a22..1f402f3 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -5,6 +5,8 @@ #include // para Uint16, Uint32, Uint8 #include // para unique_ptr #include // para string +#include "sprite.h" +#include "path_sprite.h" class Background; // lines 8-8 class Fade; // lines 9-9 class Text; // lines 10-10 @@ -31,9 +33,11 @@ private: SDL_Renderer *renderer_; // El renderizador de la ventana SDL_Texture *backbuffer_; // Textura para usar como backbuffer - std::unique_ptr fade_; // Objeto para renderizar fades - std::unique_ptr background_; // Objeto para dibujar el fondo del juego - std::shared_ptr text_; // Objeto para escribir texto + std::unique_ptr fade_; // Objeto para renderizar fades + std::unique_ptr background_; // Objeto para dibujar el fondo del juego + std::shared_ptr text_; // Objeto para escribir texto + std::unique_ptr header_; // Sprite con la cabecera del texto + std::vector> entry_names_; // Lista con los spritres de cada uno de los nombres de la tabla de records // Variables Uint16 counter_; // Contador @@ -65,6 +69,9 @@ private: // Gestiona el fade void updateFade(); + // Crea los sprites con los textos + void createSprites(); + public: // Constructor HiScoreTable(); diff --git a/source/text.cpp b/source/text.cpp index 3caa14c..3bf499d 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -173,6 +173,25 @@ std::shared_ptr Text::writeToTexture(const std::string &text, int zoom, return texture; } +// Escribe el texto con extras en una textura +std::shared_ptr Text::writeDXToTexture(Uint8 flags, const std::string &text, int kerning, Color textColor, Uint8 shadow_distance, Color shadow_color, int lenght) +{ + auto renderer = Screen::get()->getRenderer(); + auto texture = std::make_shared(renderer); + auto width = Text::lenght(text, kerning); + auto height = box_height_ + shadow_distance; + auto temp = SDL_GetRenderTarget(renderer); + texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); + texture->setBlendMode(SDL_BLENDMODE_BLEND); + texture->setAsRenderTarget(renderer); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_RenderClear(renderer); + writeDX(flags, 0, 0, text, kerning, textColor, shadow_distance, shadow_color, lenght); + SDL_SetRenderTarget(renderer, temp); + + return texture; +} + // Escribe el texto con colores void Text::writeColored(int x, int y, const std::string &text, Color color, int kerning, int lenght) { @@ -272,7 +291,7 @@ void Text::setFixedWidth(bool value) void Text::setPalette(int number) { auto temp = SDL_GetRenderTarget(Screen::get()->getRenderer()); - SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr); + SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr); sprite_->getTexture()->setPalette(number); - SDL_SetRenderTarget(Screen::get()->getRenderer(), temp); + SDL_SetRenderTarget(Screen::get()->getRenderer(), temp); } \ No newline at end of file diff --git a/source/text.h b/source/text.h index baba6f1..3ec7b97 100644 --- a/source/text.h +++ b/source/text.h @@ -55,6 +55,9 @@ public: // Escribe el texto en una textura std::shared_ptr writeToTexture(const std::string &text, int zoom = 1, int kerning = 1); + // Escribe el texto con extras en una textura + std::shared_ptr writeDXToTexture(Uint8 flags, const std::string &text, int kerning = 1, Color textColor = Color(), Uint8 shadow_distance = 1, Color shadow_color = Color(), int lenght = -1); + // Escribe el texto con colores void writeColored(int x, int y, const std::string &text, Color color, int kerning = 1, int lenght = -1);