Skip to content

Commit

Permalink
Added DrawTextToTexture to the FontsHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
bXi committed Mar 11, 2024
1 parent 5225e18 commit dbffae4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion fonts/fonthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 16 additions & 1 deletion fonts/fonthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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<std::string, Font> _fonts;

Expand All @@ -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;

Expand Down

0 comments on commit dbffae4

Please sign in to comment.