INCOMPLETE SDL video support (don't use it!) No input, no multiple video
[divverent/darkplaces.git] / sys_sdl.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 #include <SDL.h>
15
16 #ifdef WIN32
17 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
18 #endif
19
20
21
22 // =======================================================================
23 // General routines
24 // =======================================================================
25
26 void Sys_Quit (void)
27 {
28         Host_Shutdown();
29 #ifndef WIN32
30         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
31 #endif
32         fflush(stdout);
33         exit(0);
34 }
35
36 void Sys_Error (const char *error, ...)
37 {
38         va_list argptr;
39         char string[1024];
40
41 // change stdin to non blocking
42 #ifndef WIN32
43         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
44 #endif
45
46         va_start (argptr,error);
47         vsnprintf (string, sizeof (string), error, argptr);
48         va_end (argptr);
49         fprintf(stderr, "Error: %s\n", string);
50
51         Host_Shutdown ();
52         exit (1);
53 }
54
55 void Sys_Print(const char *text)
56 {
57         printf("%s", text);
58 }
59
60 double Sys_DoubleTime (void)
61 {
62         static int first = true;
63         static double oldtime = 0.0, curtime = 0.0;
64         double newtime;
65 #ifdef WIN32
66         // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
67         if (sys_usetimegettime.integer)
68         {
69                 static int firsttimegettime = true;
70                 // timeGetTime
71                 // platform:
72                 // Windows 95/98/ME/NT/2000/XP
73                 // features:
74                 // reasonable accuracy (millisecond)
75                 // issues:
76                 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
77
78                 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
79                 if (firsttimegettime)
80                 {
81                         timeBeginPeriod (1);
82                         firsttimegettime = false;
83                 }
84
85                 newtime = (double) timeGetTime () / 1000.0;
86         }
87         else
88         {
89                 // QueryPerformanceCounter
90                 // platform:
91                 // Windows 95/98/ME/NT/2000/XP
92                 // features:
93                 // very accurate (CPU cycles)
94                 // known issues:
95                 // does not necessarily match realtime too well (tends to get faster and faster in win98)
96                 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
97                 double timescale;
98                 LARGE_INTEGER PerformanceFreq;
99                 LARGE_INTEGER PerformanceCount;
100
101                 if (!QueryPerformanceFrequency (&PerformanceFreq))
102                         Sys_Error ("No hardware timer available");
103                 QueryPerformanceCounter (&PerformanceCount);
104
105                 #ifdef __BORLANDC__
106                 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
107                 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
108                 #else
109                 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
110                 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
111                 #endif
112         }
113 #else
114         struct timeval tp;
115         gettimeofday(&tp, NULL);
116         newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0;
117 #endif
118
119         if (first)
120         {
121                 first = false;
122                 oldtime = newtime;
123         }
124
125         if (newtime < oldtime)
126         {
127                 // warn if it's significant
128                 if (newtime - oldtime < -0.01)
129                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
130         }
131         else
132                 curtime += newtime - oldtime;
133         oldtime = newtime;
134
135         return curtime;
136 }
137
138 char *Sys_ConsoleInput(void)
139 {
140         if (cls.state == ca_dedicated)
141         {
142                 static char text[256];
143                 int len = 0;
144 #ifdef WIN32
145                 int c;
146
147                 // read a line out
148                 while (_kbhit ())
149                 {
150                         c = _getch ();
151                         putch (c);
152                         if (c == '\r')
153                         {
154                                 text[len] = 0;
155                                 putch ('\n');
156                                 len = 0;
157                                 return text;
158                         }
159                         if (c == 8)
160                         {
161                                 if (len)
162                                 {
163                                         putch (' ');
164                                         putch (c);
165                                         len--;
166                                         text[len] = 0;
167                                 }
168                                 continue;
169                         }
170                         text[len] = c;
171                         len++;
172                         text[len] = 0;
173                         if (len == sizeof (text))
174                                 len = 0;
175                 }
176 #else
177                 fd_set fdset;
178                 struct timeval timeout;
179                 FD_ZERO(&fdset);
180                 FD_SET(0, &fdset); // stdin
181                 timeout.tv_sec = 0;
182                 timeout.tv_usec = 0;
183                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
184                 {
185                         len = read (0, text, sizeof(text));
186                         if (len >= 1)
187                         {
188                                 // rip off the \n and terminate
189                                 text[len-1] = 0;
190                                 return text;
191                         }
192                 }
193 #endif
194         }
195         return NULL;
196 }
197
198 void Sys_Sleep(void)
199 {
200 #ifdef WIN32
201         Sleep (1);
202 #else
203         usleep(1);
204 #endif
205 }
206
207 int SDL_main (int argc, char *argv[])
208 {
209         double frameoldtime, framenewtime;
210
211         signal(SIGFPE, SIG_IGN);
212
213         com_argc = argc;
214         com_argv = argv;
215
216 #ifndef WIN32
217         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
218 #endif
219
220         Sys_Shared_EarlyInit();
221
222         Host_Init();
223
224         Sys_Shared_LateInit();
225
226         frameoldtime = Sys_DoubleTime () - 0.1;
227         while (1)
228         {
229                 // find time spent rendering last frame
230                 framenewtime = Sys_DoubleTime ();
231
232                 Host_Frame (framenewtime - frameoldtime);
233
234                 frameoldtime = framenewtime;
235         }
236         return 0;
237 }