1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-25 20:51:38 +02:00

Don't construct non-functional Font objects

Replaced Font constructor with factory method, so that if the TTF
cannot be loaded, the Font object is not constructed. The normal C++
way of handling this is with exceptions, but we're compiling with
-fno-exceptions.
This commit is contained in:
Maarten ter Huurne 2013-08-04 00:58:32 +02:00
parent 724aefe482
commit f8dc4c7bb8
3 changed files with 21 additions and 14 deletions

View File

@ -14,19 +14,25 @@
using namespace std;
Font::Font(const string &path)
Font *Font::defaultFont()
{
if (!TTF_WasInit() && TTF_Init() < 0) {
ERROR("Unable to init SDL_ttf library\n");
return;
return nullptr;
}
font = TTF_OpenFont(TTF_FONT, TTF_FONT_SIZE);
TTF_Font *font = TTF_OpenFont(TTF_FONT, TTF_FONT_SIZE);
if (!font) {
ERROR("Unable to open font\n");
return;
return nullptr;
}
return new Font(font);
}
Font::Font(TTF_Font *font)
: font(font)
{
fontheight = TTF_FontHeight(font);
}

View File

@ -14,7 +14,11 @@ public:
enum HAlign { HAlignLeft, HAlignRight, HAlignCenter };
enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle };
Font(const std::string &font);
/**
* Returns a newly created Font object for the default font,
* or nullptr if there was a problem creating it.
*/
static Font *defaultFont();
~Font();
int getTextWidth(const char *text);
@ -34,6 +38,8 @@ public:
HAlign halign = HAlignLeft, VAlign valign = VAlignTop);
private:
Font(TTF_Font *font);
void writeLine(Surface *surface, const char *text,
int x, int y, HAlign halign, VAlign valign);

View File

@ -356,18 +356,13 @@ void GMenu2X::initBG() {
}
void GMenu2X::initFont() {
if (font != NULL) {
delete font;
font = NULL;
}
string fontFile = sc.getSkinFilePath("imgs/font.png");
if (fontFile.empty()) {
ERROR("Font png not found!\n");
delete font;
font = Font::defaultFont();
if (!font) {
ERROR("Cannot function without font; aborting...\n");
quit();
exit(-1);
}
font = new Font(fontFile);
}
void GMenu2X::initMenu() {