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