]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_linux.c
slight cleanup (complete removal) of the "base" variable in surface rendering
[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 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         dpvsnprintf (string, sizeof (string), error, argptr);
44         va_end (argptr);
45
46         Con_Printf ("Quake Error: %s\n", string);
47
48         Host_Shutdown ();
49         exit (1);
50 }
51
52 void Sys_PrintToTerminal(const char *text)
53 {
54         fprintf(stdout, "%s", text);
55 }
56
57 double Sys_DoubleTime (void)
58 {
59         static int first = true;
60         static double oldtime = 0.0, curtime = 0.0;
61         double newtime;
62 #ifdef WIN32
63         // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
64         if (sys_usetimegettime.integer)
65         {
66                 static int firsttimegettime = true;
67                 // timeGetTime
68                 // platform:
69                 // Windows 95/98/ME/NT/2000/XP
70                 // features:
71                 // reasonable accuracy (millisecond)
72                 // issues:
73                 // 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
75                 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
76                 if (firsttimegettime)
77                 {
78                         timeBeginPeriod (1);
79                         firsttimegettime = false;
80                 }
81
82                 newtime = (double) timeGetTime () / 1000.0;
83         }
84         else
85         {
86                 // QueryPerformanceCounter
87                 // platform:
88                 // Windows 95/98/ME/NT/2000/XP
89                 // features:
90                 // very accurate (CPU cycles)
91                 // known issues:
92                 // does not necessarily match realtime too well (tends to get faster and faster in win98)
93                 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
94                 double timescale;
95                 LARGE_INTEGER PerformanceFreq;
96                 LARGE_INTEGER PerformanceCount;
97
98                 if (!QueryPerformanceFrequency (&PerformanceFreq))
99                         Sys_Error ("No hardware timer available");
100                 QueryPerformanceCounter (&PerformanceCount);
101
102                 #ifdef __BORLANDC__
103                 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
104                 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
105                 #else
106                 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
107                 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
108                 #endif
109         }
110 #else
111         struct timeval tp;
112         gettimeofday(&tp, NULL);
113         newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
114 #endif
115
116         if (first)
117         {
118                 first = false;
119                 oldtime = newtime;
120         }
121
122         if (newtime < oldtime)
123         {
124                 // warn if it's significant
125                 if (newtime - oldtime < -0.01)
126                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
127         }
128         else
129                 curtime += newtime - oldtime;
130         oldtime = newtime;
131
132         return curtime;
133 }
134
135 char *Sys_ConsoleInput(void)
136 {
137         if (cls.state == ca_dedicated)
138         {
139                 static char text[256];
140                 static int len = 0;
141 #ifdef WIN32
142                 int c;
143
144                 // read a line out
145                 while (_kbhit ())
146                 {
147                         c = _getch ();
148                         if (c == '\r')
149                         {
150                                 text[len] = '\0';
151                                 putch ('\n');
152                                 len = 0;
153                                 return text;
154                         }
155                         if (c == '\b')
156                         {
157                                 if (len)
158                                 {
159                                         putch (c);
160                                         putch (' ');
161                                         putch (c);
162                                         len--;
163                                 }
164                                 continue;
165                         }
166                         if (len < sizeof (text) - 1)
167                         {
168                                 putch (c);
169                                 text[len] = c;
170                                 len++;
171                         }
172                 }
173 #else
174                 fd_set fdset;
175                 struct timeval timeout;
176                 FD_ZERO(&fdset);
177                 FD_SET(0, &fdset); // stdin
178                 timeout.tv_sec = 0;
179                 timeout.tv_usec = 0;
180                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
181                 {
182                         len = read (0, text, sizeof(text));
183                         if (len >= 1)
184                         {
185                                 // rip off the \n and terminate
186                                 text[len-1] = 0;
187                                 return text;
188                         }
189                 }
190 #endif
191         }
192         return NULL;
193 }
194
195 void Sys_Sleep(int milliseconds)
196 {
197         if (milliseconds < 1)
198                 milliseconds = 1;
199 #ifdef WIN32
200         Sleep(milliseconds);
201 #else
202         usleep(milliseconds * 1000);
203 #endif
204 }
205
206 char *Sys_GetClipboardData (void)
207 {
208         return NULL;
209 }
210
211 void Sys_InitConsole (void)
212 {
213 }
214
215 void Sys_Init_Commands (void)
216 {
217 }
218
219 int main (int argc, char **argv)
220 {
221         double frameoldtime, framenewtime;
222
223         signal(SIGFPE, SIG_IGN);
224
225         com_argc = argc;
226         com_argv = (const char **)argv;
227
228 #ifndef WIN32
229         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
230 #endif
231
232         Host_Init();
233
234         frameoldtime = Sys_DoubleTime () - 0.1;
235         while (1)
236         {
237                 // find time spent rendering last frame
238                 framenewtime = Sys_DoubleTime ();
239
240                 Host_Frame (framenewtime - frameoldtime);
241
242                 frameoldtime = framenewtime;
243         }
244         return 0;
245 }