Treballant en text independent de la resolucio
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -19,7 +19,11 @@ public:
|
||||
void print(int x, int y, const char* text, uint8_t r, uint8_t g, uint8_t b);
|
||||
void print(int x, int y, const std::string& text, uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
// Obtiene el ancho de un texto renderizado
|
||||
// Renderiza texto en coordenadas lógicas, pero convierte a físicas para tamaño absoluto
|
||||
void 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);
|
||||
void 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);
|
||||
|
||||
// Obtiene el ancho de un texto renderizado (en píxeles lógicos para compatibilidad)
|
||||
int getTextWidth(const char* text);
|
||||
|
||||
// Obtiene la altura de la fuente
|
||||
|
||||
Reference in New Issue
Block a user