| 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")) { |
no test coverage detected