/***********************************************************
* Rocks'n'Diamonds -- McDuffin Strikes Back! *
*----------------------------------------------------------*
-* ©1995 Artsoft Development *
-* Holger Schemel *
-* 33659 Bielefeld-Senne *
-* Telefon: (0521) 493245 *
-* eMail: aeglos@valinor.owl.de *
-* aeglos@uni-paderborn.de *
-* q99492@pbhrzx.uni-paderborn.de *
+* (c) 1995-98 Artsoft Entertainment *
+* Holger Schemel *
+* Oststrasse 11a *
+* 33604 Bielefeld *
+* phone: ++49 +521 290471 *
+* email: aeglos@valinor.owl.de *
*----------------------------------------------------------*
* misc.c *
***********************************************************/
-#include "misc.h"
-#include "tools.h"
-#include "sound.h"
-#include "random.h"
-
#include <pwd.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/types.h>
+#include <stdarg.h>
+
+#include "misc.h"
+#include "init.h"
+#include "tools.h"
+#include "sound.h"
+#include "random.h"
static unsigned long mainCounter(int mode)
{
static void sleep_milliseconds(unsigned long milliseconds_delay)
{
- if (milliseconds_delay < 5 || !cpu_friendly)
+ if (milliseconds_delay < 5)
{
- /* we want to wait less than 5 ms -- if we assume that we have a
+ /* we want to wait only a few ms -- if we assume that we have a
kernel timer resolution of 10 ms, we would wait far to long;
therefore it's better to do a short interval of busy waiting
to get our sleeping time more accurate */
delay.tv_usec = 1000 * (milliseconds_delay % 1000);
if (select(0, NULL, NULL, NULL, &delay) != 0)
- fprintf(stderr,"%s: in function sleep_milliseconds: select() failed!\n",
- progname);
+ Error(ERR_RETURN, "sleep_milliseconds(): select() failed");
}
}
void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
{
- unsigned long actual_counter = Counter();
+ unsigned long actual_counter;
- if (actual_counter < *counter_var + delay && actual_counter >= *counter_var)
- sleep_milliseconds(*counter_var + delay - actual_counter);
+ while(1)
+ {
+ actual_counter = Counter();
+
+ if (actual_counter < *counter_var + delay &&
+ actual_counter >= *counter_var)
+ sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
+ else
+ break;
+ }
*counter_var = actual_counter;
}
redraw_mask |= REDRAW_TILES;
}
}
+
+void Error(BOOL fatal_error, char *format_str, ...)
+{
+ FILE *output_stream = stderr;
+ va_list ap;
+ char *format_ptr;
+ char *s_value;
+ int i_value;
+ double d_value;
+
+ va_start(ap, format_str); /* ap points to first unnamed argument */
+
+ fprintf(output_stream, "%s: ", program_name);
+
+ for(format_ptr=format_str; *format_ptr; format_ptr++)
+ {
+ if (*format_ptr != '%')
+ {
+ fprintf(output_stream, "%c", *format_ptr);
+ continue;
+ }
+
+ switch(*++format_ptr)
+ {
+ case 'd':
+ i_value = va_arg(ap, int);
+ fprintf(output_stream, "%d", i_value);
+ break;
+
+ case 'f':
+ d_value = va_arg(ap, double);
+ fprintf(output_stream, "%f", d_value);
+ break;
+
+ case 's':
+ s_value = va_arg(ap, char *);
+ fprintf(output_stream, "%s", s_value);
+ break;
+
+ default:
+ fprintf(stderr, "\nfatal(): invalid format string: %s\n", format_str);
+ exit(-1);
+ }
+ }
+
+ va_end(ap);
+
+ fprintf(output_stream, "\n");
+
+ if (fatal_error)
+ {
+ fprintf(output_stream, "%s: aborting\n", program_name);
+ CloseAll();
+ exit(1);
+ }
+}