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 delay.tv_sec = usec / 1000000;
32 delay.tv_usec = usec % 1000000;
34 if (select(0,NULL,NULL,NULL,&delay)!=0)
35 fprintf(stderr,"%s: in function microsleep: select failed!\n",
39 long mainCounter(int mode)
41 static struct timeval base_time = { 0, 0 };
42 struct timeval current_time;
45 gettimeofday(¤t_time,NULL);
46 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
47 base_time = current_time;
49 counter_ms = (current_time.tv_sec - base_time.tv_sec)*1000
50 + (current_time.tv_usec - base_time.tv_usec)/1000;
52 if (mode == READ_COUNTER_100)
53 return(counter_ms/10); /* return 1/100 secs since last init */
54 else /* READ_COUNTER_1000 */
55 return(counter_ms); /* return 1/1000 secs since last init */
58 void InitCounter() /* set counter back to zero */
60 mainCounter(INIT_COUNTER);
63 long Counter() /* returns 1/100 secs since last call of InitCounter() */
65 return(mainCounter(READ_COUNTER_100));
68 long Counter2() /* returns 1/1000 secs since last call of InitCounter() */
70 return(mainCounter(READ_COUNTER_1000));
73 void WaitCounter(long value) /* wait for counter to reach value */
77 while((wait=value-Counter())>0)
78 microsleep(wait*10000);
81 void WaitCounter2(long value) /* wait for counter to reach value */
85 while((wait=value-Counter2())>0)
86 microsleep(wait*1000);
89 void Delay(long value)
94 BOOL DelayReached(long *counter_var, int delay)
96 long actual_counter = Counter();
98 if (actual_counter >= *counter_var+delay || actual_counter < *counter_var)
100 *counter_var = actual_counter;
107 BOOL FrameReached(long *frame_counter_var, int frame_delay)
109 long actual_frame_counter = FrameCounter;
111 if (actual_frame_counter >= *frame_counter_var+frame_delay
112 || actual_frame_counter < *frame_counter_var)
114 *frame_counter_var = actual_frame_counter;
121 char *int2str(int ct, int nr)
125 sprintf(str,"%09d",ct);
126 return(&str[strlen(str)-nr]);
129 unsigned int SimpleRND(unsigned int max)
131 static unsigned long root = 654321;
132 struct timeval current_time;
134 gettimeofday(¤t_time,NULL);
135 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
139 unsigned int RND(unsigned int max)
141 return(random_linux_libc() % max);
144 unsigned int InitRND(long seed)
146 struct timeval current_time;
148 if (seed==NEW_RANDOMIZE)
150 gettimeofday(¤t_time,NULL);
151 srandom_linux_libc((unsigned int) current_time.tv_usec);
152 return((unsigned int) current_time.tv_usec);
156 srandom_linux_libc((unsigned int) seed);
157 return((unsigned int) seed);
165 if (!(pwd=getpwuid(getuid())))
168 return(pwd->pw_name);