Files
aee/source/core/rendering/text.hpp
2026-04-05 09:39:05 +02:00

63 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
#include <string>
#include <unordered_map>
class Text {
public:
Text(const char* fnt_file, const char* gif_file);
~Text();
// Pinta texto sobre un buffer ARGB de 320x200
void draw(Uint32* pixel_data, int x, int y, const char* text, Uint32 color) const;
void drawCentered(Uint32* pixel_data, int y, const char* text, Uint32 color) const;
// Com draw, però clippat a [clip_x_min, clip_x_max) × [clip_y_min, clip_y_max)
void drawClipped(Uint32* pixel_data, int x, int y, const char* text, Uint32 color, int clip_x_min, int clip_x_max, int clip_y_min, int clip_y_max) const;
// Dibuixa text monoespaiat: cada glif ocupa `cell_w` i es centra dins la cel·la.
// Útil per a comptadors (FPS, hora) on cada dígit pot tenir amplades diferents.
void drawMono(Uint32* pixel_data, int x, int y, const char* text, Uint32 color, int cell_w) const;
// Mono només per a dígits (0-9): cada dígit ocupa `digit_cell_w` centrat;
// la resta de glifs mantenen l'amplada natural. Ideal per a comptadors tipus "9:59.59".
void drawMonoDigits(Uint32* pixel_data, int x, int y, const char* text, Uint32 color, int digit_cell_w) const;
// Calcula ancho en píxeles d'un text
[[nodiscard]] auto width(const char* text) const -> int;
// Amplada mono: nombre de codepoints × cell_w
[[nodiscard]] auto widthMono(const char* text, int cell_w) const -> int;
// Amplada mono-dígits: amplada natural, però substituint els dígits per digit_cell_w
[[nodiscard]] auto widthMonoDigits(const char* text, int digit_cell_w) const -> int;
[[nodiscard]] auto charHeight() const -> int { return box_height_; }
[[nodiscard]] auto charBoxWidth() const -> int { return box_width_; }
private:
struct GlyphInfo {
int x, y; // posició en el bitmap
int w; // ample visual
};
int box_width_{0};
int box_height_{0};
int columns_{0};
int cell_spacing_{0};
int row_spacing_{0};
Uint8* bitmap_{nullptr}; // píxels 8-bit del GIF de la font
int bitmap_width_{0};
int bitmap_height_{0};
std::unordered_map<uint32_t, GlyphInfo> glyphs_;
static auto nextCodepoint(const char*& ptr) -> uint32_t;
void loadFont(const char* fnt_file);
void loadBitmap(const char* gif_file);
static constexpr int SCREEN_WIDTH = 320;
static constexpr int SCREEN_HEIGHT = 200;
};