Text: revisada la classe
window_message: correcions
This commit is contained in:
394
source/text.cpp
394
source/text.cpp
@@ -13,9 +13,208 @@
|
||||
#include "texture.h" // Para Texture
|
||||
#include "utils.h"
|
||||
|
||||
// Constructor
|
||||
Text::Text(std::shared_ptr<Texture> texture, const std::string &text_file) {
|
||||
// Carga los offsets desde el fichero
|
||||
auto tf = loadFile(text_file);
|
||||
|
||||
// Inicializa variables desde la estructura
|
||||
box_height_ = tf->box_height;
|
||||
box_width_ = tf->box_width;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
offset_[i].x = tf->offset[i].x;
|
||||
offset_[i].y = tf->offset[i].y;
|
||||
offset_[i].w = tf->offset[i].w;
|
||||
}
|
||||
|
||||
// Crea los objetos
|
||||
sprite_ = std::make_unique<Sprite>(texture, (SDL_FRect){0, 0, static_cast<float>(box_width_), static_cast<float>(box_height_)});
|
||||
|
||||
// Inicializa variables
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(std::shared_ptr<Texture> texture, std::shared_ptr<Text::File> text_file) {
|
||||
// Inicializa variables desde la estructura
|
||||
box_height_ = text_file->box_height;
|
||||
box_width_ = text_file->box_width;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
offset_[i].x = text_file->offset[i].x;
|
||||
offset_[i].y = text_file->offset[i].y;
|
||||
offset_[i].w = text_file->offset[i].w;
|
||||
}
|
||||
|
||||
// Crea los objetos
|
||||
sprite_ = std::make_unique<Sprite>(texture, (SDL_FRect){0, 0, static_cast<float>(box_width_), static_cast<float>(box_height_)});
|
||||
|
||||
// Inicializa variables
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Escribe texto en pantalla
|
||||
void Text::write(int x, int y, const std::string &text, int kerning, int length) {
|
||||
int shift = 0;
|
||||
const std::string_view visible_text = (length == -1) ? std::string_view(text) : std::string_view(text).substr(0, length);
|
||||
|
||||
sprite_->setY(y);
|
||||
for (const auto ch : visible_text) {
|
||||
const auto index = static_cast<unsigned char>(ch);
|
||||
|
||||
if (index < offset_.size()) {
|
||||
sprite_->setSpriteClip(offset_[index].x, offset_[index].y, box_width_, box_height_);
|
||||
sprite_->setX(x + shift);
|
||||
sprite_->render();
|
||||
shift += offset_[index].w + kerning;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe el texto al doble de tamaño
|
||||
void Text::write2X(int x, int y, const std::string &text, int kerning, int length) {
|
||||
int shift = 0;
|
||||
const std::string_view visible_text = (length == -1) ? std::string_view(text) : std::string_view(text).substr(0, length);
|
||||
|
||||
for (const auto ch : visible_text) {
|
||||
const auto index = static_cast<unsigned char>(ch);
|
||||
|
||||
if (index < offset_.size()) {
|
||||
SDL_FRect rect = {
|
||||
static_cast<float>(offset_[index].x),
|
||||
static_cast<float>(offset_[index].y),
|
||||
static_cast<float>(box_width_),
|
||||
static_cast<float>(box_height_)};
|
||||
|
||||
sprite_->getTexture()->render(x + shift, y, &rect, 2.0F, 2.0F);
|
||||
shift += (offset_[index].w + kerning) * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe el texto en una textura
|
||||
auto Text::writeToTexture(const std::string &text, int zoom, int kerning, int length) -> std::shared_ptr<Texture> {
|
||||
auto *renderer = Screen::get()->getRenderer();
|
||||
auto texture = std::make_shared<Texture>(renderer);
|
||||
auto width = Text::length(text, kerning) * zoom;
|
||||
auto height = box_height_ * zoom;
|
||||
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);
|
||||
zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning);
|
||||
SDL_SetRenderTarget(renderer, temp);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
// Escribe el texto con extras en una textura
|
||||
auto Text::writeDXToTexture(Uint8 flags, const std::string &text, int kerning, Color text_color, Uint8 shadow_distance, Color shadow_color, int length) -> std::shared_ptr<Texture> {
|
||||
auto *renderer = Screen::get()->getRenderer();
|
||||
auto texture = std::make_shared<Texture>(renderer);
|
||||
auto width = Text::length(text, kerning) + shadow_distance;
|
||||
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, text_color, shadow_distance, shadow_color, length);
|
||||
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 length) {
|
||||
sprite_->getTexture()->setColor(color.r, color.g, color.b);
|
||||
write(x, y, text, kerning, length);
|
||||
sprite_->getTexture()->setColor(255, 255, 255);
|
||||
}
|
||||
|
||||
// Escribe el texto con sombra
|
||||
void Text::writeShadowed(int x, int y, const std::string &text, Color color, Uint8 shadow_distance, int kerning, int length) {
|
||||
writeDX(Text::SHADOW, x, y, text, kerning, color, shadow_distance, color, length);
|
||||
write(x, y, text, kerning, length); // Dibuja el texto principal encima
|
||||
}
|
||||
|
||||
// Escribe el texto centrado en un punto x
|
||||
void Text::writeCentered(int x, int y, const std::string &text, int kerning, int length) {
|
||||
x -= (Text::length(text, kerning) / 2);
|
||||
write(x, y, text, kerning, length);
|
||||
}
|
||||
|
||||
// Escribe texto con extras
|
||||
void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning, Color text_color, Uint8 shadow_distance, Color shadow_color, int length) {
|
||||
const auto CENTERED = ((flags & Text::CENTER) == Text::CENTER);
|
||||
const auto SHADOWED = ((flags & Text::SHADOW) == Text::SHADOW);
|
||||
const auto COLORED = ((flags & Text::COLOR) == Text::COLOR);
|
||||
const auto STROKED = ((flags & Text::STROKE) == Text::STROKE);
|
||||
|
||||
if (CENTERED) {
|
||||
x -= (Text::length(text, kerning) / 2);
|
||||
}
|
||||
|
||||
if (SHADOWED) {
|
||||
writeColored(x + shadow_distance, y + shadow_distance, text, shadow_color, kerning, length);
|
||||
}
|
||||
|
||||
if (STROKED) {
|
||||
for (int dist = 1; dist <= shadow_distance; ++dist) {
|
||||
for (int dy = -dist; dy <= dist; ++dy) {
|
||||
for (int dx = -dist; dx <= dist; ++dx) {
|
||||
writeColored(x + dx, y + dy, text, shadow_color, kerning, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (COLORED) {
|
||||
writeColored(x, y, text, text_color, kerning, length);
|
||||
} else {
|
||||
write(x, y, text, kerning, length);
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe texto a partir de un TextStyle
|
||||
void Text::writeStyle(int x, int y, const std::string &text, const Style &style, int length) {
|
||||
writeDX(style.flags, x, y, text, style.kerning, style.text_color, style.shadow_distance, style.shadow_color);
|
||||
}
|
||||
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
auto Text::length(const std::string &text, int kerning) const -> int {
|
||||
int shift = 0;
|
||||
for (const auto &ch : text) {
|
||||
// Convertimos a unsigned char para obtener el valor ASCII correcto (0-255)
|
||||
const auto index = static_cast<unsigned char>(ch);
|
||||
|
||||
// Verificamos si el carácter está dentro de los límites del array
|
||||
if (index < offset_.size()) {
|
||||
shift += (offset_[index].w + kerning);
|
||||
}
|
||||
}
|
||||
|
||||
// Descuenta el kerning del último caracter si el texto no está vacío
|
||||
return text.empty() ? 0 : shift - kerning;
|
||||
}
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
auto Text::getCharacterSize() const -> int {
|
||||
return box_width_;
|
||||
}
|
||||
|
||||
// Establece si se usa un tamaño fijo de letra
|
||||
void Text::setFixedWidth(bool value) {
|
||||
fixed_width_ = value;
|
||||
}
|
||||
|
||||
// Llena una estructuta TextFile desde un fichero
|
||||
auto loadTextFile(const std::string &file_path) -> std::shared_ptr<TextFile> {
|
||||
auto tf = std::make_shared<TextFile>();
|
||||
auto Text::loadFile(const std::string &file_path) -> std::shared_ptr<Text::File> {
|
||||
auto tf = std::make_shared<Text::File>();
|
||||
|
||||
// Inicializa a cero el vector con las coordenadas
|
||||
for (auto &i : tf->offset) {
|
||||
@@ -73,195 +272,4 @@ auto loadTextFile(const std::string &file_path) -> std::shared_ptr<TextFile> {
|
||||
}
|
||||
|
||||
return tf;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(std::shared_ptr<Texture> texture, const std::string &text_file) {
|
||||
// Carga los offsets desde el fichero
|
||||
auto tf = loadTextFile(text_file);
|
||||
|
||||
// Inicializa variables desde la estructura
|
||||
box_height_ = tf->box_height;
|
||||
box_width_ = tf->box_width;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
offset_[i].x = tf->offset[i].x;
|
||||
offset_[i].y = tf->offset[i].y;
|
||||
offset_[i].w = tf->offset[i].w;
|
||||
}
|
||||
|
||||
// Crea los objetos
|
||||
sprite_ = std::make_unique<Sprite>(texture, (SDL_FRect){0, 0, static_cast<float>(box_width_), static_cast<float>(box_height_)});
|
||||
|
||||
// Inicializa variables
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Text::Text(std::shared_ptr<Texture> texture, std::shared_ptr<TextFile> text_file) {
|
||||
// Inicializa variables desde la estructura
|
||||
box_height_ = text_file->box_height;
|
||||
box_width_ = text_file->box_width;
|
||||
for (int i = 0; i < 128; ++i) {
|
||||
offset_[i].x = text_file->offset[i].x;
|
||||
offset_[i].y = text_file->offset[i].y;
|
||||
offset_[i].w = text_file->offset[i].w;
|
||||
}
|
||||
|
||||
// Crea los objetos
|
||||
sprite_ = std::make_unique<Sprite>(texture, (SDL_FRect){0, 0, static_cast<float>(box_width_), static_cast<float>(box_height_)});
|
||||
|
||||
// Inicializa variables
|
||||
fixed_width_ = false;
|
||||
}
|
||||
|
||||
// Escribe texto en pantalla
|
||||
void Text::write(int x, int y, const std::string &text, int kerning, int lenght) {
|
||||
int shift = 0;
|
||||
|
||||
if (lenght == -1) {
|
||||
lenght = text.length();
|
||||
}
|
||||
|
||||
sprite_->setY(y);
|
||||
for (int i = 0; i < lenght; ++i) {
|
||||
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 += offset_[static_cast<int>(text[i])].w + kerning;
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe texto en pantalla
|
||||
void Text::write2X(int x, int y, const std::string &text, int kerning) {
|
||||
int shift = 0;
|
||||
for (size_t i = 0; i < text.length(); ++i) {
|
||||
auto index = static_cast<size_t>(text[i]);
|
||||
SDL_FRect rect = {static_cast<float>(offset_[index].x), static_cast<float>(offset_[index].y), static_cast<float>(box_width_), static_cast<float>(box_height_)};
|
||||
sprite_->getTexture()->render(x + shift, y, &rect, 2.0F, 2.0F);
|
||||
shift += (offset_[index].w + kerning) * 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe el texto en una textura
|
||||
auto Text::writeToTexture(const std::string &text, int zoom, int kerning) -> std::shared_ptr<Texture> {
|
||||
auto *renderer = Screen::get()->getRenderer();
|
||||
auto texture = std::make_shared<Texture>(renderer);
|
||||
auto width = lenght(text, kerning) * zoom;
|
||||
auto height = box_height_ * zoom;
|
||||
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);
|
||||
zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning);
|
||||
SDL_SetRenderTarget(renderer, temp);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
// Escribe el texto con extras en una textura
|
||||
auto Text::writeDXToTexture(Uint8 flags, const std::string &text, int kerning, Color text_color, Uint8 shadow_distance, Color shadow_color, int lenght) -> std::shared_ptr<Texture> {
|
||||
auto *renderer = Screen::get()->getRenderer();
|
||||
auto texture = std::make_shared<Texture>(renderer);
|
||||
auto width = Text::lenght(text, kerning) + shadow_distance;
|
||||
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, text_color, 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) {
|
||||
sprite_->getTexture()->setColor(color.r, color.g, color.b);
|
||||
write(x, y, text, kerning, lenght);
|
||||
sprite_->getTexture()->setColor(255, 255, 255);
|
||||
}
|
||||
|
||||
// Escribe el texto con sombra
|
||||
void Text::writeShadowed(int x, int y, const std::string &text, Color color, Uint8 shadow_distance, int kerning, int lenght) {
|
||||
sprite_->getTexture()->setColor(color.r, color.g, color.b);
|
||||
write(x + shadow_distance, y + shadow_distance, text, kerning, lenght);
|
||||
sprite_->getTexture()->setColor(255, 255, 255);
|
||||
write(x, y, text, kerning, lenght);
|
||||
}
|
||||
|
||||
// Escribe el texto centrado en un punto x
|
||||
void Text::writeCentered(int x, int y, const std::string &text, int kerning, int lenght) {
|
||||
x -= (Text::lenght(text, kerning) / 2);
|
||||
write(x, y, text, kerning, lenght);
|
||||
}
|
||||
|
||||
// Escribe texto con extras
|
||||
void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning, Color text_color, Uint8 shadow_distance, Color shadow_color, int lenght) {
|
||||
const auto CENTERED = ((flags & TEXT_CENTER) == TEXT_CENTER);
|
||||
const auto SHADOWED = ((flags & TEXT_SHADOW) == TEXT_SHADOW);
|
||||
const auto COLORED = ((flags & TEXT_COLOR) == TEXT_COLOR);
|
||||
const auto STROKED = ((flags & TEXT_STROKE) == TEXT_STROKE);
|
||||
|
||||
if (CENTERED) {
|
||||
x -= (Text::lenght(text, kerning) / 2);
|
||||
}
|
||||
|
||||
if (SHADOWED) {
|
||||
writeColored(x + shadow_distance, y + shadow_distance, text, shadow_color, kerning, lenght);
|
||||
}
|
||||
|
||||
if (STROKED) {
|
||||
for (int dist = 1; dist <= shadow_distance; ++dist) {
|
||||
for (int dy = -dist; dy <= dist; ++dy) {
|
||||
for (int dx = -dist; dx <= dist; ++dx) {
|
||||
writeColored(x + dx, y + dy, text, shadow_color, kerning, lenght);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (COLORED) {
|
||||
writeColored(x, y, text, text_color, kerning, lenght);
|
||||
} else {
|
||||
write(x, y, text, kerning, lenght);
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe texto a partir de un TextStyle
|
||||
void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, const TextStyle &style, int length) {
|
||||
writeDX(flags, x, y, text, style.kerning, style.text_color, style.shadow_distance, style.shadow_color);
|
||||
}
|
||||
|
||||
// Obtiene la longitud en pixels de una cadena
|
||||
auto Text::lenght(const std::string &text, int kerning) const -> int {
|
||||
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;
|
||||
}
|
||||
|
||||
// Devuelve el valor de la variable
|
||||
auto Text::getCharacterSize() const -> int {
|
||||
return box_width_;
|
||||
}
|
||||
|
||||
// Establece si se usa un tamaño fijo de letra
|
||||
void Text::setFixedWidth(bool value) {
|
||||
fixed_width_ = value;
|
||||
}
|
||||
|
||||
// Establece una paleta
|
||||
void Text::setPalette(int number) {
|
||||
auto *temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
|
||||
SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr);
|
||||
sprite_->getTexture()->setPalette(number);
|
||||
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
|
||||
}
|
||||
Reference in New Issue
Block a user