rnd-20020531-1-src
authorHolger Schemel <info@artsoft.org>
Fri, 31 May 2002 15:41:42 +0000 (17:41 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 30 Aug 2014 08:37:51 +0000 (10:37 +0200)
CHANGES
src/libgame/image.c
src/libgame/misc.c
src/libgame/misc.h
src/libgame/setup.c
src/libgame/system.h
src/libgame/x11.c
src/timestamp.h

diff --git a/CHANGES b/CHANGES
index 433c604801a9bf1c59e9d7b7fc8fdc218ac58b10..1e79605f975be121195df4f06cc47ae0175393d1 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,6 @@
 Release Version 2.1.0 [XX XXX XXXX]
 -----------------------------------
+       - fixed memory leak in image loading code
        - added Boulderdash style "snap-pushing" (thanks to Achim Härtel)
        - bug fixed that prevented walking through tubes when gravity on
        - enhanced sound system (especially regarding stereo and loop sounds)
index 9b89100b75548709fcc6ec8b2df7966c9250efea..62b8fed355c5cd9bbf7931c3f53bcb5d0bd9b7fa 100644 (file)
@@ -245,6 +245,7 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
       bluestep = 256 / bluecolors;
       redbottom = greenbottom = bluebottom = 0;
       redtop = greentop = bluetop = 0;
+
       for (a=0; a<visual->map_entries; a++)
       {
        if (redbottom < 256)
@@ -275,10 +276,12 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
          /* something completely unexpected happened */
 
          fprintf(stderr, "Image_to_Pixmap: XAllocColor failed on a TrueColor/Directcolor visual\n");
+
           free(redvalue);
           free(greenvalue);
           free(bluevalue);
           free(ximageinfo);
+
          return NULL;
        }
 
@@ -291,6 +294,7 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
        while ((bluebottom < 256) && (bluebottom < bluetop))
          bluevalue[bluebottom++] = xcolor.pixel & visual->blue_mask;
       }
+
       break;
     }
 
@@ -421,6 +425,7 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
     default:
       Error(ERR_RETURN,"DirectColor, TrueColor or PseudoColor display needed");
       SetError(error, "display class not supported");
+
       return NULL;
   }
 
@@ -467,6 +472,7 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
              dst_ptr += display_bytes_per_pixel;
            }
          }
+
          break;
        }
 
@@ -486,14 +492,17 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
              dst_ptr += display_bytes_per_pixel;
            }
          }
+
          break;
        }
 
         default:
          Error(ERR_RETURN, "RGB or TrueColor image needed");
          SetError(error, "image type not supported");
+
          return NULL;
       }
+
       break;
     }
 
@@ -517,12 +526,14 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
          }
        }
       }
+
       break;
     }
 
     default:
       Error(ERR_RETURN,"DirectColor, TrueColor or PseudoColor display needed");
       SetError(error, "display class not supported");
+
       return NULL;
   }
 
@@ -544,11 +555,9 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
   XPutImage(ximageinfo->display, ximageinfo->pixmap, gc,
            ximage, 0, 0, 0, 0, ximage->width, ximage->height);
 
-  free(ximage->data);
-  ximage->data = NULL;
   XDestroyImage(ximage);
 
-  return(ximageinfo);
+  return ximageinfo;
 }
 
 void freeXImage(Image *image, XImageInfo *ximageinfo)
@@ -593,7 +602,11 @@ int Read_PCX_to_Pixmap(Display *display, Window window, GC gc, char *filename,
   /* convert image structure to X11 Pixmap */
   if (!(ximageinfo = Image_to_Pixmap(display, screen, visual,
                                     window, gc, depth, image)))
+  {
+    freeImage(image);
+
     return PCX_OtherError;
+  }
 
   /* if a private colormap has been created, install it */
   if (ximageinfo->cmap != DefaultColormap(display, screen))
@@ -613,6 +626,10 @@ int Read_PCX_to_Pixmap(Display *display, Window window, GC gc, char *filename,
   *pixmap = ximageinfo->pixmap;
   *pixmap_mask = ximageinfo->pixmap_mask;
 
+  /* free generic image and ximageinfo after native Pixmap has been created */
+  free(ximageinfo);
+  freeImage(image);
+
   return PCX_Success;
 }
 
index c59749ff92e94c71888d2dad04dc23d6d76df1cf..b344ba6f2a9e65365ea36b4fe7c14359b13c9aeb 100644 (file)
@@ -1206,6 +1206,17 @@ boolean FileIsMusic(char *basename)
   return FALSE;
 }
 
