Afegida nova tipografia 04b_25

Eliminades tipografies que no s'utilitzaven
La classe Text ara pot tornar una textura amb el text
This commit is contained in:
2024-10-29 15:22:19 +01:00
parent 59e2865a4a
commit e2abf835f9
15 changed files with 157 additions and 540 deletions

View File

@@ -4,6 +4,7 @@
#include <iostream> // Para cerr
#include <stdexcept> // Para runtime_error
#include "sprite.h" // Para Sprite
#include "screen.h" // Para Screen
#include "texture.h" // Para Texture
#include "utils.h" // Para Color, getFileName, printWithDots
@@ -122,26 +123,35 @@ Text::Text(std::shared_ptr<Texture> texture, std::shared_ptr<TextFile> text_file
// Escribe texto en pantalla
void Text::write(int x, int y, const std::string &text, int kerning, int lenght)
{
auto shift = 0;
int shift = 0;
if (lenght == -1)
{
lenght = text.length();
}
sprite_->setY(y);
const auto width = sprite_->getWidth();
const auto height = sprite_->getHeight();
for (int i = 0; i < lenght; ++i)
{
const auto index = static_cast<int>(text[i]);
sprite_->setSpriteClip(offset_[index].x, offset_[index].y, width, height);
auto index = static_cast<int>(text[i]);
sprite_->setSpriteClip(offset_[index].x, offset_[index].y, box_width_, box_height_);
sprite_->setX(x + shift);
sprite_->render();
shift += fixed_width_ ? box_width_ : (offset_[int(text[i])].w + kerning);
shift += offset_[static_cast<int>(text[i])].w + kerning;
}
}
// Escribe el texto en una textura
std::shared_ptr<Texture> Text::writeToTexture(int x, int y, const std::string &text, int kerning)
{
auto renderer = Screen::get()->getRenderer();
auto texture = std::make_shared<Texture>(renderer);
auto width = lenght(text, kerning);
texture->createBlank(width, box_height_);
texture->setAsRenderTarget(renderer);
write(0, 0, text, kerning);
return texture;
}
// Escribe el texto con colores
void Text::writeColored(int x, int y, const std::string &text, Color color, int kerning, int lenght)
{
@@ -211,12 +221,9 @@ void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerni
// Obtiene la longitud en pixels de una cadena
int Text::lenght(const std::string &text, int kerning) const
{
auto shift = 0;
for (int i = 0; i < (int)text.length(); ++i)
{
shift += (offset_[int(text[i])].w + kerning);
}
int shift = 0;
for (size_t i = 0; i < text.length(); ++i)
shift += (offset_[static_cast<int>(text[i])].w + kerning);
// Descuenta el kerning del último caracter
return shift - kerning;