[Tux4kids-commits] r1473 - in branches/commonification: tux4kids-common/trunk/t4kcommon tuxmath/trunk/src tuxtype/trunk/src

B. Luchen cheezmeister-guest at alioth.debian.org
Tue Sep 1 06:26:40 UTC 2009


Author: cheezmeister-guest
Date: 2009-09-01 06:26:38 +0000 (Tue, 01 Sep 2009)
New Revision: 1473

Modified:
   branches/commonification/tux4kids-common/trunk/t4kcommon/t4k-loaders.c
   branches/commonification/tux4kids-common/trunk/t4kcommon/tux4kids-common.h
   branches/commonification/tuxmath/trunk/src/setup.c
   branches/commonification/tuxtype/trunk/src/main.c
Log:
Added some more fault-tolerance for image loading


Modified: branches/commonification/tux4kids-common/trunk/t4kcommon/t4k-loaders.c
===================================================================
--- branches/commonification/tux4kids-common/trunk/t4kcommon/t4k-loaders.c	2009-08-31 21:03:40 UTC (rev 1472)
+++ branches/commonification/tux4kids-common/trunk/t4kcommon/t4k-loaders.c	2009-09-01 06:26:38 UTC (rev 1473)
@@ -43,15 +43,17 @@
 void            fit_in_rectangle(int* width, int* height, int max_width, int max_height);
 SDL_Surface*    set_format(SDL_Surface* img, int mode);
 sprite*         load_sprite(const char* name, int mode, int w, int h, bool proportional);
+char*           find_file(const char* base_name);
 
+//directories to search in for loaded files, in addition to common data dir (just one for now)
+static char app_prefix_path[1][PATH_MAX];
 
-
 int CheckFile(const char* file)
 {
   FILE* fp = NULL;
-
+                                                       
   if (!file)
-  {
+  {                                                 
     DEBUGMSG(debug_loaders, "CheckFile(): invalid char* argument!\n");
     return 0;
   }
@@ -69,8 +71,27 @@
   DEBUGMSG(debug_loaders, "CheckFile(): Unable to open '%s' as either FILE or DIR\n", file);
   return 0;
 }
+                            
+void AddDataPrefix(const char* path)
+{
+  strncpy(app_prefix_path, path, PATH_MAX);
+}
 
-
+/* Look for a file as an absolute path, then in 
+   potential install directories */
+char* find_file(const char* base_name)
+{
+  static char tmp_path[PATH_MAX];
+  if (CheckFile(base_name))
+    return base_name;
+  snprintf(tmp_path, PATH_MAX, "%s%s", app_prefix_path[0], base_name); 
+  if (CheckFile(tmp_path))
+    return tmp_path;
+  snprintf(tmp_path, PATH_MAX, "%s%s", COMMON_DATA_PREFIX, base_name); 
+  if (CheckFile(tmp_path))
+    return tmp_path;
+  return "";
+}
 #ifdef HAVE_RSVG
 
 /* Load a layer of SVG file and resize it to given dimensions.
@@ -273,7 +294,7 @@
   if(NULL == file_name)
   {
     DEBUGMSG(debug_loaders, "load_image(): file_name is NULL, exiting.\n");
-    return NULL;
+    return NULL;                    
   }
 
   /* run loader depending on file extension */
@@ -285,7 +306,7 @@
   if(strcmp(fn + fn_len - 4, ".svg"))
   {
     DEBUGMSG(debug_loaders, "load_image(): %s is not an SVG, loading using IMG_Load()\n", fn);
-    loaded_pic = IMG_Load(fn);
+    loaded_pic = IMG_Load(find_file(fn));
     is_svg = false;
     if (NULL == loaded_pic)
     {
@@ -309,7 +330,7 @@
       width = w;
       height = h;
     }
-    loaded_pic = load_svg(fn, width, height, NULL);
+    loaded_pic = load_svg(find_file(fn), width, height, NULL);
 #endif
 
     if(loaded_pic == NULL)
@@ -328,7 +349,7 @@
         DEBUGMSG(debug_loaders, "load_image(): Trying to load PNG equivalent of %s\n", fn);
         strcpy(fn + fn_len - 3, "png");
 
-        loaded_pic = IMG_Load(fn);
+        loaded_pic = IMG_Load(find_file(fn));
         is_svg = false;
       }
     }
@@ -343,10 +364,14 @@
     }
     /* If image was required, exit from program: */
     fprintf(stderr, "load_image(): ERROR could not load required graphics file %s\n", file_name);
-    fprintf(stderr, "%s", SDL_GetError() );
+    fprintf(stderr, "%s\n", SDL_GetError() );
+    //should do some cleanup first...
+    exit(EXIT_FAILURE);
     return NULL;
   }
-  else if(!is_svg && w > 0 && h > 0)
+  
+  //if we need to resize a loaded raster image
+  if(!is_svg && w > 0 && h > 0)
   {
     if(proportional)
     {

Modified: branches/commonification/tux4kids-common/trunk/t4kcommon/tux4kids-common.h
===================================================================
--- branches/commonification/tux4kids-common/trunk/t4kcommon/tux4kids-common.h	2009-08-31 21:03:40 UTC (rev 1472)
+++ branches/commonification/tux4kids-common/trunk/t4kcommon/tux4kids-common.h	2009-09-01 06:26:38 UTC (rev 1473)
@@ -89,6 +89,8 @@
   NUM_WIPES
 };
 
+//TODO most of these functions are documented...somewhere. That should end up
+//in this header eventually
 SDL_Surface*    GetScreen();
 void            DrawButton(SDL_Rect* target_rect, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
 void            DrawButtonOn(SDL_Surface* target, SDL_Rect* target_rect, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
@@ -132,6 +134,12 @@
 
 
 int             CheckFile(const char* file);
+/**
+ * Add a path to search for data (in addition to t4kcommon's install path and 
+ * the root directory) This affects how LoadImage and friends are able to
+ * find files.
+ */
+void            AddDataPrefix(const char* path);
 
 SDL_Surface*    LoadImage(const char* file_name, int mode);
 SDL_Surface*    LoadScaledImage(const char* file_name, int mode, int width, int height);

Modified: branches/commonification/tuxmath/trunk/src/setup.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/setup.c	2009-08-31 21:03:40 UTC (rev 1472)
+++ branches/commonification/tuxmath/trunk/src/setup.c	2009-09-01 06:26:38 UTC (rev 1473)
@@ -113,6 +113,7 @@
 
 #ifdef HAVE_LIBT4KCOMMON
   InitT4KCommon(debug_status);
+  AddDataPrefix(DATA_PREFIX);
 #endif
   /* SDL setup in own function:*/
   initialize_SDL();

Modified: branches/commonification/tuxtype/trunk/src/main.c
===================================================================
--- branches/commonification/tuxtype/trunk/src/main.c	2009-08-31 21:03:40 UTC (rev 1472)
+++ branches/commonification/tuxtype/trunk/src/main.c	2009-09-01 06:26:38 UTC (rev 1473)
@@ -216,6 +216,7 @@
 
 #ifdef HAVE_LIBT4KCOMMON
   InitT4KCommon(debug_status);
+  AddDataPrefix(DATA_PREFIX);
 #endif
 
   DEBUGMSG(debug_setup, "\n%s, version %s BEGIN\n", PACKAGE, VERSION);




More information about the Tux4kids-commits mailing list