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 = Counter(), actual_counter = Counter();
37 long delay = usec/1000;
39 while (actual_counter < base_counter+delay &&
40 actual_counter >= base_counter)
41 actual_counter = Counter();
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 return counter_ms; /* return milliseconds since last init */
72 void InitCounter() /* set counter back to zero */
74 mainCounter(INIT_COUNTER);
77 long Counter() /* get milliseconds since last call of InitCounter() */
79 return(mainCounter(READ_COUNTER));
82 void WaitCounter(long value) /* wait for counter to reach value */
86 while((wait=value-Counter())>0)
87 microsleep(wait * 1000);
90 void Delay(long value) /* Delay 'value' milliseconds */
92 microsleep(value * 1000);
95 BOOL DelayReached(long *counter_var, int delay)
97 long actual_counter = Counter();
99 if (actual_counter >= *counter_var+delay || actual_counter < *counter_var)
101 *counter_var = actual_counter;
108 BOOL FrameReached(long *frame_counter_var, int frame_delay)
110 long actual_frame_counter = FrameCounter;
112 if (actual_frame_counter >= *frame_counter_var+frame_delay ||
113 actual_frame_counter < *frame_counter_var)
115 *frame_counter_var = actual_frame_counter;
122 char *int2str(int ct, int nr)
126 sprintf(str,"%09d",ct);
127 return(&str[strlen(str)-nr]);
130 unsigned int SimpleRND(unsigned int max)
132 static unsigned long root = 654321;
133 struct timeval current_time;
135 gettimeofday(¤t_time,NULL);
136 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
140 unsigned int RND(unsigned int max)
142 return(random_linux_libc() % max);
145 unsigned int InitRND(long seed)
147 struct timeval current_time;
149 if (seed==NEW_RANDOMIZE)
151 gettimeofday(¤t_time,NULL);
152 srandom_linux_libc((unsigned int) current_time.tv_usec);
153 return((unsigned int) current_time.tv_usec);
157 srandom_linux_libc((unsigned int) seed);
158 return((unsigned int) seed);
166 if (!(pwd=getpwuid(getuid())))
169 return(pwd->pw_name);
172 void MarkTileDirty(int x, int y)
174 int xx = redraw_x1 + x;
175 int yy = redraw_y1 + y;
179 redraw[xx][yy] = TRUE;
181 redraw_mask |= REDRAW_TILES;