fixed loading custom PCX image files with default artwork file names
[rocksndiamonds.git] / src / libgame / misc.c
index 78dddf8aac39dc4b4ca62e7136e8b1daff56a9f1..259fb4dede6a1f3a41bf0fcf34e3cffffafa8d0d 100644 (file)
@@ -387,9 +387,9 @@ static char *get_corrected_real_name(char *real_name)
     if (*from_ptr == ',')
       break;
 
-    /* the user's real name may contain 'ß' characters (german sharp s),
+    /* the user's real name may contain 'german sharp s' characters,
        which have no equivalent in upper case letters (used by our fonts) */
-    if (*from_ptr == 'ß')
+    if (*from_ptr == CHAR_BYTE_SHARP_S)
     {
       from_ptr++;
       *to_ptr++ = 's';
@@ -606,6 +606,48 @@ char *getPath3(char *path1, char *path2, char *path3)
   return getStringCat3WithSeparator(path1, path2, path3, STRING_PATH_SEPARATOR);
 }
 
+char *getImg2(char *path1, char *path2)
+{
+  char *filename = getPath2(path1, path2);
+
+  if (!fileExists(filename) && strSuffix(path2, ".png"))
+  {
+    // backward compatibility: if PNG file not found, check for PCX file
+    char *path2pcx = getStringCopy(path2);
+
+    strcpy(&path2pcx[strlen(path2pcx) - 3], "pcx");
+
+    free(filename);
+
+    filename = getPath2(path1, path2pcx);
+
+    free(path2pcx);
+  }
+
+  return filename;
+}
+
+char *getImg3(char *path1, char *path2, char *path3)
+{
+  char *filename = getPath3(path1, path2, path3);
+
+  if (!fileExists(filename) && strSuffix(path3, ".png"))
+  {
+    // backward compatibility: if PNG file not found, check for PCX file
+    char *path3pcx = getStringCopy(path3);
+
+    strcpy(&path3pcx[strlen(path3pcx) - 3], "pcx");
+
+    free(filename);
+
+    filename = getPath3(path1, path2, path3pcx);
+
+    free(path3pcx);
+  }
+
+  return filename;
+}
+
 char *getStringCopy(const char *s)
 {
   char *s_copy;
@@ -760,7 +802,6 @@ void GetOptions(char *argv[],
   options.network = FALSE;
   options.verbose = FALSE;
   options.debug = FALSE;
-  options.debug_x11_sync = FALSE;
 
 #if 1
   options.verbose = TRUE;
@@ -888,10 +929,6 @@ void GetOptions(char *argv[],
     {
       options.debug = TRUE;
     }
-    else if (strncmp(option, "-debug-x11-sync", option_len) == 0)
-    {
-      options.debug_x11_sync = TRUE;
-    }
     else if (strncmp(option, "-verbose", option_len) == 0)
     {
       options.verbose = TRUE;
@@ -1010,6 +1047,9 @@ void Error(int mode, char *format, ...)
     if (mode & ERR_WARN)
       fprintf_nonewline(program.error_file, "warning: ");
 
+    if (mode & ERR_EXIT)
+      fprintf_nonewline(program.error_file, "fatal error: ");
+
     va_start(ap, format);
     vfprintf_newline(program.error_file, format, ap);
     va_end(ap);
@@ -1418,14 +1458,14 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
     { KSYM_braceright, "XK_braceright",        "brace right" },
     { KSYM_asciitilde, "XK_asciitilde",        "~" },
 
-    /* special (non-ASCII) keys (ISO-Latin-1) */
-    { KSYM_degree,     "XK_degree",            "°" },
-    { KSYM_Adiaeresis, "XK_Adiaeresis",        "Ä" },
-    { KSYM_Odiaeresis, "XK_Odiaeresis",        "Ö" },
-    { KSYM_Udiaeresis, "XK_Udiaeresis",        "Ü" },
-    { KSYM_adiaeresis, "XK_adiaeresis",        "ä" },
-    { KSYM_odiaeresis, "XK_odiaeresis",        "ö" },
-    { KSYM_udiaeresis, "XK_udiaeresis",        "ü" },
+    /* special (non-ASCII) keys */
+    { KSYM_degree,     "XK_degree",            "degree" },
+    { KSYM_Adiaeresis, "XK_Adiaeresis",        "A umlaut" },
+    { KSYM_Odiaeresis, "XK_Odiaeresis",        "O umlaut" },
+    { KSYM_Udiaeresis, "XK_Udiaeresis",        "U umlaut" },
+    { KSYM_adiaeresis, "XK_adiaeresis",        "a umlaut" },
+    { KSYM_odiaeresis, "XK_odiaeresis",        "o umlaut" },
+    { KSYM_udiaeresis, "XK_udiaeresis",        "u umlaut" },
     { KSYM_ssharp,     "XK_ssharp",            "sharp s" },
 
 #if defined(TARGET_SDL2)
@@ -1699,6 +1739,26 @@ Key getKeyFromX11KeyName(char *x11name)
 
 char getCharFromKey(Key key)
 {
+  static struct
+  {
+    Key key;
+    byte key_char;
+  } translate_key_char[] =
+  {
+    /* special (non-ASCII) keys (ISO-8859-1) */
+    { KSYM_degree,     CHAR_BYTE_DEGREE        },
+    { KSYM_Adiaeresis, CHAR_BYTE_UMLAUT_A      },
+    { KSYM_Odiaeresis, CHAR_BYTE_UMLAUT_O      },
+    { KSYM_Udiaeresis, CHAR_BYTE_UMLAUT_U      },
+    { KSYM_adiaeresis, CHAR_BYTE_UMLAUT_a      },
+    { KSYM_odiaeresis, CHAR_BYTE_UMLAUT_o      },
+    { KSYM_udiaeresis, CHAR_BYTE_UMLAUT_u      },
+    { KSYM_ssharp,     CHAR_BYTE_SHARP_S       },
+
+    /* end-of-array identifier */
+    { 0,                0                      }
+  };
+
   char *keyname = getKeyNameFromKey(key);
   char c = 0;
 
@@ -1706,6 +1766,21 @@ char getCharFromKey(Key key)
     c = keyname[0];
   else if (strEqual(keyname, "space"))
     c = ' ';
+  else
+  {
+    int i = 0;
+
+    do
+    {
+      if (key == translate_key_char[i].key)
+      {
+       c = translate_key_char[i].key_char;
+
+       break;
+      }
+    }
+    while (translate_key_char[++i].key_char);
+  }
 
   return c;
 }
@@ -2176,7 +2251,9 @@ boolean directoryExists(char *dir_name)
   if (dir_name == NULL)
     return FALSE;
 
-  boolean success = (access(dir_name, F_OK) == 0);
+  struct stat file_status;
+  boolean success = (stat(dir_name, &file_status) == 0 &&
+                    (file_status.st_mode & S_IFMT) == S_IFDIR);
 
 #if defined(PLATFORM_ANDROID)
   if (!success)
@@ -2264,40 +2341,37 @@ boolean fileHasSuffix(char *basename, char *suffix)
   return FALSE;
 }
 
-static boolean FileCouldBeArtwork(char *basename)
+static boolean FileCouldBeArtwork(char *filename)
 {
+  char *basename = getBaseNamePtr(filename);
+
   return (!strEqual(basename, ".") &&
          !strEqual(basename, "..") &&
          !fileHasSuffix(basename, "txt") &&
-         !fileHasSuffix(basename, "conf"));
+         !fileHasSuffix(basename, "conf") &&
+         !directoryExists(filename));
 }
 
 boolean FileIsGraphic(char *filename)
 {
-  char *basename = getBaseNamePtr(filename);
-
-  return FileCouldBeArtwork(basename);
+  return FileCouldBeArtwork(filename);
 }
 
 boolean FileIsSound(char *filename)
 {
-  char *basename = getBaseNamePtr(filename);
-
-  return FileCouldBeArtwork(basename);
+  return FileCouldBeArtwork(filename);
 }
 
 boolean FileIsMusic(char *filename)
 {
-  char *basename = getBaseNamePtr(filename);
-
-  return FileCouldBeArtwork(basename);
+  return FileCouldBeArtwork(filename);
 }
 
-boolean FileIsArtworkType(char *basename, int type)
+boolean FileIsArtworkType(char *filename, int type)
 {
-  if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
-      (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
-      (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
+  if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(filename)) ||
+      (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(filename)) ||
+      (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(filename)))
     return TRUE;
 
   return FALSE;