fix: en la migracio de la marquesina a la nova versio de Text s'havia posat per error un kerning superior al que havia
opt: millores en la getió de la marquesina per optimitzar rendiment
This commit is contained in:
@@ -274,6 +274,17 @@ auto Text::glyphWidth(uint32_t codepoint, int kerning) const -> int {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Devuelve el clip rect (región en el bitmap) de un glifo dado su codepoint
|
||||
auto Text::getGlyphClip(uint32_t codepoint) const -> SDL_FRect {
|
||||
auto it = offset_.find(codepoint);
|
||||
if (it == offset_.end()) { it = offset_.find('?'); }
|
||||
if (it == offset_.end()) { return {.x = 0.0F, .y = 0.0F, .w = 0.0F, .h = 0.0F}; }
|
||||
return {.x = static_cast<float>(it->second.x),
|
||||
.y = static_cast<float>(it->second.y),
|
||||
.w = static_cast<float>(box_width_),
|
||||
.h = static_cast<float>(box_height_)};
|
||||
}
|
||||
|
||||
// Devuelve el tamaño de la caja de cada caracter
|
||||
auto Text::getCharacterSize() const -> int {
|
||||
return box_width_;
|
||||
|
||||
@@ -51,6 +51,8 @@ class Text {
|
||||
[[nodiscard]] auto length(const std::string& text, int kerning = 1) const -> int; // Obtiene la longitud en pixels de una cadena
|
||||
[[nodiscard]] auto getCharacterSize() const -> int; // Devuelve el tamaño del caracter
|
||||
[[nodiscard]] auto glyphWidth(uint32_t codepoint, int kerning = 0) const -> int; // Devuelve el ancho en pixels de un glifo
|
||||
[[nodiscard]] auto getGlyphClip(uint32_t codepoint) const -> SDL_FRect; // Devuelve el clip rect del glifo
|
||||
[[nodiscard]] auto getSprite() const -> SurfaceSprite* { return sprite_.get(); } // Acceso al sprite interno
|
||||
|
||||
void setFixedWidth(bool value); // Establece si se usa un tamaño fijo de letra
|
||||
|
||||
|
||||
@@ -66,8 +66,9 @@ void Title::initMarquee() {
|
||||
uint32_t cp = Text::nextCodepoint(long_text_, pos);
|
||||
Glyph l;
|
||||
l.codepoint = cp;
|
||||
l.clip = marquee_text_->getGlyphClip(cp); // Pre-calcular clip rect (evita búsqueda por frame)
|
||||
l.x = MARQUEE_START_X;
|
||||
l.width = static_cast<float>(marquee_text_->glyphWidth(cp, 1)); // Pre-calcular ancho con kerning
|
||||
l.width = static_cast<float>(marquee_text_->glyphWidth(cp, 0)); // Pre-calcular ancho visual del glifo
|
||||
l.enabled = false;
|
||||
letters_.push_back(l);
|
||||
}
|
||||
@@ -223,15 +224,15 @@ void Title::updateMarquee(float delta_time) {
|
||||
|
||||
// Dibuja la marquesina
|
||||
void Title::renderMarquee() {
|
||||
auto* sprite = marquee_text_->getSprite();
|
||||
sprite->setY(MARQUEE_Y);
|
||||
// Solo renderizar letras activas (optimización: usa cache y rangos)
|
||||
for (int i = first_active_letter_; i <= last_active_letter_ + 1 && i < (int)letters_.size(); ++i) {
|
||||
const auto& letter = letters_[i];
|
||||
if (letter.enabled) {
|
||||
marquee_text_->writeColored(
|
||||
static_cast<int>(letter.x), // Conversión explícita float→int
|
||||
static_cast<int>(MARQUEE_Y), // Usar constante
|
||||
Text::codepointToUtf8(letter.codepoint), // Convertir codepoint a string UTF-8
|
||||
static_cast<Uint8>(PaletteColor::MAGENTA));
|
||||
if (letter.enabled && letter.clip.w > 0.0F) {
|
||||
sprite->setClip(letter.clip);
|
||||
sprite->setX(letter.x);
|
||||
sprite->render(1, static_cast<Uint8>(PaletteColor::MAGENTA));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ class Title {
|
||||
// --- Estructuras y enumeraciones ---
|
||||
struct Glyph {
|
||||
uint32_t codepoint{0}; // Codepoint Unicode del carácter
|
||||
SDL_FRect clip{}; // Clip rect pre-calculado en el bitmap de fuente
|
||||
float x{0.0F}; // Posición en el eje x (float para precisión con delta time)
|
||||
float width{0.0F}; // Ancho pre-calculado del carácter
|
||||
bool enabled{false}; // Solo se escriben y mueven si estan habilitadas
|
||||
|
||||
Reference in New Issue
Block a user