+boolean FileIsArtworkType(char *basename, int type)
+{
+  if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
+      (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
+      (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
+    return TRUE;
+
+  return FALSE;
+}
+
+
 /* ========================================================================= */
 /* functions only needed for non-Unix (non-command-line) systems */
 /* ========================================================================= */
index 3dd380b1d8787a6f9f7fa92038daf09d50bd10ad..499bd4252a44ad04a44ac5f9254bd0982688b286 100644 (file)
@@ -114,6 +114,7 @@ char getCharFromKey(Key);
 boolean FileIsGraphic(char *);
 boolean FileIsSound(char *);
 boolean FileIsMusic(char *);
+boolean FileIsArtworkType(char *, int);
 
 #if !defined(PLATFORM_UNIX)
 void initErrorFile();
index 57ba732a18e2dcdeecbd253e9b46540add7ed35c..6ad11351d1dca59eea56a49bb080f0dd5a7dd915 100644 (file)
@@ -84,6 +84,27 @@ static char *levelclass_desc[NUM_LEVELCLASS_DESC] =
 
 #define MAX_COOKIE_LEN                 256
 
+#define ARTWORKINFO_FILENAME(type)     ((type) == TREE_TYPE_GRAPHICS_DIR ? \
+                                        GRAPHICSINFO_FILENAME :            \
+                                        (type) == TREE_TYPE_SOUNDS_DIR ?   \
+                                        SOUNDSINFO_FILENAME :              \
+                                        (type) == TREE_TYPE_MUSIC_DIR ?    \
+                                        MUSICINFO_FILENAME : "")
+
+#define ARTWORK_DIRECTORY(type)                ((type) == TREE_TYPE_GRAPHICS_DIR ? \
+                                        GRAPHICS_DIRECTORY :               \
+                                        (type) == TREE_TYPE_SOUNDS_DIR ?   \
+                                        SOUNDS_DIRECTORY :                 \
+                                        (type) == TREE_TYPE_MUSIC_DIR ?    \
+                                        MUSIC_DIRECTORY : "")
+
+#define OPTIONS_ARTWORK_DIRECTORY(type)        ((type) == TREE_TYPE_GRAPHICS_DIR ? \
+                                        options.graphics_directory :       \
+                                        (type) == TREE_TYPE_SOUNDS_DIR ?   \
+                                        options.sounds_directory :         \
+                                        (type) == TREE_TYPE_MUSIC_DIR ?    \
+                                        options.music_directory : "")
+
 
 /* ------------------------------------------------------------------------- */
 /* file functions                                                            */
@@ -167,23 +188,27 @@ static char *getLevelSetupDir(char *level_subdir)
   return levelsetup_dir;
 }
 
-static char *getCurrentLevelDir()
+static char *getLevelDirFromTreeInfo(TreeInfo *node)
 {
   static char *level_dir = NULL;
 
+  if (node == NULL)
+    return options.level_directory;
+
   if (level_dir)
     free(level_dir);
 
-  if (leveldir_current == NULL)
-    return options.level_directory;
-
-  level_dir = getPath2((leveldir_current->user_defined ?
-                       getUserLevelDir(NULL) : options.level_directory),
-                      leveldir_current->fullpath);
+  level_dir = getPath2((node->user_defined ? getUserLevelDir(NULL) :
+                       options.level_directory), node->fullpath);
 
   return level_dir;
 }
 
