70 lines
2.6 KiB
C++
70 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#include "core/rendering/sprite/sprite.hpp"
|
|
class Surface;
|
|
|
|
// Bitmap font amb suport UTF-8. Els glifs es guarden en una única graella
|
|
// al png, amb ample variable per caràcter i una tinta monocroma que es
|
|
// recolora al vol via Sprite::render(src, tgt).
|
|
class Text {
|
|
public:
|
|
struct Offset {
|
|
int x{0};
|
|
int y{0};
|
|
int w{0};
|
|
};
|
|
|
|
struct File {
|
|
int box_width{0};
|
|
int box_height{0};
|
|
int columns{16};
|
|
int cell_spacing{0};
|
|
int row_spacing{0};
|
|
std::unordered_map<std::uint32_t, Offset> offset;
|
|
};
|
|
|
|
// Flags per a writeDX (composables amb |).
|
|
static constexpr Uint8 COLOR_FLAG = 1;
|
|
static constexpr Uint8 SHADOW_FLAG = 2;
|
|
static constexpr Uint8 CENTER_FLAG = 4;
|
|
static constexpr Uint8 STROKE_FLAG = 8;
|
|
|
|
Text(const std::shared_ptr<Surface>& surface, const std::string& text_file);
|
|
Text(const std::shared_ptr<Surface>& surface, const std::shared_ptr<File>& text_file);
|
|
~Text() = default;
|
|
|
|
Text(const Text&) = delete;
|
|
Text(Text&&) = delete;
|
|
auto operator=(const Text&) -> Text& = delete;
|
|
auto operator=(Text&&) -> Text& = delete;
|
|
|
|
void write(int x, int y, const std::string& text, int kerning = 1, int length = -1);
|
|
void writeColored(int x, int y, const std::string& text, Uint8 color, int kerning = 1, int length = -1);
|
|
void writeShadowed(int x, int y, const std::string& text, Uint8 color, Uint8 shadow_distance = 1, int kerning = 1, int length = -1);
|
|
void writeCentered(int x, int y, const std::string& text, int kerning = 1, int length = -1);
|
|
void writeDX(Uint8 flags, int x, int y, const std::string& text, int kerning = 1, Uint8 text_color = 0, Uint8 shadow_distance = 1, Uint8 shadow_color = 0, int length = -1);
|
|
|
|
[[nodiscard]] auto length(const std::string& text, int kerning = 1) const -> int;
|
|
[[nodiscard]] auto getCharacterSize() const -> int { return box_width_; }
|
|
[[nodiscard]] auto glyphWidth(std::uint32_t codepoint, int kerning = 0) const -> int;
|
|
[[nodiscard]] auto getGlyphClip(std::uint32_t codepoint) const -> SDL_FRect;
|
|
[[nodiscard]] auto getSprite() const -> Sprite* { return sprite_.get(); }
|
|
|
|
static auto loadTextFile(const std::string& file_path) -> std::shared_ptr<File>;
|
|
static auto codepointToUtf8(std::uint32_t cp) -> std::string;
|
|
static auto nextCodepoint(const std::string& s, std::size_t& pos) -> std::uint32_t;
|
|
|
|
private:
|
|
std::unique_ptr<Sprite> sprite_;
|
|
int box_width_{0};
|
|
int box_height_{0};
|
|
std::unordered_map<std::uint32_t, Offset> offset_;
|
|
};
|