#include "text.h" #include // for SDL_Rect #include // for basic_ostream, basic_ifstream, basic_istream #include // for cout #include "resource.h" // for Resource #include "sprite.h" // for Sprite #include "texture.h" // for Texture #include "utils.h" // for Color // Llena una estructuta TextFile desde un fichero std::shared_ptr loadTextFile(const std::string &file_path) { auto tf = std::make_shared(); // Inicializa a cero el vector con las coordenadas for (int i = 0; i < 128; ++i) { tf->offset[i].x = 0; tf->offset[i].y = 0; tf->offset[i].w = 0; tf->box_width = 0; tf->box_height = 0; } // Abre el fichero para leer los valores const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1).c_str(); std::ifstream file(file_path); if (file.is_open() && file.good()) { std::string buffer; // Lee los dos primeros valores del fichero std::getline(file, buffer); std::getline(file, buffer); tf->box_width = std::stoi(buffer); std::getline(file, buffer); std::getline(file, buffer); tf->box_height = std::stoi(buffer); // lee el resto de datos del fichero auto index = 32; auto line_read = 0; while (std::getline(file, buffer)) { // Almacena solo las lineas impares if (line_read % 2 == 1) { tf->offset[index++].w = std::stoi(buffer); } // Limpia el buffer buffer.clear(); line_read++; }; // Cierra el fichero printWithDots("Text File : ", file_name, "[ LOADED ]"); file.close(); } // El fichero no se puede abrir else { std::cerr << "Error: Fichero no encontrado " << file_path << std::endl; throw std::runtime_error("Fichero no encontrado: " + file_path); } // Establece las coordenadas para cada caracter ascii de la cadena y su ancho for (int i = 32; i < 128; ++i) { tf->offset[i].x = ((i - 32) % 15) * tf->box_width; tf->offset[i].y = ((i - 32) / 15) * tf->box_height; } return tf; } // Constructor Text::Text(std::shared_ptr 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(texture, (SDL_Rect){0, 0, box_width_, box_height_}); // Inicializa variables fixed_width_ = false; } // Constructor Text::Text(std::shared_ptr texture, std::shared_ptr 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(texture, (SDL_Rect){0, 0, box_width_, 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) { auto 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(text[i]); sprite_->setSpriteClip(offset_[index].x, offset_[index].y, width, height); sprite_->setX(x + shift); sprite_->render(); shift += fixed_width_ ? box_width_ : (offset_[int(text[i])].w + kerning); } } // 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 textColor, 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, textColor, kerning, lenght); } else { write(x, y, text, kerning, lenght); } } // 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); } // Descuenta el kerning del último caracter return shift - kerning; } // Devuelve el valor de la variable int Text::getCharacterSize() const { return box_width_; } // Recarga la textura void Text::reLoadTexture() { sprite_->getTexture()->reLoad(); } // Establece si se usa un tamaño fijo de letra void Text::setFixedWidth(bool value) { fixed_width_ = value; }