16 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)"};
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)
55 // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
56 int origflags = fcntl (1, F_GETFL, 0);
57 fcntl (1, F_SETFL, origflags & ~FNDELAY);
61 ssize_t written = write(1, text, strlen(text));
63 break; // sorry, I cannot do anything about this error - without an output
67 fcntl (1, F_SETFL, origflags);
69 //fprintf(stdout, "%s", text);
72 double Sys_DoubleTime (void)
74 static int first = true;
75 static double oldtime = 0.0, curtime = 0.0;
78 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
79 if (sys_usetimegettime.integer)
81 static int firsttimegettime = true;
84 // Windows 95/98/ME/NT/2000/XP
86 // reasonable accuracy (millisecond)
88 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
90 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
94 firsttimegettime = false;
97 newtime = (double) timeGetTime () / 1000.0;
101 // QueryPerformanceCounter
103 // Windows 95/98/ME/NT/2000/XP
105 // very accurate (CPU cycles)
107 // does not necessarily match realtime too well (tends to get faster and faster in win98)
108 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
110 LARGE_INTEGER PerformanceFreq;
111 LARGE_INTEGER PerformanceCount;
113 if (!QueryPerformanceFrequency (&PerformanceFreq))
115 Con_Printf ("No hardware timer available\n");
116 // fall back to timeGetTime
117 Cvar_SetValueQuick(&sys_usetimegettime, true);
118 return Sys_DoubleTime();
120 QueryPerformanceCounter (&PerformanceCount);
123 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
124 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
126 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
127 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
132 gettimeofday(&tp, NULL);
133 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
142 if (newtime < oldtime)
144 // warn if it's significant
145 if (newtime - oldtime < -0.01)
146 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
149 curtime += newtime - oldtime;
155 char *Sys_ConsoleInput(void)
157 if (cls.state == ca_dedicated)
159 static char text[MAX_INPUTLINE];
160 static unsigned int len = 0;
186 if (len < sizeof (text) - 1)
195 struct timeval timeout;
197 FD_SET(0, &fdset); // stdin
200 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
202 len = read (0, text, sizeof(text));
205 // rip off the \n and terminate
215 void Sys_Sleep(int milliseconds)
217 if (milliseconds < 1)
222 usleep(milliseconds * 1000);
226 char *Sys_GetClipboardData (void)
231 void Sys_InitConsole (void)
235 void Sys_Init_Commands (void)
239 int main (int argc, char **argv)
241 double frameoldtime, framenewtime;
243 signal(SIGFPE, SIG_IGN);
246 com_argv = (const char **)argv;
249 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
254 frameoldtime = Sys_DoubleTime () - 0.1;
257 // find time spent rendering last frame
258 framenewtime = Sys_DoubleTime ();
260 Host_Frame (framenewtime - frameoldtime);
262 frameoldtime = framenewtime;