17 cvar_t sys_usenoclockbutbenchmark = {CVAR_SAVE, "sys_usenoclockbutbenchmark", "0", "don't use ANY real timing, and simulate a clock (for benchmarking); the game then runs as fast as possible. Run a QC mod with bots that does some stuff, then does a quit at the end, to benchmark a server. NEVER do this on a public server."};
18 static unsigned long benchmark_time;
22 cvar_t sys_useclockgettime = {CVAR_SAVE, "sys_useclockgettime", "0", "use POSIX clock_gettime function (which has issues if the system clock speed is far off, as it can't get fixed by NTP) for timing rather than gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations)"};
24 cvar_t sys_usegettimeofday = {CVAR_SAVE, "sys_usegettimeofday", "0", "use gettimeofday (which has issues if the system time is stepped by ntpdate, or apparently on some Xen installations) which has microsecond precision rather than SDL_GetTicks (which has only millisecond precision)"};
28 // =======================================================================
30 // =======================================================================
32 void Sys_Shutdown (void)
35 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
42 void Sys_Error (const char *error, ...)
45 char string[MAX_INPUTLINE];
47 // change stdin to non blocking
49 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
52 va_start (argptr,error);
53 dpvsnprintf (string, sizeof (string), error, argptr);
56 Con_Printf ("Quake Error: %s\n", string);
62 void Sys_PrintToTerminal(const char *text)
65 // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
66 int origflags = fcntl (1, F_GETFL, 0);
67 fcntl (1, F_SETFL, origflags & ~FNDELAY);
73 int written = (int)write(1, text, (int)strlen(text));
75 break; // sorry, I cannot do anything about this error - without an output
79 fcntl (1, F_SETFL, origflags);
81 //fprintf(stdout, "%s", text);
84 double Sys_DoubleTime (void)
86 static int first = true;
87 static double oldtime = 0.0, curtime = 0.0;
89 if(sys_usenoclockbutbenchmark.integer)
92 return ((double) benchmark_time) / 1e6;
94 // use higher precision timers on some platforms
97 if (sys_useclockgettime.integer)
101 clock_gettime(CLOCK_HIGHRES, &ts);
103 clock_gettime(CLOCK_MONOTONIC, &ts);
105 newtime = (double) ts.tv_sec + ts.tv_nsec / 1000000000.0;
107 else if (sys_usegettimeofday.integer)
111 gettimeofday(&tp, NULL);
112 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
117 newtime = (double) SDL_GetTicks() / 1000.0;
127 if (newtime < oldtime)
129 // warn if it's significant
130 if (newtime - oldtime < -0.01)
131 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
133 else if (newtime > oldtime + 1800)
135 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
138 curtime += newtime - oldtime;
144 char *Sys_ConsoleInput(void)
146 if (cls.state == ca_dedicated)
148 static char text[MAX_INPUTLINE];
179 if (len == sizeof (text))
184 struct timeval timeout;
186 FD_SET(0, &fdset); // stdin
189 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
191 len = read (0, text, sizeof(text));
194 // rip off the \n and terminate
204 void Sys_Sleep(int microseconds)
206 if(sys_usenoclockbutbenchmark.integer)
208 benchmark_time += microseconds;
211 SDL_Delay(microseconds / 1000);
214 char *Sys_GetClipboardData (void)
220 if (OpenClipboard (NULL) != 0)
222 HANDLE hClipboardData;
224 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
226 if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
229 allocsize = GlobalSize (hClipboardData) + 1;
230 data = (char *)Z_Malloc (allocsize);
231 strlcpy (data, cliptext, allocsize);
232 GlobalUnlock (hClipboardData);
243 void Sys_InitConsole (void)
247 void Sys_Init_Commands (void)
249 Cvar_RegisterVariable(&sys_usenoclockbutbenchmark);
252 Cvar_RegisterVariable(&sys_useclockgettime);
254 Cvar_RegisterVariable(&sys_usegettimeofday);
258 int main (int argc, char *argv[])
260 signal(SIGFPE, SIG_IGN);
263 com_argv = (const char **)argv;
266 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
269 // we don't know which systems we'll want to init, yet...