Files
openglplayground/FontProvider.cc
2015-03-10 00:48:25 +01:00

33 lines
694 B
C++

#include <mutex>
#include "FontProvider.hh"
template<>
std::unique_ptr<FontProvider> Singleton<FontProvider>::instance{nullptr};
template<>
std::once_flag Singleton<FontProvider>::instance_flag{};
FontProvider::FontProvider()
{
}
TTF_Font *FontProvider::getFont(std::string const& name, unsigned ptsize)
{
auto key = make_tuple(name, ptsize);
auto it = fontCache_.find(key);
if (it == fontCache_.end()) {
TTFFontUPtr font{TTF_OpenFont(name.c_str(), ptsize)};
if (!font)
throw TTFException{};
std::tie(it, std::ignore) = fontCache_.insert(make_pair(key, std::move(font)));
}
return it->second.get();
}
void FontProvider::cleanup()
{
fontCache_.clear();
}