CARACTERÍSTICAS:
- Notificaciones con fondo personalizado por tema (15 temas)
- Soporte completo para temas estáticos y dinámicos
- Interpolación LERP de colores durante transiciones
- Actualización por frame durante animaciones de temas
IMPLEMENTACIÓN:
Theme System:
- Añadido getNotificationBackgroundColor() a interfaz Theme
- StaticTheme: Color fijo por tema
- DynamicTheme: Interpolación entre keyframes
- ThemeManager: LERP durante transiciones (PHASE 3)
- ThemeSnapshot: Captura color para transiciones suaves
Colores por Tema:
Estáticos (9):
- SUNSET: Púrpura oscuro (120, 40, 80)
- OCEAN: Azul marino (20, 50, 90)
- NEON: Púrpura oscuro (60, 0, 80)
- FOREST: Marrón tierra (70, 50, 30)
- RGB: Gris claro (220, 220, 220)
- MONOCHROME: Gris oscuro (50, 50, 50)
- LAVENDER: Violeta oscuro (80, 50, 100)
- CRIMSON: Rojo oscuro (80, 10, 10)
- EMERALD: Verde oscuro (10, 80, 10)
Dinámicos (6, 20 keyframes totales):
- SUNRISE: 3 keyframes (noche→alba→día)
- OCEAN_WAVES: 2 keyframes (profundo→claro)
- NEON_PULSE: 2 keyframes (apagado→encendido)
- FIRE: 4 keyframes (brasas→llamas→inferno→llamas)
- AURORA: 4 keyframes (verde→violeta→cian→violeta)
- VOLCANIC: 4 keyframes (ceniza→erupción→lava→enfriamiento)
Notifier:
- Añadido SDL_Color bg_color a estructura Notification
- Método show() acepta parámetro bg_color
- renderBackground() usa color dinámico (no negro fijo)
- Soporte para cambios de color cada frame
Engine:
- Obtiene color de fondo desde ThemeManager
- Pasa bg_color al notifier en cada notificación
- Sincronizado con tema activo y transiciones
FIXES:
- TEXT_ABSOLUTE_SIZE cambiado de 16px a 12px (múltiplo nativo)
- Centrado de notificaciones corregido en F3 fullscreen
- updatePhysicalWindowSize() usa SDL_GetCurrentDisplayMode en F3
- Notificaciones centradas correctamente en ventana/F3/F4
🎨 Generated with Claude Code
254 lines
8.4 KiB
C++
254 lines
8.4 KiB
C++
#include "textrenderer.h"
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3_ttf/SDL_ttf.h>
|
|
|
|
TextRenderer::TextRenderer() : renderer_(nullptr), font_(nullptr), font_size_(0), use_antialiasing_(true) {
|
|
}
|
|
|
|
TextRenderer::~TextRenderer() {
|
|
cleanup();
|
|
}
|
|
|
|
bool TextRenderer::init(SDL_Renderer* renderer, const char* font_path, int font_size, bool use_antialiasing) {
|
|
renderer_ = renderer;
|
|
font_size_ = font_size;
|
|
use_antialiasing_ = use_antialiasing;
|
|
|
|
// Inicializar SDL_ttf si no está inicializado
|
|
if (!TTF_WasInit()) {
|
|
if (!TTF_Init()) {
|
|
SDL_Log("Error al inicializar SDL_ttf: %s", SDL_GetError());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Cargar la fuente
|
|
font_ = TTF_OpenFont(font_path, font_size);
|
|
if (font_ == nullptr) {
|
|
SDL_Log("Error al cargar fuente '%s': %s", font_path, SDL_GetError());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void TextRenderer::cleanup() {
|
|
if (font_ != nullptr) {
|
|
TTF_CloseFont(font_);
|
|
font_ = nullptr;
|
|
}
|
|
renderer_ = nullptr;
|
|
}
|
|
|
|
void TextRenderer::print(int x, int y, const char* text, uint8_t r, uint8_t g, uint8_t b) {
|
|
if (!isInitialized() || text == nullptr || text[0] == '\0') {
|
|
return;
|
|
}
|
|
|
|
// Crear superficie con el texto renderizado
|
|
SDL_Color color = {r, g, b, 255};
|
|
SDL_Surface* text_surface = nullptr;
|
|
|
|
if (use_antialiasing_) {
|
|
// Con antialiasing (suave, mejor calidad)
|
|
text_surface = TTF_RenderText_Blended(font_, text, strlen(text), color);
|
|
} else {
|
|
// Sin antialiasing (píxeles nítidos, estilo retro)
|
|
text_surface = TTF_RenderText_Solid(font_, text, strlen(text), color);
|
|
}
|
|
|
|
if (text_surface == nullptr) {
|
|
SDL_Log("Error al renderizar texto: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
// Crear textura desde la superficie
|
|
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer_, text_surface);
|
|
|
|
if (text_texture == nullptr) {
|
|
SDL_Log("Error al crear textura: %s", SDL_GetError());
|
|
SDL_DestroySurface(text_surface);
|
|
return;
|
|
}
|
|
|
|
// Preparar rectángulo de destino
|
|
SDL_FRect dest_rect;
|
|
dest_rect.x = static_cast<float>(x);
|
|
dest_rect.y = static_cast<float>(y);
|
|
dest_rect.w = static_cast<float>(text_surface->w);
|
|
dest_rect.h = static_cast<float>(text_surface->h);
|
|
|
|
// Renderizar la textura
|
|
SDL_RenderTexture(renderer_, text_texture, nullptr, &dest_rect);
|
|
|
|
// Limpiar recursos
|
|
SDL_DestroyTexture(text_texture);
|
|
SDL_DestroySurface(text_surface);
|
|
}
|
|
|
|
void TextRenderer::print(int x, int y, const std::string& text, uint8_t r, uint8_t g, uint8_t b) {
|
|
print(x, y, text.c_str(), r, g, b);
|
|
}
|
|
|
|
void TextRenderer::printPhysical(int logical_x, int logical_y, const char* text, uint8_t r, uint8_t g, uint8_t b, float scale_x, float scale_y) {
|
|
if (!isInitialized() || text == nullptr || text[0] == '\0') {
|
|
return;
|
|
}
|
|
|
|
// Convertir coordenadas lógicas a físicas
|
|
int physical_x = static_cast<int>(logical_x * scale_x);
|
|
int physical_y = static_cast<int>(logical_y * scale_y);
|
|
|
|
// Crear superficie con el texto renderizado
|
|
SDL_Color color = {r, g, b, 255};
|
|
SDL_Surface* text_surface = nullptr;
|
|
|
|
if (use_antialiasing_) {
|
|
text_surface = TTF_RenderText_Blended(font_, text, strlen(text), color);
|
|
} else {
|
|
text_surface = TTF_RenderText_Solid(font_, text, strlen(text), color);
|
|
}
|
|
|
|
if (text_surface == nullptr) {
|
|
SDL_Log("Error al renderizar texto: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
// Crear textura desde la superficie
|
|
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer_, text_surface);
|
|
|
|
if (text_texture == nullptr) {
|
|
SDL_Log("Error al crear textura: %s", SDL_GetError());
|
|
SDL_DestroySurface(text_surface);
|
|
return;
|
|
}
|
|
|
|
// Renderizar en coordenadas físicas (bypass presentación lógica)
|
|
// Usar SDL_RenderTexture con coordenadas absolutas de ventana
|
|
SDL_FRect dest_rect;
|
|
dest_rect.x = static_cast<float>(physical_x);
|
|
dest_rect.y = static_cast<float>(physical_y);
|
|
dest_rect.w = static_cast<float>(text_surface->w);
|
|
dest_rect.h = static_cast<float>(text_surface->h);
|
|
|
|
// Deshabilitar temporalmente presentación lógica para renderizar en píxeles físicos
|
|
int logical_w = 0, logical_h = 0;
|
|
SDL_RendererLogicalPresentation presentation_mode;
|
|
SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &presentation_mode);
|
|
|
|
// Renderizar sin presentación lógica (coordenadas absolutas)
|
|
SDL_SetRenderLogicalPresentation(renderer_, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED);
|
|
SDL_RenderTexture(renderer_, text_texture, nullptr, &dest_rect);
|
|
|
|
// Restaurar presentación lógica
|
|
SDL_SetRenderLogicalPresentation(renderer_, logical_w, logical_h, presentation_mode);
|
|
|
|
// Limpiar recursos
|
|
SDL_DestroyTexture(text_texture);
|
|
SDL_DestroySurface(text_surface);
|
|
}
|
|
|
|
void TextRenderer::printPhysical(int logical_x, int logical_y, const std::string& text, uint8_t r, uint8_t g, uint8_t b, float scale_x, float scale_y) {
|
|
printPhysical(logical_x, logical_y, text.c_str(), r, g, b, scale_x, scale_y);
|
|
}
|
|
|
|
void TextRenderer::printAbsolute(int physical_x, int physical_y, const char* text, SDL_Color color) {
|
|
if (!isInitialized() || text == nullptr || text[0] == '\0') {
|
|
return;
|
|
}
|
|
|
|
// Crear superficie con el texto renderizado
|
|
SDL_Surface* text_surface = nullptr;
|
|
|
|
if (use_antialiasing_) {
|
|
text_surface = TTF_RenderText_Blended(font_, text, strlen(text), color);
|
|
} else {
|
|
text_surface = TTF_RenderText_Solid(font_, text, strlen(text), color);
|
|
}
|
|
|
|
if (text_surface == nullptr) {
|
|
SDL_Log("Error al renderizar texto: %s", SDL_GetError());
|
|
return;
|
|
}
|
|
|
|
// Crear textura desde la superficie
|
|
SDL_Texture* text_texture = SDL_CreateTextureFromSurface(renderer_, text_surface);
|
|
|
|
if (text_texture == nullptr) {
|
|
SDL_Log("Error al crear textura: %s", SDL_GetError());
|
|
SDL_DestroySurface(text_surface);
|
|
return;
|
|
}
|
|
|
|
// Configurar alpha blending si el color tiene transparencia
|
|
if (color.a < 255) {
|
|
SDL_SetTextureBlendMode(text_texture, SDL_BLENDMODE_BLEND);
|
|
SDL_SetTextureAlphaModFloat(text_texture, color.a / 255.0f);
|
|
}
|
|
|
|
// Preparar rectángulo de destino en coordenadas físicas absolutas
|
|
SDL_FRect dest_rect;
|
|
dest_rect.x = static_cast<float>(physical_x);
|
|
dest_rect.y = static_cast<float>(physical_y);
|
|
dest_rect.w = static_cast<float>(text_surface->w);
|
|
dest_rect.h = static_cast<float>(text_surface->h);
|
|
|
|
// Deshabilitar temporalmente presentación lógica para renderizar en píxeles físicos
|
|
int logical_w = 0, logical_h = 0;
|
|
SDL_RendererLogicalPresentation presentation_mode;
|
|
SDL_GetRenderLogicalPresentation(renderer_, &logical_w, &logical_h, &presentation_mode);
|
|
|
|
// Renderizar sin presentación lógica (coordenadas absolutas)
|
|
SDL_SetRenderLogicalPresentation(renderer_, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED);
|
|
SDL_RenderTexture(renderer_, text_texture, nullptr, &dest_rect);
|
|
|
|
// Restaurar presentación lógica
|
|
SDL_SetRenderLogicalPresentation(renderer_, logical_w, logical_h, presentation_mode);
|
|
|
|
// Limpiar recursos
|
|
SDL_DestroyTexture(text_texture);
|
|
SDL_DestroySurface(text_surface);
|
|
}
|
|
|
|
void TextRenderer::printAbsolute(int physical_x, int physical_y, const std::string& text, SDL_Color color) {
|
|
printAbsolute(physical_x, physical_y, text.c_str(), color);
|
|
}
|
|
|
|
int TextRenderer::getTextWidth(const char* text) {
|
|
if (!isInitialized() || text == nullptr) {
|
|
return 0;
|
|
}
|
|
|
|
int width = 0;
|
|
int height = 0;
|
|
if (!TTF_GetStringSize(font_, text, strlen(text), &width, &height)) {
|
|
return 0;
|
|
}
|
|
return width;
|
|
}
|
|
|
|
int TextRenderer::getTextWidthPhysical(const char* text) {
|
|
// Retorna el ancho REAL en píxeles físicos (sin escalado lógico)
|
|
// Idéntico a getTextWidth() pero semánticamente diferente:
|
|
// - Este método se usa cuando se necesita el ancho REAL de la fuente
|
|
// - Útil para calcular dimensiones de UI en coordenadas físicas absolutas
|
|
if (!isInitialized() || text == nullptr) {
|
|
return 0;
|
|
}
|
|
|
|
int width = 0;
|
|
int height = 0;
|
|
if (!TTF_GetStringSize(font_, text, strlen(text), &width, &height)) {
|
|
return 0;
|
|
}
|
|
return width; // Ancho real de la textura generada por TTF
|
|
}
|
|
|
|
int TextRenderer::getTextHeight() {
|
|
if (!isInitialized()) {
|
|
return 0;
|
|
}
|
|
|
|
return TTF_GetFontHeight(font_);
|
|
}
|