MCPcopy Create free account
hub / github.com/AbyssEngine/AbyssEngineOld / SpriteFont

Class SpriteFont

src/Abyss/UI/SpriteFont.h:24–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22};
23
24template <Concepts::Drawable T> class SpriteFont final : public Concepts::FontRenderer {
25 T _drawable;
26 absl::flat_hash_map<int, Glyph> _glyphs;
27
28 auto renderText(const std::string_view text, int &width, int &height) const -> std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> override {
29 std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> texture{nullptr, SDL_DestroyTexture};
30 width = 0;
31 height = 0;
32
33 for (const auto &c : text) {
34 const auto &[glyphFrameIndex, glyphWidth, glyphHeight, glyphOffsetX, glyphOffsetY] = _glyphs.at(c >= _glyphs.size() ? '?' : c);
35 int frameWidth, frameHeight;
36 _drawable.getFrameSize(glyphFrameIndex, frameWidth, frameHeight);
37 width += glyphWidth + glyphOffsetX;
38 height = std::max(height, frameHeight);
39 }
40
41 const auto renderer = Singletons::getRendererProvider().getRenderer();
42 texture.reset(SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width, height));
43 SDL_SetTextureBlendMode(texture.get(), SDL_BLENDMODE_MUL);
44 const auto oldTarget = SDL_GetRenderTarget(renderer);
45 SDL_SetRenderTarget(renderer, texture.get());
46
47 int drawX = 0;
48 for (const auto &c : text) {
49 const auto &[glyphFrameIndex, glyphWidth, glyphHeight, glyphOffsetX, glyphOffsetY] = _glyphs.at(c >= _glyphs.size() ? '?' : c);
50 int frameWidth, frameHeight;
51 _drawable.getFrameSize(glyphFrameIndex, frameWidth, frameHeight);
52 _drawable.draw(glyphFrameIndex, drawX, height);
53 drawX += glyphWidth + glyphOffsetX;
54 }
55
56 SDL_SetRenderTarget(renderer, oldTarget);
57
58 return std::move(texture);
59 }
60
61 public:
62 explicit SpriteFont(const std::string_view path, const DataTypes::Palette &palette) : _drawable(absl::StrCat(path, ".dc6")) {
63 Abyss::Common::Log::debug("Loading font {}...", path);
64
65 _drawable.setPalette(palette);
66
67 auto tableStream = Singletons::getFileProvider().loadFile(absl::StrCat(path, ".tbl"));
68 Streams::StreamReader sr(tableStream);
69 char signature[5] = {};
70 sr.readBytes(signature);
71 if (std::string_view(signature, 5) != "Woo!\x01")
72 throw std::runtime_error(absl::StrCat("Invalid font file signature: ", path));
73
74 tableStream.ignore(7); // skip unknown bytes
75
76 while (!tableStream.eof()) {
77 const auto code = sr.readUInt16();
78 auto &[glyphFrameIndex, glyphWidth, glyphHeight, glyphOffsetX, glyphOffsetY] = _glyphs[code];
79
80 tableStream.ignore(1); // Skip a byte for some reason
81

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected