17 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1", "use windows timeGetTime function (which has issues on some motherboards) for timing rather than QueryPerformanceCounter timer (which has issues on multicore/multiprocessor machines and processors which are designed to conserve power)"};
22 // =======================================================================
24 // =======================================================================
25 void Sys_Shutdown (void)
28 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
33 void Sys_Error (const char *error, ...)
36 char string[MAX_INPUTLINE];
38 // change stdin to non blocking
40 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
43 va_start (argptr,error);
44 dpvsnprintf (string, sizeof (string), error, argptr);
47 Con_Printf ("Quake Error: %s\n", string);
53 void Sys_PrintToTerminal(const char *text)
56 // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
57 int origflags = fcntl (1, F_GETFL, 0);
58 fcntl (1, F_SETFL, origflags & ~FNDELAY);
62 int written = (int)write(1, text, (int)strlen(text));
64 break; // sorry, I cannot do anything about this error - without an output
68 fcntl (1, F_SETFL, origflags);
70 //fprintf(stdout, "%s", text);
73 double Sys_DoubleTime (void)
75 static int first = true;
76 static double oldtime = 0.0, curtime = 0.0;
80 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
81 if (sys_usetimegettime.integer)
83 static int firsttimegettime = true;
86 // Windows 95/98/ME/NT/2000/XP
88 // reasonable accuracy (millisecond)
90 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
92 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
96 firsttimegettime = false;
99 newtime = (double) timeGetTime () / 1000.0;
103 // QueryPerformanceCounter
105 // Windows 95/98/ME/NT/2000/XP
107 // very accurate (CPU cycles)
109 // does not necessarily match realtime too well (tends to get faster and faster in win98)
110 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
112 LARGE_INTEGER PerformanceFreq;
113 LARGE_INTEGER PerformanceCount;
115 if (!QueryPerformanceFrequency (&PerformanceFreq))
117 Con_Printf ("No hardware timer available\n");
118 // fall back to timeGetTime
119 Cvar_SetValueQuick(&sys_usetimegettime, true);
120 return Sys_DoubleTime();
122 QueryPerformanceCounter (&PerformanceCount);
125 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
126 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
128 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
129 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
134 gettimeofday(&tp, NULL);
135 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
144 if (newtime < oldtime)
146 // warn if it's significant
147 if (newtime - oldtime < -0.01)
148 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
151 curtime += newtime - oldtime;
157 char *Sys_ConsoleInput(void)
159 if (cls.state == ca_dedicated)
161 static char text[MAX_INPUTLINE];
162 static unsigned int len = 0;
188 if (len < sizeof (text) - 1)
197 struct timeval timeout;
199 FD_SET(0, &fdset); // stdin
202 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
204 len = read (0, text, sizeof(text));
207 // rip off the \n and terminate
217 void Sys_Sleep(int microseconds)
220 if (microseconds < 1000)
222 Sleep(microseconds / 1000);
224 if (microseconds < 1)
226 usleep(microseconds);
230 char *Sys_GetClipboardData (void)
235 void Sys_InitConsole (void)
239 void Sys_Init_Commands (void)
243 int main (int argc, char **argv)
245 signal(SIGFPE, SIG_IGN);
248 com_argv = (const char **)argv;
251 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);