16 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
21 // =======================================================================
23 // =======================================================================
29 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
35 void Sys_Error (const char *error, ...)
40 // change stdin to non blocking
42 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
45 va_start (argptr,error);
46 vsnprintf (string, sizeof (string), error, argptr);
48 fprintf(stderr, "Error: %s\n", string);
54 void Sys_Print(const char *text)
59 double Sys_DoubleTime (void)
61 static int first = true;
62 static double oldtime = 0.0, curtime = 0.0;
65 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
66 if (sys_usetimegettime.integer)
68 static int firsttimegettime = true;
71 // Windows 95/98/ME/NT/2000/XP
73 // reasonable accuracy (millisecond)
75 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
77 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
81 firsttimegettime = false;
84 newtime = (double) timeGetTime () / 1000.0;
88 // QueryPerformanceCounter
90 // Windows 95/98/ME/NT/2000/XP
92 // very accurate (CPU cycles)
94 // does not necessarily match realtime too well (tends to get faster and faster in win98)
95 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
97 LARGE_INTEGER PerformanceFreq;
98 LARGE_INTEGER PerformanceCount;
100 if (!QueryPerformanceFrequency (&PerformanceFreq))
101 Sys_Error ("No hardware timer available");
102 QueryPerformanceCounter (&PerformanceCount);
105 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
106 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
108 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
109 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
114 gettimeofday(&tp, NULL);
115 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
124 if (newtime < oldtime)
126 // warn if it's significant
127 if (newtime - oldtime < -0.01)
128 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
131 curtime += newtime - oldtime;
137 char *Sys_ConsoleInput(void)
139 if (cls.state == ca_dedicated)
141 static char text[256];
172 if (len == sizeof (text))
177 struct timeval timeout;
179 FD_SET(0, &fdset); // stdin
182 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
184 len = read (0, text, sizeof(text));
187 // rip off the \n and terminate
197 void Sys_Sleep(int milliseconds)
199 if (milliseconds < 1)
204 usleep(milliseconds * 1000);
208 int main (int argc, const char **argv)
210 double frameoldtime, framenewtime;
212 signal(SIGFPE, SIG_IGN);
218 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
221 Sys_Shared_EarlyInit();
225 Sys_Shared_LateInit();
227 frameoldtime = Sys_DoubleTime () - 0.1;
230 // find time spent rendering last frame
231 framenewtime = Sys_DoubleTime ();
233 Host_Frame (framenewtime - frameoldtime);
235 frameoldtime = framenewtime;