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 ***********************************************************/
23 #include <sys/param.h>
24 #include <sys/types.h>
26 static unsigned long mainCounter(int mode)
28 static struct timeval base_time = { 0, 0 };
29 struct timeval current_time;
30 unsigned long counter_ms;
32 gettimeofday(¤t_time, NULL);
34 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
35 base_time = current_time;
37 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
38 + (current_time.tv_usec - base_time.tv_usec) / 1000;
40 return counter_ms; /* return milliseconds since last init */
43 void InitCounter() /* set counter back to zero */
45 mainCounter(INIT_COUNTER);
48 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
50 return(mainCounter(READ_COUNTER));
53 static void sleep_milliseconds(unsigned long milliseconds_delay)
55 if (milliseconds_delay < 5)
57 /* we want to wait only a few ms -- if we assume that we have a
58 kernel timer resolution of 10 ms, we would wait far to long;
59 therefore it's better to do a short interval of busy waiting
60 to get our sleeping time more accurate */
62 unsigned long base_counter = Counter(), actual_counter = Counter();
64 while (actual_counter < base_counter + milliseconds_delay &&
65 actual_counter >= base_counter)
66 actual_counter = Counter();
72 delay.tv_sec = milliseconds_delay / 1000;
73 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
75 if (select(0, NULL, NULL, NULL, &delay) != 0)
76 fprintf(stderr,"%s: in function sleep_milliseconds: select() failed!\n",
81 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
83 sleep_milliseconds(delay);
86 BOOL FrameReached(unsigned long *frame_counter_var, unsigned long frame_delay)
88 unsigned long actual_frame_counter = FrameCounter;
90 if (actual_frame_counter < *frame_counter_var+frame_delay &&
91 actual_frame_counter >= *frame_counter_var)
94 *frame_counter_var = actual_frame_counter;
98 BOOL DelayReached(unsigned long *counter_var, unsigned long delay)
100 unsigned long actual_counter = Counter();
102 if (actual_counter < *counter_var + delay &&
103 actual_counter >= *counter_var)
106 *counter_var = actual_counter;
110 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
112 unsigned long actual_counter;
116 actual_counter = Counter();
118 if (actual_counter < *counter_var + delay &&
119 actual_counter >= *counter_var)
120 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
125 *counter_var = actual_counter;
128 char *int2str(int ct, int nr)
132 sprintf(str,"%09d",ct);
133 return(&str[strlen(str)-nr]);
136 unsigned int SimpleRND(unsigned int max)
138 static unsigned long root = 654321;
139 struct timeval current_time;
141 gettimeofday(¤t_time,NULL);
142 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
146 unsigned int RND(unsigned int max)
148 return(random_linux_libc() % max);
151 unsigned int InitRND(long seed)
153 struct timeval current_time;
155 if (seed==NEW_RANDOMIZE)
157 gettimeofday(¤t_time,NULL);
158 srandom_linux_libc((unsigned int) current_time.tv_usec);
159 return((unsigned int) current_time.tv_usec);
163 srandom_linux_libc((unsigned int) seed);
164 return((unsigned int) seed);
172 if (!(pwd=getpwuid(getuid())))
175 return(pwd->pw_name);
178 void MarkTileDirty(int x, int y)
180 int xx = redraw_x1 + x;
181 int yy = redraw_y1 + y;
185 redraw[xx][yy] = TRUE;
187 redraw_mask |= REDRAW_TILES;