rnd-20070324-1-src
[rocksndiamonds.git] / src / libgame / misc.c
index 41b68190559a73a7eae920ee3e1a472000197120..c06eac4f097f9e32f7031323b4613ba78aad1bf9 100644 (file)
@@ -156,6 +156,11 @@ int log_2(unsigned int x)
   return e;
 }
 
+boolean getTokenValueFromString(char *string, char **token, char **value)
+{
+  return getTokenValueFromSetupLine(string, token, value);
+}
+
 
 /* ------------------------------------------------------------------------- */
 /* counter functions                                                         */
@@ -579,6 +584,14 @@ boolean strEqual(char *s1, char *s2)
          strcmp(s1, s2) == 0);
 }
 
+boolean strEqualN(char *s1, char *s2, int n)
+{
+  return (s1 == NULL && s2 == NULL ? TRUE  :
+         s1 == NULL && s2 != NULL ? FALSE :
+         s1 != NULL && s2 == NULL ? FALSE :
+         strncmp(s1, s2, n) == 0);
+}
+
 
 /* ------------------------------------------------------------------------- */
 /* command line option handling functions                                    */
@@ -1782,7 +1795,15 @@ int get_parameter_value(char *value_raw, char *suffix, int type)
   {
     result = (strEqual(value, "left")   ? ALIGN_LEFT :
              strEqual(value, "right")  ? ALIGN_RIGHT :
-             strEqual(value, "center") ? ALIGN_CENTER : ALIGN_DEFAULT);
+             strEqual(value, "center") ? ALIGN_CENTER :
+             strEqual(value, "middle") ? ALIGN_CENTER : ALIGN_DEFAULT);
+  }
+  else if (strEqual(suffix, ".valign"))
+  {
+    result = (strEqual(value, "top")    ? VALIGN_TOP :
+             strEqual(value, "bottom") ? VALIGN_BOTTOM :
+             strEqual(value, "middle") ? VALIGN_MIDDLE :
+             strEqual(value, "center") ? VALIGN_MIDDLE : VALIGN_DEFAULT);
   }
   else if (strEqual(suffix, ".anim_mode"))
   {
@@ -1798,8 +1819,6 @@ int get_parameter_value(char *value_raw, char *suffix, int type)
              string_has_parameter(value, "horizontal") ? ANIM_HORIZONTAL :
              string_has_parameter(value, "vertical")   ? ANIM_VERTICAL :
              string_has_parameter(value, "centered")   ? ANIM_CENTERED :
-             string_has_parameter(value, "fade")       ? ANIM_FADE :
-             string_has_parameter(value, "crossfade")  ? ANIM_CROSSFADE :
              ANIM_DEFAULT);
 
     if (string_has_parameter(value, "reverse"))
@@ -1811,6 +1830,18 @@ int get_parameter_value(char *value_raw, char *suffix, int type)
     if (string_has_parameter(value, "static_panel"))
       result |= ANIM_STATIC_PANEL;
   }
+  else if (strEqual(suffix, ".fade_mode"))
+  {
+    result = (string_has_parameter(value, "none")      ? FADE_MODE_NONE :
+             string_has_parameter(value, "fade")       ? FADE_MODE_FADE :
+             string_has_parameter(value, "crossfade")  ? FADE_MODE_CROSSFADE :
+             string_has_parameter(value, "melt")       ? FADE_MODE_MELT :
+             FADE_MODE_DEFAULT);
+  }
+  else if (strEqualN(suffix, ".font", 5))      /* (may also be ".font_xyz") */
+  {
+    result = gfx.get_font_from_token_function(value);
+  }
   else         /* generic parameter of type integer or boolean */
   {
     result = (strEqual(value, ARG_UNDEFINED) ? ARG_UNDEFINED_VALUE :
@@ -1824,7 +1855,7 @@ int get_parameter_value(char *value_raw, char *suffix, int type)
   return result;
 }
 
-int get_auto_parameter_value(char *token, char *value_raw)
+int get_token_parameter_value(char *token, char *value_raw)
 {
   char *suffix;
 
@@ -1835,6 +1866,21 @@ int get_auto_parameter_value(char *token, char *value_raw)
   if (suffix == NULL)
     suffix = token;
 
+#if 0
+  if (strncmp(suffix, ".font", 5) == 0)
+  {
+    int i;
+
+    /* !!! OPTIMIZE THIS BY USING HASH !!! */
+    for (i = 0; i < NUM_FONTS; i++)
+      if (strEqual(value_raw, font_info[i].token_name))
+       return i;
+
+    /* if font not found, use reliable default value */
+    return FONT_INITIAL_1;
+  }
+#endif
+
   return get_parameter_value(value_raw, suffix, TYPE_INTEGER);
 }
 
@@ -2683,7 +2729,7 @@ static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
                              struct FileInfo *file_list_entry)
 {
 #if 0
-  printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename);
+  printf("GOT CUSTOM ARTWORK FILE '%s'\n", file_list_entry->filename);
 #endif
 
   if (strEqual(file_list_entry->filename, UNDEFINED_FILENAME))
@@ -2805,10 +2851,46 @@ void NotifyUserAboutErrorFile()
 
 #if DEBUG
 
-#define DEBUG_NUM_TIMESTAMPS   3
+#define DEBUG_NUM_TIMESTAMPS           3
+#define DEBUG_TIME_IN_MICROSECONDS     0
+
+#if DEBUG_TIME_IN_MICROSECONDS
+static double Counter_Microseconds()
+{
+  static struct timeval base_time = { 0, 0 };
+  struct timeval current_time;
+  double counter;
+
+  gettimeofday(&current_time, NULL);
+
+  /* reset base time in case of wrap-around */
+  if (current_time.tv_sec < base_time.tv_sec)
+    base_time = current_time;
+
+  counter =
+    ((double)(current_time.tv_sec  - base_time.tv_sec)) * 1000000 +
+    ((double)(current_time.tv_usec - base_time.tv_usec));
+
+  return counter;              /* return microseconds since last init */
+}
+#endif
 
 void debug_print_timestamp(int counter_nr, char *message)
 {
+#if DEBUG_TIME_IN_MICROSECONDS
+  static double counter[DEBUG_NUM_TIMESTAMPS][2];
+
+  if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
+    Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
+
+  counter[counter_nr][0] = Counter_Microseconds();
+
+  if (message)
+    printf("%s %.3f ms\n", message,
+          (counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
+
+  counter[counter_nr][1] = counter[counter_nr][0];
+#else
   static long counter[DEBUG_NUM_TIMESTAMPS][2];
 
   if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
@@ -2817,10 +2899,11 @@ void debug_print_timestamp(int counter_nr, char *message)
   counter[counter_nr][0] = Counter();
 
   if (message)
-    printf("%s %.3f seconds\n", message,
+    printf("%s %.3f s\n", message,
           (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
 
   counter[counter_nr][1] = counter[counter_nr][0];
+#endif
 }
 
 void debug_print_parent_only(char *format, ...)