]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_linux.c
fix some glitches in the original commit
[divverent/darkplaces.git] / sys_linux.c
1
2 #ifdef WIN32
3 #include "conio.h"
4 #else
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/time.h>
8 #endif
9
10 #include <signal.h>
11
12 #include "quakedef.h"
13
14
15 #ifdef WIN32
16 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
17 #endif
18
19
20
21 // =======================================================================
22 // General routines
23 // =======================================================================
24
25 void Sys_Quit (void)
26 {
27         Host_Shutdown();
28 #ifndef WIN32
29         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
30 #endif
31         fflush(stdout);
32         exit(0);
33 }
34
35 void Sys_Error (const char *error, ...)
36 {
37         va_list argptr;
38         char string[1024];
39
40 // change stdin to non blocking
41 #ifndef WIN32
42         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
43 #endif
44
45         va_start (argptr,error);
46         vsprintf (string,error,argptr);
47         va_end (argptr);
48         fprintf(stderr, "Error: %s\n", string);
49
50         Host_Shutdown ();
51         exit (1);
52 }
53
54 void Sys_Print(const char *text)
55 {
56         printf("%s", text);
57 }
58
59 double Sys_DoubleTime (void)
60 {
61         static int first = true;
62         static double oldtime = 0.0, curtime = 0.0;
63         double newtime;
64 #ifdef WIN32
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)
67         {
68                 static int firsttimegettime = true;
69                 // timeGetTime
70                 // platform:
71                 // Windows 95/98/ME/NT/2000/XP
72                 // features:
73                 // reasonable accuracy (millisecond)
74                 // issues:
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)
76
77                 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
78                 if (firsttimegettime)
79                 {
80                         timeBeginPeriod (1);
81                         firsttimegettime = false;
82                 }
83
84                 newtime = (double) timeGetTime () / 1000.0;
85         }
86         else
87         {
88                 // QueryPerformanceCounter
89                 // platform:
90                 // Windows 95/98/ME/NT/2000/XP
91                 // features:
92                 // very accurate (CPU cycles)
93                 // known issues:
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)
96                 double timescale;
97                 LARGE_INTEGER PerformanceFreq;
98                 LARGE_INTEGER PerformanceCount;
99
100                 if (!QueryPerformanceFrequency (&PerformanceFreq))
101                         Sys_Error ("No hardware timer available");
102                 QueryPerformanceCounter (&PerformanceCount);
103
104                 #ifdef __BORLANDC__
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;
107                 #else
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;
110                 #endif
111         }
112 #else
113         struct timeval tp;
114         gettimeofday(&tp, NULL);
115         newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
116 #endif
117
118         if (first)
119         {
120                 first = false;
121                 oldtime = newtime;
122         }
123
124         if (newtime < oldtime)
125         {
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);
129         }
130         else
131                 curtime += newtime - oldtime;
132         oldtime = newtime;
133
134         return curtime;
135 }
136
137 char *Sys_ConsoleInput(void)
138 {
139         if (cls.state == ca_dedicated)
140         {
141                 static char text[256];
142                 int len = 0;
143 #ifdef WIN32
144                 int c;
145
146                 // read a line out
147                 while (_kbhit ())
148                 {
149                         c = _getch ();
150                         putch (c);
151                         if (c == '\r')
152                         {
153                                 text[len] = 0;
154                                 putch ('\n');
155                                 len = 0;
156                                 return text;
157                         }
158                         if (c == 8)
159                         {
160                                 if (len)
161                                 {
162                                         putch (' ');
163                                         putch (c);
164                                         len--;
165                                         text[len] = 0;
166                                 }
167                                 continue;
168                         }
169                         text[len] = c;
170                         len++;
171                         text[len] = 0;
172                         if (len == sizeof (text))
173                                 len = 0;
174                 }
175 #else
176                 fd_set fdset;
177                 struct timeval timeout;
178                 FD_ZERO(&fdset);
179                 FD_SET(0, &fdset); // stdin
180                 timeout.tv_sec = 0;
181                 timeout.tv_usec = 0;
182                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
183                 {
184                         len = read (0, text, sizeof(text));
185                         if (len >= 1)
186                         {
187                                 // rip off the \n and terminate
188                                 text[len-1] = 0;
189                                 return text;
190                         }
191                 }
192 #endif
193         }
194         return NULL;
195 }
196
197 void Sys_Sleep(void)
198 {
199 #ifdef WIN32
200         Sleep (1);
201 #else
202         usleep(1);
203 #endif
204 }
205
206 int main (int argc, const char **argv)
207 {
208         double frameoldtime, framenewtime;
209
210         signal(SIGFPE, SIG_IGN);
211
212         com_argc = argc;
213         com_argv = argv;
214
215 #ifndef WIN32
216         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
217 #endif
218
219         Sys_Shared_EarlyInit();
220
221         Host_Init();
222
223         Sys_Shared_LateInit();
224
225         frameoldtime = Sys_DoubleTime () - 0.1;
226         while (1)
227         {
228                 // find time spent rendering last frame
229                 framenewtime = Sys_DoubleTime ();
230
231                 Host_Frame (framenewtime - frameoldtime);
232
233                 frameoldtime = framenewtime;
234         }
235         return 0;
236 }