61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
// text_renderer.cpp - Implementació del renderitzador de text
|
|
// © 2025 Port a C++20 amb SDL3
|
|
|
|
#include "text_renderer.hpp"
|
|
#include "../core/graphics/shape_loader.hpp"
|
|
#include "../core/rendering/shape_renderer.hpp"
|
|
#include <iostream>
|
|
|
|
namespace Utils {
|
|
|
|
void TextRenderer::render(SDL_Renderer* renderer,
|
|
const std::string& text,
|
|
int x, int y,
|
|
int spacing) {
|
|
int current_x = x;
|
|
|
|
for (char c : text) {
|
|
std::string filename = get_char_filename(c);
|
|
auto shape = Graphics::ShapeLoader::load("font/" + filename);
|
|
|
|
if (shape && shape->es_valida()) {
|
|
Punt pos = {current_x, y};
|
|
Rendering::render_shape(renderer, shape, pos, 0.0f, 1.0f, true, 1.0f);
|
|
}
|
|
|
|
current_x += spacing;
|
|
}
|
|
}
|
|
|
|
int TextRenderer::calculate_width(const std::string& text, int spacing) {
|
|
return static_cast<int>(text.length()) * spacing;
|
|
}
|
|
|
|
std::string TextRenderer::get_char_filename(char c) {
|
|
// Números 0-9
|
|
if (c >= '0' && c <= '9')
|
|
return std::string("char_") + c + ".shp";
|
|
|
|
// Letras A-Z (mayúsculas)
|
|
if (c >= 'A' && c <= 'Z')
|
|
return std::string("char_") + c + ".shp";
|
|
|
|
// Convertir minúsculas a mayúsculas
|
|
if (c >= 'a' && c <= 'z')
|
|
return std::string("char_") + char(c - 32) + ".shp";
|
|
|
|
// Caracteres especiales
|
|
switch (c) {
|
|
case ':': return "char_colon.shp";
|
|
case '.': return "char_dot.shp";
|
|
case '-': return "char_minus.shp";
|
|
case ',': return "char_comma.shp";
|
|
case '!': return "char_exclamation.shp";
|
|
case '?': return "char_question.shp";
|
|
case ' ': return "char_space.shp";
|
|
default: return "char_question.shp"; // Fallback
|
|
}
|
|
}
|
|
|
|
} // namespace Utils
|