unique_ptr based management for SDL_Surfaces

This commit is contained in:
2015-03-08 13:59:08 +01:00
parent 31e3a307b5
commit ffbc59140a
6 changed files with 80 additions and 50 deletions

49
font.cc
View File

@@ -10,32 +10,49 @@ Font::Font(std::string const& filename, unsigned ptsize)
{
assert(TTF_WasInit());
font_ = TTF_OpenFont(filename.c_str(), ptsize);
if(!font_)
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_)
if (font_)
TTF_CloseFont(font_);
}
Texture2D Font::render(std::string const& text, bool fast) const {
SDL_Surface *surf;
if(fast)
surf = TTF_RenderUTF8_Solid(font_, text.c_str(), SDL_Color{255, 255, 255, 255});
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 = TTF_RenderUTF8_Blended(font_, text.c_str(), SDL_Color{255, 255, 255, 255});
surf.reset(TTF_RenderUTF8_Blended(font_, text.c_str(), SDL_Color{255, 255, 255, 255}));
if(!surf)
if (!surf)
throw TTFException{};
try {
Texture2D ret{surf};
SDL_FreeSurface(surf);
return ret;
} catch(...) {
SDL_FreeSurface(surf);
throw;
}
Texture2D ret{surf.get()};
return ret;
}
TTF_Font* Font::getFont() const
{
return font_;
}