mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-25 20:25:54 +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:
parent
724aefe482
commit
f8dc4c7bb8
14
src/font.cpp
14
src/font.cpp
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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");
|
||||
font = Font::defaultFont();
|
||||
if (!font) {
|
||||
ERROR("Cannot function without font; aborting...\n");
|
||||
quit();
|
||||
exit(-1);
|
||||
}
|
||||
font = new Font(fontFile);
|
||||
}
|
||||
|
||||
void GMenu2X::initMenu() {
|
||||
|
Loading…
Reference in New Issue
Block a user