+static char *getCurrentLevelDir()
+{
+  return getLevelDirFromTreeInfo(leveldir_current);
+}
+
 static char *getDefaultGraphicsDir(char *graphics_subdir)
 {
   static char *graphics_dir = NULL;
@@ -1590,14 +1615,9 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
                                              char *directory_name, int type)
 {
   char *directory_path = getPath2(base_directory, directory_name);
-  char *filename =
-    getPath2(directory_path,
-            (type == TREE_TYPE_GRAPHICS_DIR ? GRAPHICSINFO_FILENAME :
-             type == TREE_TYPE_SOUNDS_DIR ? SOUNDSINFO_FILENAME :
-             type == TREE_TYPE_MUSIC_DIR ? MUSICINFO_FILENAME : ""));
+  char *filename = getPath2(directory_path, ARTWORKINFO_FILENAME(type));
   struct SetupFileList *setup_file_list = NULL;
   TreeInfo *artwork_new = NULL;
-  char *check_dir = NULL;
   int i;
 
   if (access(filename, F_OK) == 0)             /* file exists */
@@ -1615,9 +1635,7 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
       {
        char *entry_name = dir_entry->d_name;
 
-       if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(entry_name)) ||
-           (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(entry_name)) ||
-           (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(entry_name)))
+       if (FileIsArtworkType(entry_name, type))
        {
          valid_file_found = TRUE;
          break;
@@ -1687,11 +1705,8 @@ static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
     artwork_new->fullpath = getPath2(node_parent->fullpath, directory_name);
   }
 
-  check_dir = (type == TREE_TYPE_GRAPHICS_DIR ? options.graphics_directory :
-              type == TREE_TYPE_SOUNDS_DIR ? options.sounds_directory :
-              type == TREE_TYPE_MUSIC_DIR ? options.music_directory : "");
   artwork_new->user_defined =
-    (artwork_new->basepath == check_dir ? FALSE : TRUE);
+    (artwork_new->basepath == OPTIONS_ARTWORK_DIRECTORY(type) ? FALSE : TRUE);
 
   /* (may use ".sort_priority" from "setup_file_list" above) */
   artwork_new->color = LEVELCOLOR(artwork_new);
@@ -1745,12 +1760,7 @@ static void LoadArtworkInfoFromArtworkDir(TreeInfo **node_first,
 
   if ((dir = opendir(base_directory)) == NULL)
   {
-    if ((type == TREE_TYPE_GRAPHICS_DIR &&
-        base_directory == options.graphics_directory) ||
-       (type == TREE_TYPE_SOUNDS_DIR &&
-        base_directory == options.sounds_directory) ||
-       (type == TREE_TYPE_MUSIC_DIR &&
-        base_directory == options.music_directory))
+    if (base_directory == OPTIONS_ARTWORK_DIRECTORY(type))
       Error(ERR_WARN, "cannot read directory '%s'", base_directory);
     return;
   }
@@ -1858,53 +1868,50 @@ void LoadArtworkInfo()
 #endif
 }
 
