+int WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay)
+{
+ unsigned int actual_counter;
+ int skip_frames = 0;
+
+ while (1)
+ {
+ actual_counter = Counter();
+
+ if (actual_counter >= *counter_var &&
+ actual_counter < *counter_var + delay)
+ sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
+ else
+ break;
+ }
+
+ if (*counter_var != 0 &&
+ delay != 0 &&
+ actual_counter >= *counter_var + delay)
+ {
+ int lag = actual_counter - (*counter_var + delay);
+ int delay2 = (delay + 1) / 2;
+
+ if (lag >= delay2)
+ skip_frames = (lag + delay2) / delay;
+ }
+
+ *counter_var = actual_counter;
+
+ return skip_frames;
+}
+
+void SkipUntilDelayReached(unsigned int *counter_var, unsigned int delay,
+ int *loop_var, int last_loop_value)
+{
+ int skip_frames = WaitUntilDelayReached(counter_var, delay);
+
+#if 0
+#if DEBUG
+ printf("::: %d: %d ms", *loop_var, delay);
+ if (skip_frames)
+ printf(" -> SKIP %d FRAME(S) [%d ms]", skip_frames, skip_frames * delay);
+ printf("\n");
+#endif
+#endif
+
+ if (skip_frames == 0)
+ return;
+
+ // when skipping frames, make sure to never skip the last frame, as
+ // this may be needed for animations to reach a defined end state;
+ // furthermore, we assume that this function is called at the end
+ // of a "for" loop, which continues by incrementing the loop variable
+ // by one before checking the loop condition again; therefore we have
+ // to check against the last loop value minus one here
+
+ last_loop_value--;
+
+ if (*loop_var < last_loop_value) // never skip the last frame
+ {
+ *loop_var += skip_frames;
+
+ if (*loop_var > last_loop_value) // never skip the last frame
+ *loop_var = last_loop_value;
+ }
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* random generator functions */
+/* ------------------------------------------------------------------------- */
+
+unsigned int init_random_number(int nr, int seed)
+{
+ if (seed == NEW_RANDOMIZE)
+ {
+ /* default random seed */
+ seed = (int)time(NULL); // seconds since the epoch
+
+#if !defined(PLATFORM_WIN32)
+ /* add some more randomness */
+ struct timeval current_time;
+
+ gettimeofday(¤t_time, NULL);
+
+ seed += (int)current_time.tv_usec; // microseconds since the epoch
+#endif
+
+ /* add some more randomness */
+ seed += (int)SDL_GetTicks(); // milliseconds since SDL init
+
+ /* add some more randomness */
+ seed += GetSimpleRandom(1000000);
+ }
+
+ srandom_linux_libc(nr, (unsigned int) seed);
+
+ return (unsigned int) seed;
+}
+
+unsigned int get_random_number(int nr, int max)
+{
+ return (max > 0 ? random_linux_libc(nr) % max : 0);
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* system info functions */
+/* ------------------------------------------------------------------------- */
+
+#if !defined(PLATFORM_ANDROID)
+static char *get_corrected_real_name(char *real_name)
+{
+ char *real_name_new = checked_malloc(MAX_USERNAME_LEN + 1);
+ char *from_ptr = real_name;
+ char *to_ptr = real_name_new;
+
+ /* copy the name string, but not more than MAX_USERNAME_LEN characters */
+ while (*from_ptr && (int)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1)
+ {
+ /* the name field read from "passwd" file may also contain additional
+ user information, separated by commas, which will be removed here */
+ if (*from_ptr == ',')
+ break;
+
+ /* 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 == CHAR_BYTE_SHARP_S)
+ {
+ from_ptr++;
+ *to_ptr++ = 's';
+ *to_ptr++ = 's';
+ }
+ else
+ *to_ptr++ = *from_ptr++;
+ }
+
+ *to_ptr = '\0';
+
+ return real_name_new;
+}
+#endif
+
+char *getLoginName()
+{
+ static char *login_name = NULL;
+
+#if defined(PLATFORM_WIN32)
+ if (login_name == NULL)
+ {
+ unsigned long buffer_size = MAX_USERNAME_LEN + 1;
+ login_name = checked_malloc(buffer_size);
+
+ if (GetUserName(login_name, &buffer_size) == 0)
+ strcpy(login_name, ANONYMOUS_NAME);
+ }
+#elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID)
+ if (login_name == NULL)
+ {
+ struct passwd *pwd;
+
+ if ((pwd = getpwuid(getuid())) == NULL)
+ login_name = ANONYMOUS_NAME;
+ else
+ login_name = getStringCopy(pwd->pw_name);
+ }
+#else
+ login_name = ANONYMOUS_NAME;
+#endif
+
+ return login_name;
+}
+
+char *getRealName()
+{
+ static char *real_name = NULL;
+
+#if defined(PLATFORM_WIN32)
+ if (real_name == NULL)
+ {
+ static char buffer[MAX_USERNAME_LEN + 1];
+ unsigned long buffer_size = MAX_USERNAME_LEN + 1;
+
+ if (GetUserName(buffer, &buffer_size) != 0)
+ real_name = get_corrected_real_name(buffer);
+ else
+ real_name = ANONYMOUS_NAME;
+ }
+#elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID)
+ if (real_name == NULL)
+ {
+ struct passwd *pwd;
+
+ if ((pwd = getpwuid(getuid())) != NULL && strlen(pwd->pw_gecos) != 0)
+ real_name = get_corrected_real_name(pwd->pw_gecos);
+ else
+ real_name = ANONYMOUS_NAME;
+ }
+#else
+ real_name = ANONYMOUS_NAME;
+#endif
+
+ return real_name;
+}
+
+time_t getFileTimestampEpochSeconds(char *filename)
+{
+ struct stat file_status;
+
+ if (stat(filename, &file_status) != 0) /* cannot stat file */
+ return 0;
+
+ return file_status.st_mtime;
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* path manipulation functions */
+/* ------------------------------------------------------------------------- */
+
+static char *getLastPathSeparatorPtr(char *filename)
+{
+ char *last_separator = strrchr(filename, CHAR_PATH_SEPARATOR_UNIX);
+
+ if (last_separator == NULL) /* also try DOS/Windows variant */
+ last_separator = strrchr(filename, CHAR_PATH_SEPARATOR_DOS);
+
+ return last_separator;
+}
+
+char *getBaseNamePtr(char *filename)
+{
+ char *last_separator = getLastPathSeparatorPtr(filename);
+
+ if (last_separator != NULL)
+ return last_separator + 1; /* separator found: strip base path */
+ else
+ return filename; /* no separator found: filename has no path */
+}
+
+char *getBaseName(char *filename)
+{
+ return getStringCopy(getBaseNamePtr(filename));
+}
+
+char *getBaseNameNoSuffix(char *filename)
+{
+ char *basename = getStringCopy(getBaseNamePtr(filename));
+
+ // remove trailing suffix (separated by dot or hyphen)
+ if (basename[0] != '.' && basename[0] != '-')
+ {
+ if (strchr(basename, '.') != NULL)
+ *strchr(basename, '.') = '\0';
+
+ if (strchr(basename, '-') != NULL)
+ *strchr(basename, '-') = '\0';
+ }
+
+ return basename;
+}
+
+char *getBasePath(char *filename)
+{
+ char *basepath = getStringCopy(filename);
+ char *last_separator = getLastPathSeparatorPtr(basepath);
+
+ if (last_separator != NULL)
+ *last_separator = '\0'; /* separator found: strip basename */
+ else
+ basepath = "."; /* no separator found: use current path */
+
+ return basepath;
+}
+
+static char *getProgramMainDataPath()
+{
+ char *main_data_path = getStringCopy(program.command_basepath);
+
+#if defined(PLATFORM_MACOSX)
+ static char *main_data_binary_subdir = NULL;
+ static char *main_data_files_subdir = NULL;
+
+ if (main_data_binary_subdir == NULL)
+ {
+ main_data_binary_subdir = checked_malloc(strlen(program.program_title) + 1 +
+ strlen("app") + 1 +
+ strlen(MAC_APP_BINARY_SUBDIR) + 1);
+
+ sprintf(main_data_binary_subdir, "%s.app/%s",
+ program.program_title, MAC_APP_BINARY_SUBDIR);
+ }
+
+ if (main_data_files_subdir == NULL)
+ {
+ main_data_files_subdir = checked_malloc(strlen(program.program_title) + 1 +
+ strlen("app") + 1 +
+ strlen(MAC_APP_FILES_SUBDIR) + 1);
+
+ sprintf(main_data_files_subdir, "%s.app/%s",
+ program.program_title, MAC_APP_FILES_SUBDIR);
+ }
+
+ if (strSuffix(main_data_path, main_data_binary_subdir))
+ {
+ char *main_data_path_old = main_data_path;
+
+ // cut relative path to Mac OS X application binary directory from path
+ main_data_path[strlen(main_data_path) -
+ strlen(main_data_binary_subdir)] = '\0';
+
+ // cut trailing path separator from path (but not if path is root directory)
+ if (strSuffix(main_data_path, "/") && !strEqual(main_data_path, "/"))
+ main_data_path[strlen(main_data_path) - 1] = '\0';
+
+ // replace empty path with current directory
+ if (strEqual(main_data_path, ""))
+ main_data_path = ".";
+
+ // add relative path to Mac OS X application resources directory to path
+ main_data_path = getPath2(main_data_path, main_data_files_subdir);
+
+ free(main_data_path_old);
+ }
+#endif
+
+ return main_data_path;
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* various string functions */
+/* ------------------------------------------------------------------------- */
+
+char *getStringCat2WithSeparator(char *s1, char *s2, char *sep)
+{
+ if (s1 == NULL || s2 == NULL || sep == NULL)
+ return NULL;
+
+ char *complete_string = checked_malloc(strlen(s1) + strlen(sep) +
+ strlen(s2) + 1);
+
+ sprintf(complete_string, "%s%s%s", s1, sep, s2);
+
+ return complete_string;
+}
+
+char *getStringCat3WithSeparator(char *s1, char *s2, char *s3, char *sep)
+{
+ if (s1 == NULL || s2 == NULL || s3 == NULL || sep == NULL)
+ return NULL;
+
+ char *complete_string = checked_malloc(strlen(s1) + strlen(sep) +
+ strlen(s2) + strlen(sep) +
+ strlen(s3) + 1);
+
+ sprintf(complete_string, "%s%s%s%s%s", s1, sep, s2, sep, s3);
+
+ return complete_string;
+}
+
+char *getStringCat2(char *s1, char *s2)
+{
+ return getStringCat2WithSeparator(s1, s2, "");
+}
+
+char *getStringCat3(char *s1, char *s2, char *s3)
+{
+ return getStringCat3WithSeparator(s1, s2, s3, "");
+}
+
+char *getPath2(char *path1, char *path2)
+{
+#if defined(PLATFORM_ANDROID)
+ // workaround for reading from assets directory -- skip "." subdirs in path
+ if (strEqual(path1, "."))
+ return getStringCopy(path2);
+ else if (strEqual(path2, "."))
+ return getStringCopy(path1);
+#endif
+
+ return getStringCat2WithSeparator(path1, path2, STRING_PATH_SEPARATOR);
+}
+
+char *getPath3(char *path1, char *path2, char *path3)
+{
+#if defined(PLATFORM_ANDROID)
+ // workaround for reading from assets directory -- skip "." subdirs in path
+ if (strEqual(path1, "."))
+ return getStringCat2WithSeparator(path2, path3, STRING_PATH_SEPARATOR);
+ else if (strEqual(path2, "."))
+ return getStringCat2WithSeparator(path1, path3, STRING_PATH_SEPARATOR);
+ else if (strEqual(path3, "."))
+ return getStringCat2WithSeparator(path1, path2, STRING_PATH_SEPARATOR);
+#endif
+
+ return getStringCat3WithSeparator(path1, path2, path3, STRING_PATH_SEPARATOR);
+}
+
+static char *getPngOrPcxIfNotExists(char *filename)
+{
+ // switch from PNG to PCX file and vice versa, if file does not exist
+ // (backwards compatibility with PCX files used in previous versions)
+
+ if (!fileExists(filename) && strSuffix(filename, ".png"))
+ strcpy(&filename[strlen(filename) - 3], "pcx");
+ else if (!fileExists(filename) && strSuffix(filename, ".pcx"))
+ strcpy(&filename[strlen(filename) - 3], "png");
+
+ return filename;
+}
+
+char *getImg2(char *path1, char *path2)
+{
+ return getPngOrPcxIfNotExists(getPath2(path1, path2));
+}
+
+char *getImg3(char *path1, char *path2, char *path3)
+{
+ return getPngOrPcxIfNotExists(getPath3(path1, path2, path3));
+}
+
+char *getStringCopy(const char *s)
+{
+ char *s_copy;
+
+ if (s == NULL)
+ return NULL;
+
+ s_copy = checked_malloc(strlen(s) + 1);
+ strcpy(s_copy, s);
+
+ return s_copy;
+}
+
+char *getStringCopyN(const char *s, int n)
+{
+ char *s_copy;
+ int s_len = MAX(0, n);
+
+ if (s == NULL)
+ return NULL;
+
+ s_copy = checked_malloc(s_len + 1);
+ strncpy(s_copy, s, s_len);
+ s_copy[s_len] = '\0';
+
+ return s_copy;
+}
+
+char *getStringCopyNStatic(const char *s, int n)
+{
+ static char *s_copy = NULL;
+
+ checked_free(s_copy);
+
+ s_copy = getStringCopyN(s, n);
+
+ return s_copy;
+}
+
+char *getStringToLower(const char *s)
+{
+ char *s_copy = checked_malloc(strlen(s) + 1);
+ char *s_ptr = s_copy;
+
+ while (*s)
+ *s_ptr++ = tolower(*s++);
+ *s_ptr = '\0';
+
+ return s_copy;
+}
+
+void setString(char **old_value, char *new_value)
+{
+ checked_free(*old_value);
+
+ *old_value = getStringCopy(new_value);
+}
+
+boolean strEqual(char *s1, char *s2)
+{
+ return (s1 == NULL && s2 == NULL ? TRUE :
+ s1 == NULL && s2 != NULL ? FALSE :
+ s1 != NULL && s2 == NULL ? FALSE :
+ 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);
+}
+
+boolean strPrefix(char *s, char *prefix)
+{
+ return (s == NULL && prefix == NULL ? TRUE :
+ s == NULL && prefix != NULL ? FALSE :
+ s != NULL && prefix == NULL ? FALSE :
+ strncmp(s, prefix, strlen(prefix)) == 0);
+}
+
+boolean strSuffix(char *s, char *suffix)
+{
+ return (s == NULL && suffix == NULL ? TRUE :
+ s == NULL && suffix != NULL ? FALSE :
+ s != NULL && suffix == NULL ? FALSE :
+ strlen(s) < strlen(suffix) ? FALSE :
+ strncmp(&s[strlen(s) - strlen(suffix)], suffix, strlen(suffix)) == 0);
+}
+
+boolean strPrefixLower(char *s, char *prefix)
+{
+ char *s_lower = getStringToLower(s);
+ boolean match = strPrefix(s_lower, prefix);
+
+ free(s_lower);
+
+ return match;
+}
+
+boolean strSuffixLower(char *s, char *suffix)
+{
+ char *s_lower = getStringToLower(s);
+ boolean match = strSuffix(s_lower, suffix);
+
+ free(s_lower);
+
+ return match;
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* command line option handling functions */
+/* ------------------------------------------------------------------------- */
+
+void GetOptions(int argc, char *argv[],
+ void (*print_usage_function)(void),
+ void (*print_version_function)(void))
+{
+ char *ro_base_path = RO_BASE_PATH;
+ char *rw_base_path = RW_BASE_PATH;
+ char **argvplus = checked_calloc((argc + 1) * sizeof(char **));
+ char **options_left = &argvplus[1];
+
+ /* replace original "argv" with null-terminated array of string pointers */
+ while (argc--)
+ argvplus[argc] = argv[argc];
+
+ /* if the program is configured to start from current directory (default),
+ determine program package directory from program binary (some versions
+ of KDE/Konqueror and Mac OS X (especially "Mavericks") apparently do not
+ set the current working directory to the program package directory) */
+
+ if (strEqual(ro_base_path, "."))
+ ro_base_path = getProgramMainDataPath();
+ if (strEqual(rw_base_path, "."))
+ rw_base_path = getProgramMainDataPath();