59 lines
1015 B
C++
59 lines
1015 B
C++
#include <cassert>
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
#include "common.hh"
|
|
#include "texture.hh"
|
|
#include "font.hh"
|
|
|
|
Font::Font(std::string const& filename, unsigned ptsize)
|
|
: font_{nullptr}
|
|
{
|
|
assert(TTF_WasInit());
|
|
font_ = TTF_OpenFont(filename.c_str(), ptsize);
|
|
if (!font_)
|
|
throw TTFException{};
|
|
}
|
|
|
|
Font::Font(Font&& move)
|
|
: font_(move.font_)
|
|
{
|
|
move.font_ = nullptr;
|
|
}
|
|
|
|
Font& Font::operator=(Font&& move)
|
|
{
|
|
if (font_)
|
|
TTF_CloseFont(font_);
|
|
font_ = move.font_;
|
|
move.font_ = nullptr;
|
|
|
|
return *this;
|
|
}
|
|
|
|
Font::~Font()
|
|
{
|
|
if (font_)
|
|
TTF_CloseFont(font_);
|
|
}
|
|
|
|
Texture2D Font::render(std::string const& text, bool fast) const
|
|
{
|
|
SDLSurfaceUPtr surf;
|
|
if (fast)
|
|
surf.reset(TTF_RenderUTF8_Solid(font_, text.c_str(), SDL_Color{255, 255, 255, 255}));
|
|
else
|
|
surf.reset(TTF_RenderUTF8_Blended(font_, text.c_str(), SDL_Color{255, 255, 255, 255}));
|
|
|
|
if (!surf)
|
|
throw TTFException{};
|
|
|
|
Texture2D ret{surf.get()};
|
|
|
|
return ret;
|
|
}
|
|
|
|
TTF_Font* Font::getFont() const
|
|
{
|
|
return font_;
|
|
}
|