]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_sdl.c
only issue one clear for 2D/rect shadowmaps
[divverent/darkplaces.git] / sys_sdl.c
1 #include "quakedef.h"
2
3 #ifdef WIN32
4 #include <io.h>
5 #include "conio.h"
6 #else
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/time.h>
10 #endif
11
12 #include <signal.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[MAX_INPUTLINE];
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 #ifndef WIN32
53         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
54         int origflags = fcntl (1, F_GETFL, 0);
55         fcntl (1, F_SETFL, origflags & ~FNDELAY);
56 #else
57 #define write _write
58 #endif
59         while(*text)
60         {
61                 int written = (int)write(1, text, (int)strlen(text));
62                 if(written <= 0)
63                         break; // sorry, I cannot do anything about this error - without an output
64                 text += written;
65         }
66 #ifndef WIN32
67         fcntl (1, F_SETFL, origflags);
68 #endif
69         //fprintf(stdout, "%s", text);
70 }
71
72 double Sys_DoubleTime (void)
73 {
74         static int first = true;
75         static double oldtime = 0.0, curtime = 0.0;
76         double newtime;
77         newtime = (double) SDL_GetTicks() / 1000.0;
78
79
80         if (first)
81         {
82                 first = false;
83                 oldtime = newtime;
84         }
85
86         if (newtime < oldtime)
87         {
88                 // warn if it's significant
89                 if (newtime - oldtime < -0.01)
90                         Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
91         }
92         else if (newtime > oldtime + 1800)
93         {
94                 Con_Printf("Sys_DoubleTime: time stepped forward (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
95         }
96         else
97                 curtime += newtime - oldtime;
98         oldtime = newtime;
99
100         return curtime;
101 }
102
103 char *Sys_ConsoleInput(void)
104 {
105         if (cls.state == ca_dedicated)
106         {
107                 static char text[MAX_INPUTLINE];
108                 int len = 0;
109 #ifdef WIN32
110                 int c;
111
112                 // read a line out
113                 while (_kbhit ())
114                 {
115                         c = _getch ();
116                         _putch (c);
117                         if (c == '\r')
118                         {
119                                 text[len] = 0;
120                                 _putch ('\n');
121                                 len = 0;
122                                 return text;
123                         }
124                         if (c == 8)
125                         {
126                                 if (len)
127                                 {
128                                         _putch (' ');
129                                         _putch (c);
130                                         len--;
131                                         text[len] = 0;
132                                 }
133                                 continue;
134                         }
135                         text[len] = c;
136                         len++;
137                         text[len] = 0;
138                         if (len == sizeof (text))
139                                 len = 0;
140                 }
141 #else
142                 fd_set fdset;
143                 struct timeval timeout;
144                 FD_ZERO(&fdset);
145                 FD_SET(0, &fdset); // stdin
146                 timeout.tv_sec = 0;
147                 timeout.tv_usec = 0;
148                 if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
149                 {
150                         len = read (0, text, sizeof(text));
151                         if (len >= 1)
152                         {
153                                 // rip off the \n and terminate
154                                 text[len-1] = 0;
155                                 return text;
156                         }
157                 }
158 #endif
159         }
160         return NULL;
161 }
162
163 void Sys_Sleep(int microseconds)
164 {
165         SDL_Delay(microseconds / 1000);
166 }
167
168 char *Sys_GetClipboardData (void)
169 {
170 #ifdef WIN32
171         char *data = NULL;
172         char *cliptext;
173
174         if (OpenClipboard (NULL) != 0)
175         {
176                 HANDLE hClipboardData;
177
178                 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
179                 {
180                         if ((cliptext = (char *)GlobalLock (hClipboardData)) != 0)
181                         {
182                                 size_t allocsize;
183                                 allocsize = GlobalSize (hClipboardData) + 1;
184                                 data = (char *)Z_Malloc (allocsize);
185                                 strlcpy (data, cliptext, allocsize);
186                                 GlobalUnlock (hClipboardData);
187                         }
188                 }
189                 CloseClipboard ();
190         }
191         return data;
192 #else
193         return NULL;
194 #endif
195 }
196
197 void Sys_InitConsole (void)
198 {
199 }
200
201 void Sys_Init_Commands (void)
202 {
203 }
204
205 int main (int argc, char *argv[])
206 {
207         signal(SIGFPE, SIG_IGN);
208
209         com_argc = argc;
210         com_argv = (const char **)argv;
211
212 #ifndef WIN32
213         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
214 #endif
215
216         // we don't know which systems we'll want to init, yet...
217         SDL_Init(0);
218
219         Host_Main();
220
221         return 0;
222 }