Treballant en text independent de la resolucio

This commit is contained in:
2025-10-09 20:43:34 +02:00
parent f00b08b6be
commit 68381dc92d
4 changed files with 128 additions and 32 deletions

View File

@@ -90,6 +90,68 @@ void TextRenderer::print(int x, int y, const std::string& text, uint8_t r, uint8
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);
}
int TextRenderer::getTextWidth(const char* text) {
if (!isInitialized() || text == nullptr) {
return 0;