X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=7e273adadc5269a8ef394975dc5bafb39d4e375a;hb=3c53030b9e20c59004046274e94eff2ee90d0ab5;hp=d21815b29b48db5dc91e27de15558b5f50984880;hpb=f965a2a6e56dd5c1e91b306dfd52b38b1dd49537;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index d21815b2..7e273ada 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -168,7 +168,7 @@ boolean getTokenValueFromString(char *string, char **token, char **value) /* ------------------------------------------------------------------------- */ #if defined(PLATFORM_MSDOS) -volatile unsigned long counter = 0; +volatile unsigned int counter = 0; void increment_counter() { @@ -185,7 +185,7 @@ END_OF_FUNCTION(increment_counter); #if 1 #ifdef TARGET_SDL -static unsigned long getCurrentMS() +static unsigned int getCurrentMS() { return SDL_GetTicks(); } @@ -193,7 +193,7 @@ static unsigned long getCurrentMS() #else /* !TARGET_SDL */ #if defined(PLATFORM_UNIX) -static unsigned long getCurrentMS() +static unsigned int getCurrentMS() { struct timeval current_time; @@ -204,10 +204,10 @@ static unsigned long getCurrentMS() #endif /* PLATFORM_UNIX */ #endif /* !TARGET_SDL */ -static unsigned long mainCounter(int mode) +static unsigned int mainCounter(int mode) { - static unsigned long base_ms = 0; - unsigned long current_ms; + static unsigned int base_ms = 0; + unsigned int current_ms; /* get current system milliseconds */ current_ms = getCurrentMS(); @@ -223,11 +223,11 @@ static unsigned long mainCounter(int mode) #else #ifdef TARGET_SDL -static unsigned long mainCounter(int mode) +static unsigned int mainCounter(int mode) { - static unsigned long base_ms = 0; - unsigned long current_ms; - unsigned long counter_ms; + static unsigned int base_ms = 0; + unsigned int current_ms; + unsigned int counter_ms; current_ms = SDL_GetTicks(); @@ -243,11 +243,11 @@ static unsigned long mainCounter(int mode) #else /* !TARGET_SDL */ #if defined(PLATFORM_UNIX) -static unsigned long mainCounter(int mode) +static unsigned int mainCounter(int mode) { static struct timeval base_time = { 0, 0 }; struct timeval current_time; - unsigned long counter_ms; + unsigned int counter_ms; gettimeofday(¤t_time, NULL); @@ -276,7 +276,7 @@ void InitCounter() /* set counter back to zero */ #endif } -unsigned long Counter() /* get milliseconds since last call of InitCounter() */ +unsigned int Counter() /* get milliseconds since last call of InitCounter() */ { #if !defined(PLATFORM_MSDOS) return mainCounter(READ_COUNTER); @@ -285,7 +285,7 @@ unsigned long Counter() /* get milliseconds since last call of InitCounter() */ #endif } -static void sleep_milliseconds(unsigned long milliseconds_delay) +static void sleep_milliseconds(unsigned int milliseconds_delay) { boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE); @@ -296,7 +296,7 @@ static void sleep_milliseconds(unsigned long milliseconds_delay) therefore it's better to do a short interval of busy waiting to get our sleeping time more accurate */ - unsigned long base_counter = Counter(), actual_counter = Counter(); + unsigned int base_counter = Counter(), actual_counter = Counter(); while (actual_counter < base_counter + milliseconds_delay && actual_counter >= base_counter) @@ -320,15 +320,15 @@ static void sleep_milliseconds(unsigned long milliseconds_delay) } } -void Delay(unsigned long delay) /* Sleep specified number of milliseconds */ +void Delay(unsigned int delay) /* Sleep specified number of milliseconds */ { sleep_milliseconds(delay); } -boolean FrameReached(unsigned long *frame_counter_var, - unsigned long frame_delay) +boolean FrameReached(unsigned int *frame_counter_var, + unsigned int frame_delay) { - unsigned long actual_frame_counter = FrameCounter; + unsigned int actual_frame_counter = FrameCounter; if (actual_frame_counter >= *frame_counter_var && actual_frame_counter < *frame_counter_var + frame_delay) @@ -339,10 +339,10 @@ boolean FrameReached(unsigned long *frame_counter_var, return TRUE; } -boolean DelayReached(unsigned long *counter_var, - unsigned long delay) +boolean DelayReached(unsigned int *counter_var, + unsigned int delay) { - unsigned long actual_counter = Counter(); + unsigned int actual_counter = Counter(); if (actual_counter >= *counter_var && actual_counter < *counter_var + delay) @@ -353,9 +353,9 @@ boolean DelayReached(unsigned long *counter_var, return TRUE; } -void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) +void WaitUntilDelayReached(unsigned int *counter_var, unsigned int delay) { - unsigned long actual_counter; + unsigned int actual_counter; while (1) { @@ -376,12 +376,12 @@ void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) /* random generator functions */ /* ------------------------------------------------------------------------- */ -unsigned int init_random_number(int nr, long seed) +unsigned int init_random_number(int nr, int seed) { if (seed == NEW_RANDOMIZE) { /* default random seed */ - seed = (long)time(NULL); // seconds since the epoch + seed = (int)time(NULL); // seconds since the epoch #if !defined(PLATFORM_WIN32) /* add some more randomness */ @@ -389,12 +389,12 @@ unsigned int init_random_number(int nr, long seed) gettimeofday(¤t_time, NULL); - seed += (long)current_time.tv_usec; // microseconds since the epoch + seed += (int)current_time.tv_usec; // microseconds since the epoch #endif #if defined(TARGET_SDL) /* add some more randomness */ - seed += (long)SDL_GetTicks(); // milliseconds since SDL init + seed += (int)SDL_GetTicks(); // milliseconds since SDL init #endif #if 1 @@ -426,7 +426,7 @@ static char *get_corrected_real_name(char *real_name) char *to_ptr = real_name_new; /* copy the name string, but not more than MAX_USERNAME_LEN characters */ - while (*from_ptr && (long)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1) + 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 */ @@ -564,6 +564,36 @@ char *getBasePath(char *filename) return basepath; } +static char *getProgramMainDataPath() +{ + char *main_data_path = getStringCopy(program.command_basepath); + +#if defined(PLATFORM_MACOSX) + static char *main_data_binary_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); + } + + // cut relative path to Mac OS X application binary directory from path + if (strSuffix(main_data_path, main_data_binary_subdir)) + 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'; +#endif + + return main_data_path; +} + /* ------------------------------------------------------------------------- */ /* various string functions */ @@ -721,6 +751,18 @@ void GetOptions(char *argv[], void (*print_usage_function)(void)) char *rw_base_path = RW_BASE_PATH; char **options_left = &argv[1]; +#if 1 + /* 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 "Maverick") 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(); +#else + #if !defined(PLATFORM_MACOSX) /* if the program is configured to start from current directory (default), determine program package directory (KDE/Konqueror does not do this by @@ -732,6 +774,8 @@ void GetOptions(char *argv[], void (*print_usage_function)(void)) ro_base_path = program.command_basepath; if (strEqual(rw_base_path, ".")) rw_base_path = program.command_basepath; +#endif + #endif /* initialize global program options */ @@ -890,7 +934,7 @@ void GetOptions(char *argv[], void (*print_usage_function)(void)) options.special_flags = getStringCopy(&option[2]); #else char *flags_string = &option[2]; - unsigned long flags_value; + unsigned int flags_value; if (*flags_string == '\0') Error(ERR_EXIT_HELP, "empty flag ignored"); @@ -963,9 +1007,11 @@ void Error(int mode, char *format, ...) static boolean last_line_was_separator = FALSE; char *process_name = ""; +#if 1 /* display warnings only when running in verbose mode */ if (mode & ERR_WARN && !options.verbose) return; +#endif if (mode == ERR_INFO_LINE) { @@ -1024,7 +1070,7 @@ void Error(int mode, char *format, ...) /* checked memory allocation and freeing functions */ /* ------------------------------------------------------------------------- */ -void *checked_malloc(unsigned long size) +void *checked_malloc(unsigned int size) { void *ptr; @@ -1036,7 +1082,7 @@ void *checked_malloc(unsigned long size) return ptr; } -void *checked_calloc(unsigned long size) +void *checked_calloc(unsigned int size) { void *ptr; @@ -1048,7 +1094,7 @@ void *checked_calloc(unsigned long size) return ptr; } -void *checked_realloc(void *ptr, unsigned long size) +void *checked_realloc(void *ptr, unsigned int size) { ptr = realloc(ptr, size); @@ -1064,7 +1110,7 @@ void checked_free(void *ptr) free(ptr); } -void clear_mem(void *ptr, unsigned long size) +void clear_mem(void *ptr, unsigned int size) { #if defined(PLATFORM_WIN32) /* for unknown reason, memset() sometimes crashes when compiled with MinGW */ @@ -1193,7 +1239,8 @@ boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size, const int chunk_name_length = 4; /* read chunk name */ - fgets(chunk_name, chunk_name_length + 1, file); + if (fgets(chunk_name, chunk_name_length + 1, file) == NULL) + return FALSE; if (chunk_size != NULL) { @@ -1256,7 +1303,7 @@ int putFileVersion(FILE *file, int version) return 4; } -void ReadBytesFromFile(FILE *file, byte *buffer, unsigned long bytes) +void ReadBytesFromFile(FILE *file, byte *buffer, unsigned int bytes) { int i; @@ -1264,7 +1311,7 @@ void ReadBytesFromFile(FILE *file, byte *buffer, unsigned long bytes) buffer[i] = fgetc(file); } -void WriteBytesToFile(FILE *file, byte *buffer, unsigned long bytes) +void WriteBytesToFile(FILE *file, byte *buffer, unsigned int bytes) { int i; @@ -1272,13 +1319,13 @@ void WriteBytesToFile(FILE *file, byte *buffer, unsigned long bytes) fputc(buffer[i], file); } -void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes) +void ReadUnusedBytesFromFile(FILE *file, unsigned int bytes) { while (bytes-- && !feof(file)) fgetc(file); } -void WriteUnusedBytesToFile(FILE *file, unsigned long bytes) +void WriteUnusedBytesToFile(FILE *file, unsigned int bytes) { while (bytes--) fputc(0, file); @@ -1475,7 +1522,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode) while (translate_key[++i].x11name); if (!translate_key[i].x11name) - sprintf(name_buffer, "0x%04lx", (unsigned long)key); + sprintf(name_buffer, "0x%04x", (unsigned int)key); } *x11name = name_buffer; @@ -1552,7 +1599,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode) } else if (strPrefix(name_ptr, "0x")) { - unsigned long value = 0; + unsigned int value = 0; name_ptr += 2; @@ -1873,7 +1920,7 @@ boolean FileIsMusic(char *filename) return TRUE; #if defined(TARGET_SDL) - if (fileHasPrefix(basename, "mod") || + if ((fileHasPrefix(basename, "mod") && !fileHasSuffix(basename, "txt")) || fileHasSuffix(basename, "mod") || fileHasSuffix(basename, "s3m") || fileHasSuffix(basename, "it") || @@ -3148,7 +3195,7 @@ void debug_print_timestamp(int counter_nr, char *message) counter[counter_nr][0] = Counter_Microseconds(); #else - static long counter[DEBUG_NUM_TIMESTAMPS][2]; + static int counter[DEBUG_NUM_TIMESTAMPS][2]; char *unit = "s"; counter[counter_nr][0] = Counter();