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