diff --git a/fonts/fonthandler.cpp b/fonts/fonthandler.cpp index 453311d..7f39b53 100644 --- a/fonts/fonthandler.cpp +++ b/fonts/fonthandler.cpp @@ -60,4 +60,18 @@ int Fonts::_measureText(Font font, std::string textToDraw) { return width; } -//*/ +Texture Fonts::_drawTextToTexture(Font font, std::string textToDraw, Color color) { + if (textToDraw.empty()) { + textToDraw = " "; + }; + + Texture tex; + tex.surface = TTF_RenderUTF8_Blended(font.font, textToDraw.c_str(), color); + + tex.width = tex.surface->w; + tex.height = tex.surface->h; + + tex.texture = SDL_CreateTextureFromSurface(Window::GetRenderer(), tex.surface); + + return tex; +} diff --git a/fonts/fonthandler.h b/fonts/fonthandler.h index 5f40bfc..b4a86ce 100644 --- a/fonts/fonthandler.h +++ b/fonts/fonthandler.h @@ -34,7 +34,7 @@ class Fonts { * @param textToDraw The text to draw. * @param color The color of the text. */ - static void DrawText(Font font, vf2d pos, const char *textToDraw, Color color) { + static void DrawText(Font font, vf2d pos, std::string textToDraw, Color color) { get()._drawText(font, pos, textToDraw, color); } @@ -48,6 +48,19 @@ class Fonts { static int MeasureText(Font font, std::string text) { return get()._measureText(font, text); } + + /** + * @brief Draws text onto a texture using the specified font. + * + * @param font The font to use for rendering the text. + * @param textToDraw The text string to render. + * @param color The color of the rendered text (default is WHITE). + * @return A Texture object representing the rendered text, or an empty texture if rendering fails. + */ + static Texture DrawTextToTexture(Font font, std::string textToDraw, Color color) { + return get()._drawTextToTexture(font, textToDraw, color); + } + private: std::unordered_map _fonts; @@ -57,6 +70,8 @@ class Fonts { int _measureText(Font font, std::string text); + Texture _drawTextToTexture(Font font, std::string textToDraw, Color color); + public: Fonts(const Fonts &) = delete;