6#include "fifechan/backends/sdl3/truetypefont.hpp"
15#include "fifechan/backends/sdl3/graphics.hpp"
16#include "fifechan/exception.hpp"
17#include "fifechan/graphics.hpp"
18#include "fifechan/image.hpp"
27 mFont(TTF_OpenFont(filename.c_str(), static_cast<float>(size)))
30 if (
mFont ==
nullptr) {
31 throwException(
"TrueTypeFont::TrueTypeFont. " + std::string(SDL_GetError()));
35 TrueTypeFont::~TrueTypeFont()
45 TTF_GetStringSize(
mFont, text.c_str(), text.length(), &w, &h);
63 if (sdlGraphics ==
nullptr) {
64 throwException(
"TrueTypeFont::drawString. Graphics object must be fcn::sdl3::Graphics!");
78 SDL_Surface* textSurface =
nullptr;
81 textSurface = TTF_RenderText_Blended(
mFont, text.c_str(), text.length(), sdlCol);
83 textSurface = TTF_RenderText_Solid(
mFont, text.c_str(), text.length(), sdlCol);
86 if (textSurface ==
nullptr) {
87 throwException(
"TrueTypeFont::drawString. " + std::string(SDL_GetError()));
91 SDL_Renderer* renderer = sdlGraphics->getRenderTarget();
92 SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);
93 if (texture ==
nullptr) {
94 SDL_DestroySurface(textSurface);
95 throwException(
"TrueTypeFont::drawString. Failed to create texture: " + std::string(SDL_GetError()));
99 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
100 SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
104 dst.x =
static_cast<float>(x);
105 dst.y =
static_cast<float>(y + yoffset);
106 dst.w =
static_cast<float>(textSurface->w);
107 dst.h =
static_cast<float>(textSurface->h);
108 src.w =
static_cast<float>(textSurface->w);
109 src.h =
static_cast<float>(textSurface->h);
113 sdlGraphics->drawSDLTexture(texture, src, dst);
115 SDL_DestroyTexture(texture);
116 SDL_DestroySurface(textSurface);
uint8_t a
Alpha color component (0-255).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
virtual Color const & getColor() const =0
Gets the color to use when drawing.
SDL3 renderer-specific implementation of the Graphics interface.
virtual bool isAntiAlias()
Checks if anti aliasing is used.
int mGlyphSpacing
Additional spacing between glyphs in pixels.
int mRowSpacing
Additional spacing between rows in pixels.
std::string mFilename
Filename of the font used to create mFont.
virtual void setGlyphSpacing(int spacing)
Sets the spacing between letters in pixels.
TTF_Font * mFont
Underlying TTF_Font pointer from SDL_ttf.
virtual void setAntiAlias(bool antiAlias)
Enable or disable anti-aliasing for rendered glyphs.
int getWidth(std::string const &text) const override
Gets the width of a string.
virtual int getRowSpacing()
Gets the spacing between rows in pixels.
void drawString(fcn::Graphics *graphics, std::string const &text, int x, int y) override
Draws a string.
bool mAntiAlias
Whether anti-aliasing is enabled for rendering.
virtual void setRowSpacing(int spacing)
Sets the spacing between rows in pixels.
TrueTypeFont(std::string const &filename, int size)
Constructor.
virtual int getGlyphSpacing()
Gets the spacing between letters in pixels.
int getHeight() const override
Gets the height of the glyphs in the font.
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.