16 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
21 // =======================================================================
23 // =======================================================================
24 void Sys_Shutdown (void)
27 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
32 void Sys_Error (const char *error, ...)
35 char string[MAX_INPUTLINE];
37 // change stdin to non blocking
39 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
42 va_start (argptr,error);
43 dpvsnprintf (string, sizeof (string), error, argptr);
46 Con_Printf ("Quake Error: %s\n", string);
52 void Sys_PrintToTerminal(const char *text)
54 fprintf(stdout, "%s", text);
57 double Sys_DoubleTime (void)
59 static int first = true;
60 static double oldtime = 0.0, curtime = 0.0;
63 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
64 if (sys_usetimegettime.integer)
66 static int firsttimegettime = true;
69 // Windows 95/98/ME/NT/2000/XP
71 // reasonable accuracy (millisecond)
73 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
75 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
79 firsttimegettime = false;
82 newtime = (double) timeGetTime () / 1000.0;
86 // QueryPerformanceCounter
88 // Windows 95/98/ME/NT/2000/XP
90 // very accurate (CPU cycles)
92 // does not necessarily match realtime too well (tends to get faster and faster in win98)
93 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
95 LARGE_INTEGER PerformanceFreq;
96 LARGE_INTEGER PerformanceCount;
98 if (!QueryPerformanceFrequency (&PerformanceFreq))
100 Con_Printf ("No hardware timer available\n");
101 // fall back to timeGetTime
102 Cvar_SetValueQuick(&sys_usetimegettime, true);
103 return Sys_DoubleTime();
105 QueryPerformanceCounter (&PerformanceCount);
108 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
109 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
111 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
112 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
117 gettimeofday(&tp, NULL);
118 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.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);
134 curtime += newtime - oldtime;
140 char *Sys_ConsoleInput(void)
142 if (cls.state == ca_dedicated)
144 static char text[MAX_INPUTLINE];
145 static unsigned int len = 0;
171 if (len < sizeof (text) - 1)
180 struct timeval timeout;
182 FD_SET(0, &fdset); // stdin
185 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
187 len = read (0, text, sizeof(text));
190 // rip off the \n and terminate
200 void Sys_Sleep(int milliseconds)
202 if (milliseconds < 1)
207 usleep(milliseconds * 1000);
211 char *Sys_GetClipboardData (void)
216 void Sys_InitConsole (void)
220 void Sys_Init_Commands (void)
224 int main (int argc, char **argv)
226 double frameoldtime, framenewtime;
228 signal(SIGFPE, SIG_IGN);
231 com_argv = (const char **)argv;
234 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
239 frameoldtime = Sys_DoubleTime () - 0.1;
242 // find time spent rendering last frame
243 framenewtime = Sys_DoubleTime ();
245 Host_Frame (framenewtime - frameoldtime);
247 frameoldtime = framenewtime;