Do not crash due to a missing font
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Sun, 1 Aug 2010 18:21:57 +0000 (18:21 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Sun, 1 Aug 2010 18:21:57 +0000 (18:21 +0000)
git-svn-id: https://s.snth.net/svn/neverball/trunk@3228 78b8d119-cf0a-0410-b17c-f493084dd1d7

share/gui.c
share/image.c

index 4dedee0..c758574 100644 (file)
@@ -265,26 +265,32 @@ void gui_init(void)
 
         /* Load the font. */
 
-        if (!(fontdata = fs_load(fontpath, &fontdatalen)))
+        if ((fontdata = fs_load(fontpath, &fontdatalen)))
         {
-            fprintf(stderr, L_("Could not load font '%s'.\n"), fontpath);
-            /* Return or no return, we'll probably crash now. */
-            return;
-        }
+            fontrwops = SDL_RWFromConstMem(fontdata, fontdatalen);
+
+            /* Load small, medium, and large typefaces. */
 
-        fontrwops = SDL_RWFromConstMem(fontdata, fontdatalen);
+            font[GUI_SML] = TTF_OpenFontRW(fontrwops, 0, s0);
 
-        /* Load small, medium, and large typefaces. */
+            SDL_RWseek(fontrwops, 0, SEEK_SET);
+            font[GUI_MED] = TTF_OpenFontRW(fontrwops, 0, s1);
 
-        font[GUI_SML] = TTF_OpenFontRW(fontrwops, 0, s0);
+            SDL_RWseek(fontrwops, 0, SEEK_SET);
+            font[GUI_LRG] = TTF_OpenFontRW(fontrwops, 0, s2);
 
-        SDL_RWseek(fontrwops, 0, SEEK_SET);
-        font[GUI_MED] = TTF_OpenFontRW(fontrwops, 0, s1);
+            /* fontrwops remains open. */
+        }
+        else
+        {
+            fontrwops = NULL;
 
-        SDL_RWseek(fontrwops, 0, SEEK_SET);
-        font[GUI_LRG] = TTF_OpenFontRW(fontrwops, 0, s2);
+            font[GUI_SML] = NULL;
+            font[GUI_MED] = NULL;
+            font[GUI_LRG] = NULL;
 
-        /* fontrwops remains open. */
+            fprintf(stderr, L_("Could not load font '%s'.\n"), fontpath);
+        }
 
         radius = s / 60;
 
@@ -445,7 +451,10 @@ struct size
 static struct size gui_measure(const char *text, TTF_Font *font)
 {
     struct size size = { 0, 0 };
-    TTF_SizeUTF8(font, text, &size.w, &size.h);
+
+    if (font)
+        TTF_SizeUTF8(font, text, &size.w, &size.h);
+
     return size;
 }
 
index b1566bf..b98d903 100644 (file)
@@ -202,7 +202,7 @@ GLuint make_image_from_font(int *W, int *H,
 
     /* Render the text. */
 
-    if (text && strlen(text) > 0)
+    if (font && text && strlen(text) > 0)
     {
         SDL_Color    col = { 0xFF, 0xFF, 0xFF, 0xFF };
         SDL_Surface *orig;