Files
aee/source/core/rendering/text.hpp
2026-04-05 01:15:45 +02:00

50 lines
1.5 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;
// Calcula ancho en píxeles d'un text
[[nodiscard]] auto width(const char* text) const -> int;
[[nodiscard]] auto charHeight() const -> int { return box_height_; }
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;
};