-void LoadArtworkInfoFromLevelInfo(TreeInfo *node)
+void LoadArtworkInfoFromLevelInfo(ArtworkDirTree **artwork_node,
+                                 LevelDirTree *level_node)
 {
-  while (node)
+  /* recursively check all level directories for artwork sub-directories */
+
+  while (level_node)
   {
-    char *path = getPath3((node->user_defined ?
-                          getUserLevelDir(NULL) : options.level_directory),
-                         node->fullpath, SOUNDS_DIRECTORY);
+    char *path = getPath2(getLevelDirFromTreeInfo(level_node),
+                         ARTWORK_DIRECTORY((*artwork_node)->type));
 
 #if 0
-    if (!node->parent_link)
+    if (!level_node->parent_link)
       printf("CHECKING '%s' ['%s', '%s'] ...\n", path,
-            node->filename, node->name);
+            level_node->filename, level_node->name);
 #endif
 
-    if (!node->parent_link)
+    if (!level_node->parent_link)
     {
-      TreeInfo *topnode_last = artwork.snd_first;
+      TreeInfo *topnode_last = *artwork_node;
 
-      LoadArtworkInfoFromArtworkDir(&artwork.snd_first, NULL,
-                                   path,
-                                   TREE_TYPE_SOUNDS_DIR);
+      LoadArtworkInfoFromArtworkDir(artwork_node, NULL, path,
+                                   (*artwork_node)->type);
 
-      if (topnode_last != artwork.snd_first)
+      if (topnode_last != *artwork_node)
       {
-#if 0
-       printf("NEW NODE: '%s'\n", artwork.snd_first->name);
-#endif
-
-       free(artwork.snd_first->name);
-       free(artwork.snd_first->name_sorting);
-       free(artwork.snd_first->name_short);
+       free((*artwork_node)->name);
+       free((*artwork_node)->name_sorting);
+       free((*artwork_node)->name_short);
 
-       artwork.snd_first->name         = getStringCopy(node->name);
-       artwork.snd_first->name_sorting = getStringCopy(node->name);
-       artwork.snd_first->name_short   = getStringCopy(node->filename);
+       (*artwork_node)->name         = getStringCopy(level_node->name);
+       (*artwork_node)->name_sorting = getStringCopy(level_node->name);
+       (*artwork_node)->name_short   = getStringCopy(level_node->filename);
 
-       artwork.snd_first->sort_priority = node->sort_priority;
-       artwork.snd_first->color = LEVELCOLOR(artwork.snd_first);
+       (*artwork_node)->sort_priority = level_node->sort_priority;
+       (*artwork_node)->color = LEVELCOLOR((*artwork_node));
       }
     }
 
     free(path);
 
-    if (node->node_group != NULL)
-      LoadArtworkInfoFromLevelInfo(node->node_group);
+    if (level_node->node_group != NULL)
+      LoadArtworkInfoFromLevelInfo(artwork_node, level_node->node_group);
 
-    node = node->next;
+    level_node = level_node->next;
   }
 }
 
@@ -1912,12 +1919,18 @@ void LoadLevelArtworkInfo()
 {
   DrawInitText("Looking for custom level artwork:", 120, FC_GREEN);
 
-  LoadArtworkInfoFromLevelInfo(leveldir_first);
+  LoadArtworkInfoFromLevelInfo(&artwork.gfx_first, leveldir_first);
+  LoadArtworkInfoFromLevelInfo(&artwork.snd_first, leveldir_first);
+  LoadArtworkInfoFromLevelInfo(&artwork.mus_first, leveldir_first);
 
+  sortTreeInfo(&artwork.gfx_first, compareTreeInfoEntries);
   sortTreeInfo(&artwork.snd_first, compareTreeInfoEntries);
+  sortTreeInfo(&artwork.mus_first, compareTreeInfoEntries);
 
 #if 1
+  dumpTreeInfo(artwork.gfx_first, 0);
   dumpTreeInfo(artwork.snd_first, 0);
+  dumpTreeInfo(artwork.mus_first, 0);
 #endif
 }
 
index 1a130ee0843c6fa9d22c91c1eadbbce2b476400e..b21f866cfa4cbec0cc7203a69e07a669f5d1675b 100644 (file)
@@ -381,6 +381,7 @@ struct TreeInfo
 
 typedef struct TreeInfo TreeInfo;
 typedef struct TreeInfo LevelDirTree;
+typedef struct TreeInfo ArtworkDirTree;
 typedef struct TreeInfo GraphicsDirTree;
 typedef struct TreeInfo SoundsDirTree;
 typedef struct TreeInfo MusicDirTree;
index 394350a6f17f33768a1efbc056662178c002e7f1..7d45a493a28b771e3a4c6bb3dc1183b92aa9d7b2 100644 (file)
@@ -255,10 +255,6 @@ Bitmap *X11LoadImage(char *filename)
   char *error = "Read_PCX_to_Pixmap(): %s '%s'";
   int pcx_err;
 
-#if defined(PLATFORM_MSDOS)
-  rest(100);
-#endif
-
   pcx_err = Read_PCX_to_Pixmap(display, window->drawable, window->gc, filename,
                               &new_bitmap->drawable, &new_bitmap->clip_mask);
   switch(pcx_err)
index 3dd66bf70c29fe25864a86a9c419877a0a62b2f4..062fe59a5a1ce893e232ef7912d079ea5841ce47 100644 (file)
@@ -1 +1 @@
-#define COMPILE_DATE_STRING "[2002-05-27 23:07]"
+#define COMPILE_DATE_STRING "[2002-05-31 17:39]"