1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 #include <sys/param.h>
19 #include <sys/types.h>
28 static unsigned long mainCounter(int mode)
30 static struct timeval base_time = { 0, 0 };
31 struct timeval current_time;
32 unsigned long counter_ms;
34 gettimeofday(¤t_time, NULL);
36 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
37 base_time = current_time;
39 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
40 + (current_time.tv_usec - base_time.tv_usec) / 1000;
42 return counter_ms; /* return milliseconds since last init */
45 void InitCounter() /* set counter back to zero */
47 mainCounter(INIT_COUNTER);
50 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
52 return(mainCounter(READ_COUNTER));
55 static void sleep_milliseconds(unsigned long milliseconds_delay)
57 if (milliseconds_delay < 5)
59 /* we want to wait only a few ms -- if we assume that we have a
60 kernel timer resolution of 10 ms, we would wait far to long;
61 therefore it's better to do a short interval of busy waiting
62 to get our sleeping time more accurate */
64 unsigned long base_counter = Counter(), actual_counter = Counter();
66 while (actual_counter < base_counter + milliseconds_delay &&
67 actual_counter >= base_counter)
68 actual_counter = Counter();
74 delay.tv_sec = milliseconds_delay / 1000;
75 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
77 if (select(0, NULL, NULL, NULL, &delay) != 0)
78 Error(ERR_RETURN, "sleep_milliseconds(): select() failed");
82 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
84 sleep_milliseconds(delay);
87 BOOL FrameReached(unsigned long *frame_counter_var, unsigned long frame_delay)
89 unsigned long actual_frame_counter = FrameCounter;
91 if (actual_frame_counter < *frame_counter_var+frame_delay &&
92 actual_frame_counter >= *frame_counter_var)
95 *frame_counter_var = actual_frame_counter;
99 BOOL DelayReached(unsigned long *counter_var, unsigned long delay)
101 unsigned long actual_counter = Counter();
103 if (actual_counter < *counter_var + delay &&
104 actual_counter >= *counter_var)
107 *counter_var = actual_counter;
111 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
113 unsigned long actual_counter;
117 actual_counter = Counter();
119 if (actual_counter < *counter_var + delay &&
120 actual_counter >= *counter_var)
121 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
126 *counter_var = actual_counter;
129 char *int2str(int ct, int nr)
133 sprintf(str,"%09d",ct);
134 return(&str[strlen(str)-nr]);
137 unsigned int SimpleRND(unsigned int max)
139 static unsigned long root = 654321;
140 struct timeval current_time;
142 gettimeofday(¤t_time,NULL);
143 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
147 unsigned int RND(unsigned int max)
149 return(random_linux_libc() % max);
152 unsigned int InitRND(long seed)
154 struct timeval current_time;
156 if (seed==NEW_RANDOMIZE)
158 gettimeofday(¤t_time,NULL);
159 srandom_linux_libc((unsigned int) current_time.tv_usec);
160 return((unsigned int) current_time.tv_usec);
164 srandom_linux_libc((unsigned int) seed);
165 return((unsigned int) seed);
173 if (!(pwd=getpwuid(getuid())))
176 return(pwd->pw_name);
179 void MarkTileDirty(int x, int y)
181 int xx = redraw_x1 + x;
182 int yy = redraw_y1 + y;
186 redraw[xx][yy] = TRUE;
188 redraw_mask |= REDRAW_TILES;
192 void Error(BOOL fatal_error, char *format_str, ...)
194 FILE *output_stream = stderr;
201 va_start(ap, format_str); /* ap points to first unnamed argument */
203 fprintf(output_stream, "%s: ", program_name);
205 for(format_ptr=format_str; *format_ptr; format_ptr++)
207 if (*format_ptr != '%')
209 fprintf(output_stream, "%c", *format_ptr);
213 switch(*++format_ptr)
216 i_value = va_arg(ap, int);
217 fprintf(output_stream, "%d", i_value);
221 d_value = va_arg(ap, double);
222 fprintf(output_stream, "%f", d_value);
226 s_value = va_arg(ap, char *);
227 fprintf(output_stream, "%s", s_value);
231 fprintf(stderr, "\nfatal(): invalid format string: %s\n", format_str);
238 fprintf(output_stream, "\n");
242 fprintf(output_stream, "%s: aborting\n", program_name);