Treballant en la nova tabla de records: ja pinta amb sprites

This commit is contained in:
2025-01-25 21:17:45 +01:00
parent f12a456017
commit 52a0c2b91f
5 changed files with 74 additions and 26 deletions

View File

@@ -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;

View File

@@ -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<Sprite>(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<PathSprite>(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);
}
}

View File

@@ -5,6 +5,8 @@
#include <SDL2/SDL_stdinc.h> // para Uint16, Uint32, Uint8
#include <memory> // para unique_ptr
#include <string> // 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> fade_; // Objeto para renderizar fades
std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego
std::shared_ptr<Text> text_; // Objeto para escribir texto
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego
std::shared_ptr<Text> text_; // Objeto para escribir texto
std::unique_ptr<Sprite> header_; // Sprite con la cabecera del texto
std::vector<std::shared_ptr<PathSprite>> 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();

View File

@@ -173,6 +173,25 @@ std::shared_ptr<Texture> Text::writeToTexture(const std::string &text, int zoom,
return texture;
}
// Escribe el texto con extras en una textura
std::shared_ptr<Texture> 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<Texture>(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);
}

View File

@@ -55,6 +55,9 @@ public:
// Escribe el texto en una textura
std::shared_ptr<Texture> writeToTexture(const std::string &text, int zoom = 1, int kerning = 1);
// Escribe el texto con extras en una textura
std::shared_ptr<Texture> 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);