1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * ©1995 Artsoft Development *
6 * 33659 Bielefeld-Senne *
7 * Telefon: (0521) 493245 *
8 * eMail: aeglos@valinor.owl.de *
9 * aeglos@uni-paderborn.de *
10 * q99492@pbhrzx.uni-paderborn.de *
11 *----------------------------------------------------------*
13 ***********************************************************/
24 #include <sys/param.h>
25 #include <sys/types.h>
27 void microsleep(unsigned long usec)
31 /* we want to wait less than 5 ms -- if we assume that we have a
32 kernel timer resolution of 10 ms, we would wait far to long;
33 therefore it's better to do a short interval of busy waiting
34 to get our sleeping time more accurate */
36 long base_counter = Counter2(), actual_counter = Counter2();
37 long delay = usec/1000;
39 while (actual_counter < base_counter+delay &&
40 actual_counter >= base_counter)
41 actual_counter = Counter2();
47 delay.tv_sec = usec / 1000000;
48 delay.tv_usec = usec % 1000000;
50 if (select(0,NULL,NULL,NULL,&delay) != 0)
51 fprintf(stderr,"%s: in function microsleep: select failed!\n",
56 long mainCounter(int mode)
58 static struct timeval base_time = { 0, 0 };
59 struct timeval current_time;
62 gettimeofday(¤t_time,NULL);
63 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
64 base_time = current_time;
66 counter_ms = (current_time.tv_sec - base_time.tv_sec)*1000
67 + (current_time.tv_usec - base_time.tv_usec)/1000;
69 if (mode == READ_COUNTER_100)
70 return(counter_ms/10); /* return 1/100 secs since last init */
71 else /* READ_COUNTER_1000 */
72 return(counter_ms); /* return 1/1000 secs since last init */
75 void InitCounter() /* set counter back to zero */
77 mainCounter(INIT_COUNTER);
80 long Counter() /* returns 1/100 secs since last call of InitCounter() */
82 return(mainCounter(READ_COUNTER_100));
85 long Counter2() /* returns 1/1000 secs since last call of InitCounter() */
87 return(mainCounter(READ_COUNTER_1000));
90 void WaitCounter(long value) /* wait for counter to reach value */
94 while((wait=value-Counter())>0)
95 microsleep(wait*10000);
98 void WaitCounter2(long value) /* wait for counter to reach value */
102 while((wait=value-Counter2())>0)
103 microsleep(wait*1000);
106 void Delay(long value)
111 BOOL DelayReached(long *counter_var, int delay)
113 long actual_counter = Counter();
115 if (actual_counter >= *counter_var+delay || actual_counter < *counter_var)
117 *counter_var = actual_counter;
124 BOOL FrameReached(long *frame_counter_var, int frame_delay)
126 long actual_frame_counter = FrameCounter;
128 if (actual_frame_counter >= *frame_counter_var+frame_delay
129 || actual_frame_counter < *frame_counter_var)
131 *frame_counter_var = actual_frame_counter;
138 char *int2str(int ct, int nr)
142 sprintf(str,"%09d",ct);
143 return(&str[strlen(str)-nr]);
146 unsigned int SimpleRND(unsigned int max)
148 static unsigned long root = 654321;
149 struct timeval current_time;
151 gettimeofday(¤t_time,NULL);
152 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
156 unsigned int RND(unsigned int max)
158 return(random_linux_libc() % max);
161 unsigned int InitRND(long seed)
163 struct timeval current_time;
165 if (seed==NEW_RANDOMIZE)
167 gettimeofday(¤t_time,NULL);
168 srandom_linux_libc((unsigned int) current_time.tv_usec);
169 return((unsigned int) current_time.tv_usec);
173 srandom_linux_libc((unsigned int) seed);
174 return((unsigned int) seed);
182 if (!(pwd=getpwuid(getuid())))
185 return(pwd->pw_name);
188 void MarkTileDirty(int x, int y)
190 int xx = redraw_x1 + x;
191 int yy = redraw_y1 + y;
195 redraw[xx][yy] = TRUE;
197 redraw_mask |= REDRAW_TILES;