spacebox/src/Text.cpp

76 lines
1.9 KiB
C++

#include "Text.hpp"
using namespace sb;
/*!
* @param content text to be displayed
*/
void Text::content(const std::string& content)
{
_content = content;
refresh();
}
/*!
* @param foreground text color
*/
void Text::foreground(const sb::Color& foreground)
{
_foreground = foreground;
refresh();
}
/*!
* @param background text background color which fills the Plane
*/
void Text::background(const sb::Color& background)
{
_background = background;
refresh();
}
/*!
* @param font shared pointer to a TTF_Font
*/
void Text::font(std::shared_ptr<TTF_Font> font)
{
_font = font;
refresh();
}
void Text::refresh()
{
/* Create pixel data using the SDL image library. The pixel data surface is converted from paletted format to ARGB and flipped. */
SDL_Surface* shaded = TTF_RenderText_Shaded(_font.get(), _content.c_str(), _foreground, _background);
if (!shaded)
{
sb::Log::sdl_error("Could not create text");
}
else
{
SDL_Surface* converted = converted = SDL_ConvertSurfaceFormat(shaded, SDL_PIXELFORMAT_ARGB8888, 0);
if (!converted)
{
sb::Log::sdl_error("Could not convert pixel format");
}
else
{
SDL_Surface* flipped = rotozoomSurfaceXY(converted, 0, 1, -1, 0);
if (!flipped)
{
sb::Log::sdl_error("Could not flip surface");
}
else
{
/* Generate texture and create storage. Load the pixels from the text rendering surface into the default texture. The texture object will handle
* destroying the previous texture. Then destroy the pixels surface. */
texture().generate({flipped->w, flipped->h}, GL_RGBA8, GL_LINEAR);
texture().load(flipped);
SDL_FreeSurface(flipped);
}
SDL_FreeSurface(converted);
}
SDL_FreeSurface(shaded);
}
}