From 57143262ac624f33bb77a841c9c50e60e2c8e175 Mon Sep 17 00:00:00 2001 From: zv0n Date: Fri, 18 Dec 2020 15:08:21 +0100 Subject: [PATCH] SDLPP: allow change of font size --- sdlpp/sdlpp_font.cpp | 35 +++++++++++++++++++++++++++-------- sdlpp/sdlpp_font.hpp | 8 ++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/sdlpp/sdlpp_font.cpp b/sdlpp/sdlpp_font.cpp index 81b6996..f4d7f11 100644 --- a/sdlpp/sdlpp_font.cpp +++ b/sdlpp/sdlpp_font.cpp @@ -1,16 +1,14 @@ #include "sdlpp_font.hpp" namespace SDLPP { -Font::Font( const std::string &font, int size ) { - font_ptr = TTF_OpenFont( font.c_str(), size ); - if ( font_ptr == NULL ) { - std::cerr << "Unable to load font '" << font - << "': TTF Error: " << TTF_GetError() << std::endl; - throw "TTF_OpenFont error"; - } +Font::Font( const std::string &font ) : font_path(font) {} +Font::Font( const std::string &font, int size ) : Font(font) { + original_size = size; + font_ptr = TTF_OpenFont( font_path.c_str(), size ); + checkFont(); } Font::~Font() { - TTF_CloseFont( font_ptr ); + closeFont(); } const TTF_Font *Font::getFont() const { return font_ptr; @@ -30,4 +28,25 @@ void Font::setStyle( int style ) { void Font::setHinting( int hinting ) { TTF_SetFontHinting( font_ptr, hinting ); } +void Font::changeFontSize( int size ) { + closeFont(); + font_ptr = TTF_OpenFont( font_path.c_str(), size ); + checkFont(); +} +void Font::revertSize() { + closeFont(); + font_ptr = TTF_OpenFont( font_path.c_str(), original_size ); + checkFont(); +} + +void Font::closeFont() { + TTF_CloseFont( font_ptr ); +} +void Font::checkFont() { + if ( font_ptr == NULL ) { + std::cerr << "Unable to load font '" << font_path + << "': TTF Error: " << TTF_GetError() << std::endl; + throw "TTF_OpenFont error"; + } +} } // namespace SDLPP diff --git a/sdlpp/sdlpp_font.hpp b/sdlpp/sdlpp_font.hpp index 7983cde..b97fece 100644 --- a/sdlpp/sdlpp_font.hpp +++ b/sdlpp/sdlpp_font.hpp @@ -9,6 +9,7 @@ namespace SDLPP { class SDLPPSCOPE Font { public: Font() = delete; + Font( const std::string &font ); Font( const std::string &font, int size ); virtual ~Font(); const TTF_Font *getFont() const; @@ -17,9 +18,16 @@ public: int getOutline(); void setStyle( int style ); void setHinting( int hinting ); + void changeFontSize( int size ); + void revertSize(); private: + void closeFont(); + void checkFont(); + TTF_Font *font_ptr; + const std::string font_path; + int original_size = -1; }; } // namespace SDLPP #endif