X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fmisc.c;h=6445ee2894655da2331d777b733e12b81fe847cf;hb=e557b2b5d9951a4e692fd4e32a5cf45c84252c64;hp=28d04a6d1b3844eb59994f17142859aa4905a3ff;hpb=1256664ceac31f448a0139edd3bd0dc8fa5a8697;p=rocksndiamonds.git diff --git a/src/misc.c b/src/misc.c index 28d04a6d..6445ee28 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,122 +1,129 @@ /*********************************************************** * Rocks'n'Diamonds -- McDuffin Strikes Back! * *----------------------------------------------------------* -* ©1995 Artsoft Development * -* Holger Schemel * -* 33659 Bielefeld-Senne * -* Telefon: (0521) 493245 * -* eMail: aeglos@valinor.owl.de * -* aeglos@uni-paderborn.de * -* q99492@pbhrzx.uni-paderborn.de * +* (c) 1995-98 Artsoft Entertainment * +* Holger Schemel * +* Oststrasse 11a * +* 33604 Bielefeld * +* phone: ++49 +521 290471 * +* email: aeglos@valinor.owl.de * *----------------------------------------------------------* * misc.c * ***********************************************************/ -#include "misc.h" -#include "tools.h" -#include "sound.h" -#include "random.h" - #include #include #include #include #include #include +#include -void microsleep(unsigned long usec) -{ - if (usec < 5000) - { - /* we want to wait less than 5 ms -- if we assume that we have a - kernel timer resolution of 10 ms, we would wait far to long; - therefore it's better to do a short interval of busy waiting - to get our sleeping time more accurate */ - - long base_counter = Counter(), actual_counter = Counter(); - long delay = usec/1000; - - while (actual_counter < base_counter+delay && - actual_counter >= base_counter) - actual_counter = Counter(); - } - else - { - struct timeval delay; - - delay.tv_sec = usec / 1000000; - delay.tv_usec = usec % 1000000; - - if (select(0,NULL,NULL,NULL,&delay) != 0) - fprintf(stderr,"%s: in function microsleep: select failed!\n", - progname); - } -} +#include "misc.h" +#include "init.h" +#include "tools.h" +#include "sound.h" +#include "random.h" -long mainCounter(int mode) +static unsigned long mainCounter(int mode) { static struct timeval base_time = { 0, 0 }; struct timeval current_time; - long counter_ms; + unsigned long counter_ms; + + gettimeofday(¤t_time, NULL); - gettimeofday(¤t_time,NULL); if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec) base_time = current_time; - counter_ms = (current_time.tv_sec - base_time.tv_sec)*1000 - + (current_time.tv_usec - base_time.tv_usec)/1000; + counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000 + + (current_time.tv_usec - base_time.tv_usec) / 1000; return counter_ms; /* return milliseconds since last init */ } -void InitCounter() /* set counter back to zero */ +void InitCounter() /* set counter back to zero */ { mainCounter(INIT_COUNTER); } -long Counter() /* get milliseconds since last call of InitCounter() */ +unsigned long Counter() /* get milliseconds since last call of InitCounter() */ { return(mainCounter(READ_COUNTER)); } -void WaitCounter(long value) /* wait for counter to reach value */ +static void sleep_milliseconds(unsigned long milliseconds_delay) { - long wait; + if (milliseconds_delay < 5) + { + /* we want to wait only a few ms -- if we assume that we have a + kernel timer resolution of 10 ms, we would wait far to long; + 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(); + + while (actual_counter < base_counter + milliseconds_delay && + actual_counter >= base_counter) + actual_counter = Counter(); + } + else + { + struct timeval delay; - while((wait=value-Counter())>0) - microsleep(wait * 1000); + delay.tv_sec = milliseconds_delay / 1000; + delay.tv_usec = 1000 * (milliseconds_delay % 1000); + + if (select(0, NULL, NULL, NULL, &delay) != 0) + Error(ERR_RETURN, "sleep_milliseconds(): select() failed"); + } } -void Delay(long value) /* Delay 'value' milliseconds */ +void Delay(unsigned long delay) /* Sleep specified number of milliseconds */ { - microsleep(value * 1000); + sleep_milliseconds(delay); } -BOOL DelayReached(long *counter_var, int delay) +BOOL FrameReached(unsigned long *frame_counter_var, unsigned long frame_delay) { - long actual_counter = Counter(); + unsigned long actual_frame_counter = FrameCounter; - if (actual_counter >= *counter_var+delay || actual_counter < *counter_var) - { - *counter_var = actual_counter; - return(TRUE); - } - else + if (actual_frame_counter < *frame_counter_var+frame_delay && + actual_frame_counter >= *frame_counter_var) return(FALSE); + + *frame_counter_var = actual_frame_counter; + return(TRUE); } -BOOL FrameReached(long *frame_counter_var, int frame_delay) +BOOL DelayReached(unsigned long *counter_var, unsigned long delay) { - long actual_frame_counter = FrameCounter; + unsigned long actual_counter = Counter(); + + if (actual_counter < *counter_var + delay && + actual_counter >= *counter_var) + return(FALSE); - if (actual_frame_counter >= *frame_counter_var+frame_delay || - actual_frame_counter < *frame_counter_var) + *counter_var = actual_counter; + return(TRUE); +} + +void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay) +{ + unsigned long actual_counter; + + while(1) { - *frame_counter_var = actual_frame_counter; - return(TRUE); + actual_counter = Counter(); + + if (actual_counter < *counter_var + delay && + actual_counter >= *counter_var) + sleep_milliseconds((*counter_var + delay - actual_counter) / 2); + else + break; } - else - return(FALSE); + + *counter_var = actual_counter; } char *int2str(int ct, int nr) @@ -181,3 +188,165 @@ void MarkTileDirty(int x, int y) redraw_mask |= REDRAW_TILES; } } + +void GetOptions(char *argv[]) +{ + char **options_left = &argv[1]; + + while (*options_left) + { + char option_str[MAX_OPTION_LEN]; + char *option = options_left[0]; + char *next_option = options_left[1]; + char *option_arg = NULL; + int option_len = strlen(option); + + strcpy(option_str, option); /* copy argument into buffer */ + option = option_str; + + if (strcmp(option, "--") == 0) /* stop scanning arguments */ + break; + + if (option_len >= MAX_OPTION_LEN) + Error(ERR_EXITHELP, "unrecognized option '%s'", option); + + if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */ + option++; + + option_arg = strchr(option, '='); + if (option_arg == NULL) /* no '=' in option */ + option_arg = next_option; + else + { + *option_arg++ = '\0'; /* cut argument from option */ + if (*option_arg == '\0') /* no argument after '=' */ + Error(ERR_EXITHELP, "option '%s' has invalid argument", option_str); + } + + option_len = strlen(option); + + if (strcmp(option, "-") == 0) + Error(ERR_EXITHELP, "unrecognized option '%s'", option); + else if (strncmp(option, "-help", option_len) == 0) + { + printf("Usage: %s [options] [server.name [port]]\n" + "Options:\n" + " -d, --display machine:0 X server display\n" + " -l, --levels directory alternative level directory\n" + " -v, --verbose verbose mode\n", + program_name); + exit(0); + } + else if (strncmp(option, "-display", option_len) == 0) + { + if (option_arg == NULL) + Error(ERR_EXITHELP, "option '%s' requires an argument", option_str); + + display_name = option_arg; + if (option_arg == next_option) + options_left++; + + printf("--display == '%s'\n", display_name); + } + else if (strncmp(option, "-levels", option_len) == 0) + { + if (option_arg == NULL) + Error(ERR_EXITHELP, "option '%s' requires an argument", option_str); + + level_directory = option_arg; + if (option_arg == next_option) + options_left++; + + printf("--levels == '%s'\n", level_directory); + } + else if (strncmp(option, "-verbose", option_len) == 0) + { + printf("--verbose\n"); + + verbose = TRUE; + } + else if (*option == '-') + Error(ERR_EXITHELP, "unrecognized option '%s'", option_str); + else if (server_host == NULL) + { + server_host = *options_left; + + printf("server.name == '%s'\n", server_host); + } + else if (server_port == 0) + { + server_port = atoi(*options_left); + if (server_port < 1024) + Error(ERR_EXITHELP, "bad port number '%d'", server_port); + + printf("port == %d\n", server_port); + } + else + Error(ERR_EXITHELP, "too many arguments"); + + options_left++; + } +} + +void Error(int mode, char *format_str, ...) +{ + FILE *output_stream = stderr; + + if (format_str) + { + va_list ap; + char *format_ptr; + char *s_value; + int i_value; + double d_value; + + fprintf(output_stream, "%s: ", program_name); + + va_start(ap, format_str); /* ap points to first unnamed argument */ + + for(format_ptr=format_str; *format_ptr; format_ptr++) + { + if (*format_ptr != '%') + { + fprintf(output_stream, "%c", *format_ptr); + continue; + } + + switch(*++format_ptr) + { + case 'd': + i_value = va_arg(ap, int); + fprintf(output_stream, "%d", i_value); + break; + + case 'f': + d_value = va_arg(ap, double); + fprintf(output_stream, "%f", d_value); + break; + + case 's': + s_value = va_arg(ap, char *); + fprintf(output_stream, "%s", s_value); + break; + + default: + fprintf(stderr, "\nError(): invalid format string: %s\n",format_str); + CloseAllAndExit(10); + } + } + + va_end(ap); + + fprintf(output_stream, "\n"); + } + + if (mode == ERR_EXITHELP) + fprintf(output_stream, "%s: Try option '--help' for more information.\n", + program_name); + + if (mode == ERR_EXIT || mode == ERR_EXITHELP) + { + fprintf(output_stream, "%s: aborting\n", program_name); + CloseAllAndExit(1); + } +}