rnd-20030118-1-src
[rocksndiamonds.git] / src / libgame / image.c
index 9b89100b75548709fcc6ec8b2df7966c9250efea..ce66e3280b43eaed02485317de4edeff0d7e5aef 100644 (file)
@@ -1,7 +1,7 @@
 /***********************************************************
 * Artsoft Retro-Game Library                               *
 *----------------------------------------------------------*
-* (c) 1994-2001 Artsoft Entertainment                      *
+* (c) 1994-2002 Artsoft Entertainment                      *
 *               Holger Schemel                             *
 *               Detmolder Strasse 189                      *
 *               33604 Bielefeld                            *
 #include "image.h"
 #include "pcx.h"
 #include "misc.h"
+#include "setup.h"
 
 
+/* ========================================================================= */
+/* PLATFORM SPECIFIC IMAGE FUNCTIONS                                         */
+/* ========================================================================= */
+
 #if defined(TARGET_X11)
 
 /* for MS-DOS/Allegro, exclude all except newImage() and freeImage() */
@@ -33,8 +38,8 @@ Image *newImage(unsigned int width, unsigned int height, unsigned int depth)
   depth = 8;
 #endif
 
-  image = checked_malloc(sizeof(Image));
-  image->data = checked_malloc(width * height * bytes_per_pixel);
+  image = checked_calloc(sizeof(Image));
+  image->data = checked_calloc(width * height * bytes_per_pixel);
   image->width = width;
   image->height = height;
   image->depth = depth;
@@ -245,6 +250,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 +281,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 +299,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 +430,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 +477,7 @@ XImageInfo *Image_to_Pixmap(Display *display, int screen, Visual *visual,
              dst_ptr += display_bytes_per_pixel;
            }
          }
+
          break;
        }
 
@@ -486,14 +497,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 +531,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 +560,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 +607,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,8 +631,135 @@ 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;
 }
 
 #endif /* PLATFORM_UNIX */
 #endif /* TARGET_X11 */
+
+
+/* ========================================================================= */
+/* PLATFORM INDEPENDANT IMAGE FUNCTIONS                                      */
+/* ========================================================================= */
+
+struct ImageInfo
+{
+  char *source_filename;
+  int num_references;
+
+  Bitmap *bitmap;
+};
+typedef struct ImageInfo ImageInfo;
+
+static struct ArtworkListInfo *image_info = NULL;
+
+static void *Load_PCX(char *filename)
+{
+  ImageInfo *img_info;
+
+#if 0
+  printf("loading PCX file '%s'\n", filename);
+#endif
+
+  img_info = checked_calloc(sizeof(ImageInfo));
+
+  if ((img_info->bitmap = LoadImage(filename)) == NULL)
+  {
+    Error(ERR_WARN, "cannot read image file '%s': LoadImage() failed: %s",
+         filename, GetError());
+    free(img_info);
+    return NULL;
+  }
+
+  img_info->source_filename = getStringCopy(filename);
+
+  return img_info;
+}
+
+static void FreeImage(void *ptr)
+{
+  ImageInfo *image = (ImageInfo *)ptr;
+
+  if (image == NULL)
+    return;
+
+  if (image->bitmap)
+    FreeBitmap(image->bitmap);
+
+  if (image->source_filename)
+    free(image->source_filename);
+
+  free(image);
+}
+
+struct FileInfo *getCurrentImageList()
+{
+  return image_info->file_list;
+}
+
+Bitmap *getBitmapFromImageID(int graphic)
+{
+  ImageInfo **img_info = (ImageInfo **)image_info->artwork_list;
+
+  return (img_info[graphic] != NULL ? img_info[graphic]->bitmap : NULL);
+}
+
+char *getTokenFromImageID(int graphic)
+{
+  struct FileInfo *file_list = (struct FileInfo *)image_info->file_list;
+
+  return file_list[graphic].token;
+}
+
+char *getImageConfigFilename()
+{
+  return getCustomArtworkConfigFilename(image_info->type);
+}
+
+void InitImageList(struct ConfigInfo *config_list,
+                  struct ConfigInfo *config_suffix_list,
+                  int num_file_list_entries)
+{
+  int i;
+
+  image_info = checked_calloc(sizeof(struct ArtworkListInfo));
+
+  image_info->type = ARTWORK_TYPE_GRAPHICS;
+
+  image_info->num_file_list_entries = num_file_list_entries;
+  image_info->num_suffix_list_entries = 0;
+  for (i=0; config_suffix_list[i].token != NULL; i++)
+    image_info->num_suffix_list_entries++;
+
+  image_info->file_list =
+    getFileListFromConfigList(config_list, config_suffix_list,
+                             num_file_list_entries);
+  image_info->suffix_list = config_suffix_list;
+  image_info->custom_setup_list = NULL;
+
+  image_info->artwork_list =
+    checked_calloc(num_file_list_entries * sizeof(ImageInfo *));
+
+  image_info->content_list = NULL;
+
+  image_info->load_artwork = Load_PCX;
+  image_info->free_artwork = FreeImage;
+}
+
+void ReloadCustomImages()
+{
+#if 0
+  printf("DEBUG: reloading images '%s' ...\n", artwork.gfx_current_identifier);
+#endif
+
+  ReloadCustomArtworkList(image_info);
+}
+
+void FreeAllImages()
+{
+  FreeCustomArtworkList(image_info);
+}