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, ...)
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);
45 fprintf(stderr, "Error: %s\n", string);
47 Con_Print ("Quake Error: ");
55 void Sys_PrintToTerminal(const char *text)
60 double Sys_DoubleTime (void)
62 static int first = true;
63 static double oldtime = 0.0, curtime = 0.0;
66 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
67 if (sys_usetimegettime.integer)
69 static int firsttimegettime = true;
72 // Windows 95/98/ME/NT/2000/XP
74 // reasonable accuracy (millisecond)
76 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
78 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
82 firsttimegettime = false;
85 newtime = (double) timeGetTime () / 1000.0;
89 // QueryPerformanceCounter
91 // Windows 95/98/ME/NT/2000/XP
93 // very accurate (CPU cycles)
95 // does not necessarily match realtime too well (tends to get faster and faster in win98)
96 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
98 LARGE_INTEGER PerformanceFreq;
99 LARGE_INTEGER PerformanceCount;
101 if (!QueryPerformanceFrequency (&PerformanceFreq))
102 Sys_Error ("No hardware timer available");
103 QueryPerformanceCounter (&PerformanceCount);
106 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
107 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
109 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
110 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
115 gettimeofday(&tp, NULL);
116 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
125 if (newtime < oldtime)
127 // warn if it's significant
128 if (newtime - oldtime < -0.01)
129 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
132 curtime += newtime - oldtime;
138 char *Sys_ConsoleInput(void)
140 if (cls.state == ca_dedicated)
142 static char text[256];
169 if (len < sizeof (text) - 1)
178 struct timeval timeout;
180 FD_SET(0, &fdset); // stdin
183 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
185 len = read (0, text, sizeof(text));
188 // rip off the \n and terminate
198 void Sys_Sleep(int milliseconds)
200 if (milliseconds < 1)
205 usleep(milliseconds * 1000);
209 char *Sys_GetClipboardData (void)
214 void Sys_InitConsole (void)
218 void Sys_Init_Commands (void)
222 int main (int argc, char **argv)
224 double frameoldtime, framenewtime;
226 signal(SIGFPE, SIG_IGN);
229 com_argv = (const char **)argv;
232 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
237 frameoldtime = Sys_DoubleTime () - 0.1;
240 // find time spent rendering last frame
241 framenewtime = Sys_DoubleTime ();
243 Host_Frame (framenewtime - frameoldtime);
245 frameoldtime = framenewtime;