various printf/fprintf calls are now Con_Printf, and some other minor cleanups
[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 // =======================================================================
17 // General routines
18 // =======================================================================
19
20 void Sys_Shutdown (void)
21 {
22 #ifndef WIN32
23         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
24 #endif
25         fflush(stdout);
26         SDL_Quit();
27 }
28
29
30 void Sys_Error (const char *error, ...)
31 {
32         va_list argptr;
33         char string[1024];
34
35 // change stdin to non blocking
36 #ifndef WIN32
37         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
38 #endif
39
40         va_start (argptr,error);
41         dpvsnprintf (string, sizeof (string), error, argptr);
42         va_end (argptr);
43
44         Con_Printf ("Quake Error: %s\n", string);
45
46         Host_Shutdown ();
47         exit (1);
48 }
49
50 void Sys_PrintToTerminal(const char *text)
51 {
52         fprintf(stdout, "%s", text);
53 }
54
55 double Sys_DoubleTime (void)
56 {
57         static int first = true;
58         static double oldtime = 0.0, curtime = 0.0;
59         double newtime;
60         newtime = (double) SDL_GetTicks() / 1000.0;
61
62
63         if (first)
64         {
65                 first = false;
66                 oldtime = newtime;
67         }
68
69         if (newtime < oldtime)
70         {
71                 // warn if it's significant
72                 if (newtime - oldtime < -0.01)
73                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
74         }
75         else
76                 curtime += newtime - oldtime;
77         oldtime = newtime;
78
79         return curtime;
80 }
81
82 char *Sys_ConsoleInput(void)
83 {
84         if (cls.state == ca_dedicated)
85         {
86                 static char text[256];
87                 int len = 0;
88 #ifdef WIN32
89                 int c;
90
91                 // read a line out
92                 while (_kbhit ())
93                 {
94                         c = _getch ();
95                         putch (c);
96                         if (c == '\r')
97                         {
98                                 text[len] = 0;
99                                 putch ('\n');
100                                 len = 0;
101                                 return text;
102                         }
103                         if (c == 8)
104                         {
105                                 if (len)
106                                 {
107                                         putch (' ');
108                                         putch (c);
109                                         len--;
110                                         text[len] = 0;
111                                 }
112                                 continue;
113                         }
114                         text[len] = c;
115                         len++;
116                         text[len] = 0;
117                         if (len == sizeof (text))
118                                 len = 0;
119                 }
120 #else
121                 fd_set fdset;
122                 struct timeval timeout;
123                 FD_ZERO(&fdset);
124                 FD_SET(0, &fdset); // stdin
125                 timeout.tv_sec = 0;
126                 timeout.tv_usec = 0;
127                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
128                 {
129                         len = read (0, text, sizeof(text));
130                         if (len >= 1)
131                         {
132                                 // rip off the \n and terminate
133                                 text[len-1] = 0;
134                                 return text;
135                         }
136                 }
137 #endif
138         }
139         return NULL;
140 }
141
142 void Sys_Sleep(int milliseconds)
143 {
144         if (milliseconds < 1)
145                 milliseconds = 1;
146         SDL_Delay(milliseconds);
147 }
148
149 char *Sys_GetClipboardData (void)
150 {
151 #ifdef WIN32
152         char *data = NULL;
153         char *cliptext;
154
155         if (OpenClipboard (NULL) != 0)
156         {
157                 HANDLE hClipboardData;
158
159                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
160                 {
161                         if ((cliptext = GlobalLock (hClipboardData)) != 0)
162                         {
163                                 data = malloc (GlobalSize(hClipboardData)+1);
164                                 strcpy (data, cliptext);
165                                 GlobalUnlock (hClipboardData);
166                         }
167                 }
168                 CloseClipboard ();
169         }
170         return data;
171 #else
172         return NULL;
173 #endif
174 }
175
176 void Sys_InitConsole (void)
177 {
178 }
179
180 void Sys_Init_Commands (void)
181 {
182 }
183
184 int main (int argc, char *argv[])
185 {
186         double frameoldtime, framenewtime;
187
188         signal(SIGFPE, SIG_IGN);
189
190         com_argc = argc;
191         com_argv = (const char **)argv;
192
193 #ifndef WIN32
194         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
195 #endif
196
197         Host_Init();
198
199         frameoldtime = Sys_DoubleTime () - 0.1;
200         while (1)
201         {
202                 // find time spent rendering last frame
203                 framenewtime = Sys_DoubleTime ();
204
205                 Host_Frame (framenewtime - frameoldtime);
206
207                 frameoldtime = framenewtime;
208         }
209         return 0;
210 }