X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fmisc.c;h=3d448a69f57c10c39e18078c85e06aa0cbe769e1;hb=a8b2caa1d373c84c1b3fc2f615370c2a319051ca;hp=211108d0f5dbeb74f1e9a11ca56fd351499afc4b;hpb=52189f4b3b797e08699f67880198b585320cf8bf;p=rocksndiamonds.git diff --git a/src/misc.c b/src/misc.c index 211108d0..3d448a69 100644 --- a/src/misc.c +++ b/src/misc.c @@ -11,14 +11,17 @@ * misc.c * ***********************************************************/ -#include #include #include -#include #include #include #include +#ifndef WIN32 +#include +#include +#endif + #include "misc.h" #include "init.h" #include "tools.h" @@ -39,11 +42,31 @@ END_OF_FUNCTION(increment_counter); #endif - /* maximal allowed length of a command line option */ #define MAX_OPTION_LEN 256 +#ifdef TARGET_SDL + +static unsigned long mainCounter(int mode) +{ + static unsigned long base_ms = 0; + unsigned long current_ms; + unsigned long counter_ms; + + current_ms = SDL_GetTicks(); + + /* reset base time in case of counter initializing or wrap-around */ + if (mode == INIT_COUNTER || current_ms < base_ms) + base_ms = current_ms; + + counter_ms = current_ms - base_ms; + + return counter_ms; /* return milliseconds since last init */ +} + +#else /* !TARGET_SDL */ #ifndef MSDOS + static unsigned long mainCounter(int mode) { static struct timeval base_time = { 0, 0 }; @@ -52,6 +75,7 @@ static unsigned long mainCounter(int mode) gettimeofday(¤t_time, NULL); + /* reset base time in case of counter initializing or wrap-around */ if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec) base_time = current_time; @@ -60,7 +84,9 @@ static unsigned long mainCounter(int mode) return counter_ms; /* return milliseconds since last init */ } -#endif + +#endif /* !MSDOS */ +#endif /* !TARGET_SDL */ void InitCounter() /* set counter back to zero */ { @@ -87,7 +113,7 @@ static void sleep_milliseconds(unsigned long milliseconds_delay) boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE); #ifdef MSDOS - /* donĂ¯t use select() to perform waiting operations under DOS/Windows + /* don't use select() to perform waiting operations under DOS/Windows environment; always use a busy loop for waiting instead */ do_busy_waiting = TRUE; #endif @@ -107,6 +133,9 @@ static void sleep_milliseconds(unsigned long milliseconds_delay) } else { +#ifdef TARGET_SDL + SDL_Delay(milliseconds_delay); +#else /* !TARGET_SDL */ struct timeval delay; delay.tv_sec = milliseconds_delay / 1000; @@ -114,6 +143,7 @@ static void sleep_milliseconds(unsigned long milliseconds_delay) if (select(0, NULL, NULL, NULL, &delay) != 0) Error(ERR_WARN, "sleep_milliseconds(): select() failed"); +#endif /* !TARGET_SDL */ } } @@ -198,12 +228,25 @@ char *int2str(int number, int size) unsigned int SimpleRND(unsigned int max) { +#ifdef TARGET_SDL + + static unsigned long root = 654321; + unsigned long current_ms; + + current_ms = SDL_GetTicks(); + root = root * 4253261 + current_ms; + return (root % max); + +#else /* !TARGET_SDL */ + static unsigned long root = 654321; struct timeval current_time; - gettimeofday(¤t_time,NULL); + gettimeofday(¤t_time, NULL); root = root * 4253261 + current_time.tv_sec + current_time.tv_usec; return (root % max); + +#endif /* !TARGET_SDL */ } #ifdef DEBUG @@ -226,34 +269,56 @@ unsigned int RND(unsigned int max) unsigned int InitRND(long seed) { +#ifdef TARGET_SDL + unsigned long current_ms; + + if (seed == NEW_RANDOMIZE) + { + current_ms = SDL_GetTicks(); + srandom_linux_libc((unsigned int) current_ms); + return (unsigned int) current_ms; + } + else + { + srandom_linux_libc((unsigned int) seed); + return (unsigned int) seed; + } +#else /* !TARGET_SDL */ struct timeval current_time; if (seed == NEW_RANDOMIZE) { - gettimeofday(¤t_time,NULL); + gettimeofday(¤t_time, NULL); srandom_linux_libc((unsigned int) current_time.tv_usec); - return (unsigned int)current_time.tv_usec; + return (unsigned int) current_time.tv_usec; } else { srandom_linux_libc((unsigned int) seed); - return (unsigned int)seed; + return (unsigned int) seed; } +#endif /* !TARGET_SDL */ } char *getLoginName() { +#ifdef WIN32 + return ANONYMOUS_NAME; +#else struct passwd *pwd; if ((pwd = getpwuid(getuid())) == NULL) return ANONYMOUS_NAME; else return pwd->pw_name; +#endif } char *getRealName() { -#ifndef MSDOS +#if defined(MSDOS) || defined(WIN32) + return ANONYMOUS_NAME; +#else struct passwd *pwd; if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0) @@ -283,14 +348,14 @@ char *getRealName() return real_name; } -#else - return ANONYMOUS_NAME; #endif } char *getHomeDir() { -#ifndef MSDOS +#if defined(MSDOS) || defined(WIN32) + return "."; +#else static char *home_dir = NULL; if (!home_dir) @@ -307,8 +372,6 @@ char *getHomeDir() } return home_dir; -#else - return "."; #endif } @@ -401,6 +464,12 @@ void GetOptions(char *argv[]) options.serveronly = FALSE; options.network = FALSE; options.verbose = FALSE; + options.debug = FALSE; + + /* initialize some more global variables */ + global.frames_per_second = 0; + global.fps_slowdown = FALSE; + global.fps_slowdown_factor = 1; while (*options_left) { @@ -442,7 +511,7 @@ void GetOptions(char *argv[]) "Options:\n" " -d, --display machine:0 X server display\n" " -b, --basepath directory alternative base directory\n" - " -l, --level directory alternative level directory\n" + " -l, --level directory alternative level directory\n" " -s, --serveronly only start network server\n" " -n, --network network multiplayer game\n" " -v, --verbose verbose mode\n", @@ -494,6 +563,10 @@ void GetOptions(char *argv[]) { options.verbose = TRUE; } + else if (strncmp(option, "-debug", option_len) == 0) + { + options.debug = TRUE; + } else if (*option == '-') { Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str); @@ -524,7 +597,7 @@ void Error(int mode, char *format, ...) if (mode & ERR_WARN && !options.verbose) return; -#ifdef MSDOS +#if defined(MSDOS) || defined(WIN32) if ((error = openErrorFile()) == NULL) { printf("Cannot write to error output file!\n"); @@ -722,8 +795,10 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode) { KEY_Meta_R, "XK_Meta_R", "right meta" }, { KEY_Alt_L, "XK_Alt_L", "left alt" }, { KEY_Alt_R, "XK_Alt_R", "right alt" }, - { KEY_Mode_switch, "XK_Mode_switch", "mode switch" }, - { KEY_Multi_key, "XK_Multi_key", "multi key" }, + { KEY_Super_L, "XK_Super_L", "left super" }, /* Win-L */ + { KEY_Super_R, "XK_Super_R", "right super" }, /* Win-R */ + { KEY_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */ + { KEY_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */ /* some special keys */ { KEY_BackSpace, "XK_BackSpace", "backspace" }, @@ -734,7 +809,7 @@ void translate_keyname(Key *keysym, char **x11name, char **name, int mode) { KEY_End, "XK_End", "end" }, { KEY_Page_Up, "XK_Page_Up", "page up" }, { KEY_Page_Down, "XK_Page_Down", "page down" }, - + { KEY_Menu, "XK_Menu", "menu" }, /* Win-Menu */ /* ASCII 0x20 to 0x40 keys (except numbers) */ { KEY_space, "XK_space", "space" }, @@ -1107,8 +1182,13 @@ boolean validLevelSeries(struct LevelDirInfo *node) struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node) { - if (node == NULL) /* start with first level directory entry */ - return getFirstValidLevelSeries(leveldir_first); + if (node == NULL) + { + if (leveldir_first) /* start with first level directory entry */ + return getFirstValidLevelSeries(leveldir_first); + else + return NULL; + } else if (node->node_group) /* enter level group (step down into tree) */ return getFirstValidLevelSeries(node->node_group); else if (node->parent_link) /* skip start entry of level group */