1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-29 04:10:16 +02:00

Made Font ownership explicit using unique_ptr

This commit is contained in:
Maarten ter Huurne 2014-07-31 23:20:31 +02:00
parent 902145b698
commit e32964bb50
4 changed files with 13 additions and 19 deletions

View File

@ -14,9 +14,9 @@
using namespace std; using namespace std;
Font *Font::defaultFont() unique_ptr<Font> Font::defaultFont()
{ {
return new Font(TTF_FONT, TTF_FONT_SIZE); return unique_ptr<Font>(new Font(TTF_FONT, TTF_FONT_SIZE));
} }
Font::Font(const std::string &path, unsigned int size) Font::Font(const std::string &path, unsigned int size)

View File

@ -5,20 +5,26 @@
#define FONT_H #define FONT_H
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <memory>
#include <string> #include <string>
class Surface; class Surface;
/**
* Wrapper around a TrueType or other FreeType-supported font.
* The wrapper is valid even if the font couldn't be loaded, but in that case
* nothing will be drawn.
*/
class Font { class Font {
public: public:
enum HAlign { HAlignLeft, HAlignRight, HAlignCenter }; enum HAlign { HAlignLeft, HAlignRight, HAlignCenter };
enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle }; enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle };
/** /**
* Returns a newly created Font object for the default font, * Returns a newly created Font object for the default font.
* or nullptr if there was a problem creating it.
*/ */
static Font *defaultFont(); static std::unique_ptr<Font> defaultFont();
Font(const std::string &path, unsigned int size); Font(const std::string &path, unsigned int size);
~Font(); ~Font();

View File

@ -278,7 +278,6 @@ GMenu2X::~GMenu2X() {
delete PowerSaver::getInstance(); delete PowerSaver::getInstance();
quit(); quit();
delete font;
#ifdef ENABLE_INOTIFY #ifdef ENABLE_INOTIFY
delete monitor; delete monitor;
#endif #endif
@ -355,11 +354,6 @@ void GMenu2X::initBG() {
} }
void GMenu2X::initFont() { void GMenu2X::initFont() {
if (font) {
delete font;
font = NULL;
}
string path = skinConfStr["font"]; string path = skinConfStr["font"];
if (!path.empty()) { if (!path.empty()) {
unsigned int size = skinConfInt["fontsize"]; unsigned int size = skinConfInt["fontsize"];
@ -367,16 +361,10 @@ void GMenu2X::initFont() {
size = 12; size = 12;
if (path.substr(0,5)=="skin:") if (path.substr(0,5)=="skin:")
path = sc.getSkinFilePath(path.substr(5, path.length())); path = sc.getSkinFilePath(path.substr(5, path.length()));
font = new Font(path, size); font.reset(new Font(path, size));
} else { } else {
font = Font::defaultFont(); font = Font::defaultFont();
} }
if (!font) {
ERROR("Cannot function without font; aborting...\n");
quit();
exit(-1);
}
} }
void GMenu2X::initMenu() { void GMenu2X::initMenu() {

View File

@ -159,7 +159,7 @@ public:
SurfaceCollection sc; SurfaceCollection sc;
Translator tr; Translator tr;
Surface *s, *bg; Surface *s, *bg;
Font *font; std::unique_ptr<Font> font;
//Status functions //Status functions
void main(); void main();