5#include "fifechan/backends/sdl2/truetypefont.hpp"
7#include <SDL2/SDL_render.h>
11#include "fifechan/backends/sdl2/graphics.hpp"
12#include "fifechan/exception.hpp"
13#include "fifechan/graphics.hpp"
14#include "fifechan/image.hpp"
23 mFont(TTF_OpenFont(filename.c_str(), size))
26 if (
mFont ==
nullptr) {
27 throwException(
"TrueTypeFont::TrueTypeFont. " + std::string(TTF_GetError()));
31 TrueTypeFont::~TrueTypeFont()
40 TTF_SizeText(
mFont, text.c_str(), &w, &h);
58 if (sdlGraphics ==
nullptr) {
59 throwException(
"TrueTypeFont::drawString. Graphics object must be fcn::sdl2::Graphics!");
73 SDL_Surface* textSurface =
nullptr;
75 textSurface = TTF_RenderText_Blended(
mFont, text.c_str(), sdlCol);
77 textSurface = TTF_RenderText_Solid(
mFont, text.c_str(), sdlCol);
80 if (textSurface ==
nullptr) {
81 throwException(
"TrueTypeFont::drawString. " + std::string(TTF_GetError()));
85 SDL_Renderer* renderer = sdlGraphics->getRenderTarget();
86 SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);
87 if (texture ==
nullptr) {
88 SDL_FreeSurface(textSurface);
89 throwException(
"TrueTypeFont::drawString. Failed to create texture: " + std::string(SDL_GetError()));
93 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
94#if SDL_VERSION_ATLEAST(2, 0, 12)
95 SDL_SetTextureScaleMode(texture, SDL_ScaleModeNearest);
102 src.w = textSurface->w;
103 src.h = textSurface->h;
107 sdlGraphics->drawSDLTexture(texture, src, dst);
109 SDL_DestroyTexture(texture);
110 SDL_FreeSurface(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.
SDL2 renderer-specific implementation of the Graphics interface.
TrueTypeFont(std::string const &filename, int size)
Constructor.
int mRowSpacing
Additional spacing between rows in pixels.
void drawString(fcn::Graphics *graphics, std::string const &text, int x, int y) override
Draws a string.
TTF_Font * mFont
Underlying TTF_Font pointer from SDL_ttf.
int mGlyphSpacing
Additional spacing between glyphs 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.
int getWidth(std::string const &text) const override
Gets the width of a string.
virtual void setRowSpacing(int spacing)
Sets the spacing between rows in pixels.
int getHeight() const override
Gets the height of the glyphs in the font.
virtual bool isAntiAlias()
Checks if anti aliasing is used.
virtual int getGlyphSpacing()
Gets the spacing between letters in pixels.
virtual int getRowSpacing()
Gets the spacing between rows in pixels.
bool mAntiAlias
Whether anti-aliasing is enabled for rendering.
virtual void setAntiAlias(bool antiAlias)
Enable or disable anti-aliasing for rendered glyphs.
Unified header for the SDL backend.