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 vsnprintf (string, sizeof (string), error, argptr);
45 fprintf(stderr, "Error: %s\n", string);
51 void Sys_PrintToTerminal(const char *text)
56 double Sys_DoubleTime (void)
58 static int first = true;
59 static double oldtime = 0.0, curtime = 0.0;
62 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
63 if (sys_usetimegettime.integer)
65 static int firsttimegettime = true;
68 // Windows 95/98/ME/NT/2000/XP
70 // reasonable accuracy (millisecond)
72 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
74 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
78 firsttimegettime = false;
81 newtime = (double) timeGetTime () / 1000.0;
85 // QueryPerformanceCounter
87 // Windows 95/98/ME/NT/2000/XP
89 // very accurate (CPU cycles)
91 // does not necessarily match realtime too well (tends to get faster and faster in win98)
92 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
94 LARGE_INTEGER PerformanceFreq;
95 LARGE_INTEGER PerformanceCount;
97 if (!QueryPerformanceFrequency (&PerformanceFreq))
98 Sys_Error ("No hardware timer available");
99 QueryPerformanceCounter (&PerformanceCount);
102 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
103 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
105 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
106 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
111 gettimeofday(&tp, NULL);
112 newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
121 if (newtime < oldtime)
123 // warn if it's significant
124 if (newtime - oldtime < -0.01)
125 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
128 curtime += newtime - oldtime;
134 char *Sys_ConsoleInput(void)
136 if (cls.state == ca_dedicated)
138 static char text[256];
165 if (len < sizeof (text) - 1)
174 struct timeval timeout;
176 FD_SET(0, &fdset); // stdin
179 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
181 len = read (0, text, sizeof(text));
184 // rip off the \n and terminate
194 void Sys_Sleep(int milliseconds)
196 if (milliseconds < 1)
201 usleep(milliseconds * 1000);
205 int main (int argc, const char **argv)
207 double frameoldtime, framenewtime;
209 signal(SIGFPE, SIG_IGN);
215 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
218 Sys_Shared_EarlyInit();
222 Sys_Shared_LateInit();
224 frameoldtime = Sys_DoubleTime () - 0.1;
227 // find time spent rendering last frame
228 framenewtime = Sys_DoubleTime ();
230 Host_Frame (framenewtime - frameoldtime);
232 frameoldtime = framenewtime;