SDLPP: allow change of font size

This commit is contained in:
zv0n 2020-12-18 15:08:21 +01:00
parent 346b8ce384
commit 57143262ac
2 changed files with 35 additions and 8 deletions

View File

@ -1,16 +1,14 @@
#include "sdlpp_font.hpp" #include "sdlpp_font.hpp"
namespace SDLPP { namespace SDLPP {
Font::Font( const std::string &font, int size ) { Font::Font( const std::string &font ) : font_path(font) {}
font_ptr = TTF_OpenFont( font.c_str(), size ); Font::Font( const std::string &font, int size ) : Font(font) {
if ( font_ptr == NULL ) { original_size = size;
std::cerr << "Unable to load font '" << font font_ptr = TTF_OpenFont( font_path.c_str(), size );
<< "': TTF Error: " << TTF_GetError() << std::endl; checkFont();
throw "TTF_OpenFont error";
}
} }
Font::~Font() { Font::~Font() {
TTF_CloseFont( font_ptr ); closeFont();
} }
const TTF_Font *Font::getFont() const { const TTF_Font *Font::getFont() const {
return font_ptr; return font_ptr;
@ -30,4 +28,25 @@ void Font::setStyle( int style ) {
void Font::setHinting( int hinting ) { void Font::setHinting( int hinting ) {
TTF_SetFontHinting( font_ptr, 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 } // namespace SDLPP

View File

@ -9,6 +9,7 @@ namespace SDLPP {
class SDLPPSCOPE Font { class SDLPPSCOPE Font {
public: public:
Font() = delete; Font() = delete;
Font( const std::string &font );
Font( const std::string &font, int size ); Font( const std::string &font, int size );
virtual ~Font(); virtual ~Font();
const TTF_Font *getFont() const; const TTF_Font *getFont() const;
@ -17,9 +18,16 @@ public:
int getOutline(); int getOutline();
void setStyle( int style ); void setStyle( int style );
void setHinting( int hinting ); void setHinting( int hinting );
void changeFontSize( int size );
void revertSize();
private: private:
void closeFont();
void checkFont();
TTF_Font *font_ptr; TTF_Font *font_ptr;
const std::string font_path;
int original_size = -1;
}; };
} // namespace SDLPP } // namespace SDLPP
#endif #endif