17 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
20 // =======================================================================
22 // =======================================================================
24 void Sys_Shutdown (void)
27 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
34 void Sys_Error (const char *error, ...)
39 // change stdin to non blocking
41 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
44 va_start (argptr,error);
45 vsnprintf (string, sizeof (string), error, argptr);
47 fprintf(stderr, "Error: %s\n", string);
53 void Sys_PrintToTerminal(const char *text)
58 double Sys_DoubleTime (void)
60 static int first = true;
61 static double oldtime = 0.0, curtime = 0.0;
64 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
65 if (!sys_usetimegettime.integer)
67 // QueryPerformanceCounter
69 // Windows 95/98/ME/NT/2000/XP
71 // very accurate (CPU cycles)
73 // does not necessarily match realtime too well (tends to get faster and faster in win98)
74 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
76 LARGE_INTEGER PerformanceFreq;
77 LARGE_INTEGER PerformanceCount;
79 if (!QueryPerformanceFrequency (&PerformanceFreq))
80 Sys_Error ("No hardware timer available");
81 QueryPerformanceCounter (&PerformanceCount);
84 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
85 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
87 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
88 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
92 newtime = (double) SDL_GetTicks() / 1000.0;
101 if (newtime < oldtime)
103 // warn if it's significant
104 if (newtime - oldtime < -0.01)
105 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
108 curtime += newtime - oldtime;
114 char *Sys_ConsoleInput(void)
116 if (cls.state == ca_dedicated)
118 static char text[256];
149 if (len == sizeof (text))
154 struct timeval timeout;
156 FD_SET(0, &fdset); // stdin
159 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
161 len = read (0, text, sizeof(text));
164 // rip off the \n and terminate
174 void Sys_Sleep(int milliseconds)
176 if (milliseconds < 1)
178 SDL_Delay(milliseconds);
181 int SDL_main (int argc, char *argv[])
183 double frameoldtime, framenewtime;
185 signal(SIGFPE, SIG_IGN);
191 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
194 Sys_Shared_EarlyInit();
197 Cvar_RegisterVariable(&sys_usetimegettime);
202 Sys_Shared_LateInit();
204 frameoldtime = Sys_DoubleTime () - 0.1;
207 // find time spent rendering last frame
208 framenewtime = Sys_DoubleTime ();
210 Host_Frame (framenewtime - frameoldtime);
212 frameoldtime = framenewtime;