diff options
Diffstat (limited to 'apps/plugins/sdl/progs/quake/sys_sdl.c')
-rw-r--r-- | apps/plugins/sdl/progs/quake/sys_sdl.c | 436 |
1 files changed, 436 insertions, 0 deletions
diff --git a/apps/plugins/sdl/progs/quake/sys_sdl.c b/apps/plugins/sdl/progs/quake/sys_sdl.c new file mode 100644 index 0000000000..3cc3122bc2 --- /dev/null +++ b/apps/plugins/sdl/progs/quake/sys_sdl.c | |||
@@ -0,0 +1,436 @@ | |||
1 | #include <limits.h> | ||
2 | #include <sys/types.h> | ||
3 | #include <stdarg.h> | ||
4 | #include <ctype.h> | ||
5 | |||
6 | #include "quakedef.h" | ||
7 | |||
8 | qboolean isDedicated; | ||
9 | |||
10 | int noconinput = 0; | ||
11 | |||
12 | char *basedir = "/.rockbox/quake"; | ||
13 | char *cachedir = NULL; | ||
14 | |||
15 | cvar_t sys_linerefresh = {"sys_linerefresh","0"};// set for entity display | ||
16 | cvar_t sys_nostdout = {"sys_nostdout","0"}; | ||
17 | |||
18 | // ======================================================================= | ||
19 | // General routines | ||
20 | // ======================================================================= | ||
21 | |||
22 | void Sys_DebugNumber(int y, int val) | ||
23 | { | ||
24 | } | ||
25 | |||
26 | int enable_printf = 1; | ||
27 | |||
28 | void Sys_Printf (char *fmt, ...) | ||
29 | { | ||
30 | va_list argptr; | ||
31 | char text[1024]; | ||
32 | |||
33 | va_start (argptr,fmt); | ||
34 | vsprintf (text,fmt,argptr); | ||
35 | va_end (argptr); | ||
36 | if(enable_printf) | ||
37 | { | ||
38 | printf("%s", text); | ||
39 | } | ||
40 | LOGF("%s", text); | ||
41 | |||
42 | //Con_Print (text); | ||
43 | } | ||
44 | |||
45 | void Sys_Quit (void) | ||
46 | { | ||
47 | Host_Shutdown(); | ||
48 | exit(0); | ||
49 | } | ||
50 | |||
51 | void Sys_Init(void) | ||
52 | { | ||
53 | #if id386 | ||
54 | Sys_SetFPCW(); | ||
55 | #endif | ||
56 | } | ||
57 | |||
58 | #if !id386 | ||
59 | |||
60 | /* | ||
61 | ================ | ||
62 | Sys_LowFPPrecision | ||
63 | ================ | ||
64 | */ | ||
65 | void Sys_LowFPPrecision (void) | ||
66 | { | ||
67 | // causes weird problems on Nextstep | ||
68 | } | ||
69 | |||
70 | |||
71 | /* | ||
72 | ================ | ||
73 | Sys_HighFPPrecision | ||
74 | ================ | ||
75 | */ | ||
76 | void Sys_HighFPPrecision (void) | ||
77 | { | ||
78 | // causes weird problems on Nextstep | ||
79 | } | ||
80 | |||
81 | #endif // !id386 | ||
82 | |||
83 | |||
84 | void Sys_Error (char *error, ...) | ||
85 | { | ||
86 | va_list argptr; | ||
87 | char string[1024]; | ||
88 | |||
89 | va_start (argptr,error); | ||
90 | vsprintf (string,error,argptr); | ||
91 | va_end (argptr); | ||
92 | rb->splashf(HZ*5, "Error: %s\n", string); | ||
93 | |||
94 | Host_Shutdown (); | ||
95 | exit (1); | ||
96 | |||
97 | } | ||
98 | |||
99 | void Sys_Warn (char *warning, ...) | ||
100 | { | ||
101 | va_list argptr; | ||
102 | char string[1024]; | ||
103 | |||
104 | va_start (argptr,warning); | ||
105 | vsprintf (string,warning,argptr); | ||
106 | va_end (argptr); | ||
107 | rb->splashf(HZ*2, "Warning: %s", string); | ||
108 | } | ||
109 | |||
110 | /* | ||
111 | =============================================================================== | ||
112 | |||
113 | FILE IO | ||
114 | |||
115 | =============================================================================== | ||
116 | */ | ||
117 | |||
118 | #define MAX_HANDLES 10 | ||
119 | static FILE *sys_handles[MAX_HANDLES]; | ||
120 | |||
121 | void Sys_Shutdown(void) | ||
122 | { | ||
123 | for(int i = 0; i < MAX_HANDLES; i++) | ||
124 | { | ||
125 | FILE *f = sys_handles[i]; | ||
126 | if(f) | ||
127 | fclose(f); | ||
128 | sys_handles[i] = NULL; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | int findhandle (void) | ||
133 | { | ||
134 | int i; | ||
135 | |||
136 | for (i=1 ; i<MAX_HANDLES ; i++) | ||
137 | if (!sys_handles[i]) | ||
138 | return i; | ||
139 | Sys_Error ("out of handles"); | ||
140 | return -1; | ||
141 | } | ||
142 | |||
143 | /* | ||
144 | ================ | ||
145 | Qfilelength | ||
146 | ================ | ||
147 | */ | ||
148 | static int Qfilelength (FILE *f) | ||
149 | { | ||
150 | int pos; | ||
151 | int end; | ||
152 | |||
153 | pos = ftell (f); | ||
154 | fseek (f, 0, SEEK_END); | ||
155 | end = ftell (f); | ||
156 | fseek (f, pos, SEEK_SET); | ||
157 | |||
158 | return end; | ||
159 | } | ||
160 | |||
161 | int Sys_FileOpenRead (char *path, int *hndl) | ||
162 | { | ||
163 | FILE *f; | ||
164 | int i; | ||
165 | |||
166 | i = findhandle (); | ||
167 | |||
168 | f = fopen(path, "rb"); | ||
169 | if (!f) | ||
170 | { | ||
171 | *hndl = -1; | ||
172 | return -1; | ||
173 | } | ||
174 | sys_handles[i] = f; | ||
175 | *hndl = i; | ||
176 | |||
177 | //rb->splashf(HZ*2, "Allocating handle %d to %s", i, path); | ||
178 | |||
179 | |||
180 | return Qfilelength(f); | ||
181 | } | ||
182 | |||
183 | int Sys_FileOpenWrite (char *path) | ||
184 | { | ||
185 | FILE *f; | ||
186 | int i; | ||
187 | |||
188 | i = findhandle (); | ||
189 | |||
190 | f = fopen(path, "wb"); | ||
191 | if (!f) | ||
192 | Sys_Error ("Error opening %s: %s", path,strerror(errno)); | ||
193 | sys_handles[i] = f; | ||
194 | |||
195 | return i; | ||
196 | } | ||
197 | |||
198 | void Sys_FileClose (int handle) | ||
199 | { | ||
200 | if ( handle >= 0 ) { | ||
201 | //rb->splashf(HZ, "Close handle %d", handle); | ||
202 | fclose (sys_handles[handle]); | ||
203 | sys_handles[handle] = NULL; | ||
204 | } | ||
205 | } | ||
206 | |||
207 | void Sys_FileSeek (int handle, int position) | ||
208 | { | ||
209 | if ( handle >= 0 ) { | ||
210 | fseek (sys_handles[handle], position, SEEK_SET); | ||
211 | } | ||
212 | } | ||
213 | |||
214 | int Sys_FileRead (int handle, void *dst, int count) | ||
215 | { | ||
216 | char *data; | ||
217 | int size, done; | ||
218 | |||
219 | size = 0; | ||
220 | if ( handle >= 0 ) { | ||
221 | data = dst; | ||
222 | while ( count > 0 ) { | ||
223 | done = fread (data, 1, count, sys_handles[handle]); | ||
224 | if ( done == 0 ) { | ||
225 | break; | ||
226 | } | ||
227 | else if(done < 0) | ||
228 | { | ||
229 | Sys_Error("stream error %d, file is %d = %d", done, handle, sys_handles[handle]); | ||
230 | } | ||
231 | data += done; | ||
232 | count -= done; | ||
233 | size += done; | ||
234 | } | ||
235 | } | ||
236 | return size; | ||
237 | |||
238 | } | ||
239 | |||
240 | int Sys_FileWrite (int handle, void *src, int count) | ||
241 | { | ||
242 | char *data; | ||
243 | int size, done; | ||
244 | |||
245 | size = 0; | ||
246 | if ( handle >= 0 ) { | ||
247 | data = src; | ||
248 | while ( count > 0 ) { | ||
249 | done = fread (data, 1, count, sys_handles[handle]); | ||
250 | if ( done == 0 ) { | ||
251 | break; | ||
252 | } | ||
253 | data += done; | ||
254 | count -= done; | ||
255 | size += done; | ||
256 | } | ||
257 | } | ||
258 | return size; | ||
259 | } | ||
260 | |||
261 | int Sys_FileTime (char *path) | ||
262 | { | ||
263 | FILE *f; | ||
264 | |||
265 | f = fopen(path, "rb"); | ||
266 | if (f) | ||
267 | { | ||
268 | fclose(f); | ||
269 | return 1; | ||
270 | } | ||
271 | |||
272 | return -1; | ||
273 | } | ||
274 | |||
275 | void Sys_mkdir (char *path) | ||
276 | { | ||
277 | #ifdef __WIN32__ | ||
278 | mkdir (path); | ||
279 | #else | ||
280 | mkdir (path); | ||
281 | #endif | ||
282 | } | ||
283 | |||
284 | void Sys_DebugLog(char *file, char *fmt, ...) | ||
285 | { | ||
286 | va_list argptr; | ||
287 | static char data[1024]; | ||
288 | FILE *fp; | ||
289 | |||
290 | va_start(argptr, fmt); | ||
291 | vsprintf(data, fmt, argptr); | ||
292 | va_end(argptr); | ||
293 | fp = fopen(file, "a"); | ||
294 | fwrite(data, strlen(data), 1, fp); | ||
295 | fclose(fp); | ||
296 | } | ||
297 | |||
298 | double Sys_FloatTime (void) | ||
299 | { | ||
300 | static int starttime = 0; | ||
301 | |||
302 | if ( ! starttime ) | ||
303 | starttime = *rb->current_tick; | ||
304 | |||
305 | return (*rb->current_tick - starttime) / ((double)HZ); | ||
306 | } | ||
307 | |||
308 | // ======================================================================= | ||
309 | // Sleeps for microseconds | ||
310 | // ======================================================================= | ||
311 | |||
312 | static volatile int oktogo; | ||
313 | |||
314 | void alarm_handler(int x) | ||
315 | { | ||
316 | oktogo=1; | ||
317 | } | ||
318 | |||
319 | byte *Sys_ZoneBase (int *size) | ||
320 | { | ||
321 | |||
322 | char *QUAKEOPT = getenv("QUAKEOPT"); | ||
323 | |||
324 | *size = 0xc00000; | ||
325 | if (QUAKEOPT) | ||
326 | { | ||
327 | while (*QUAKEOPT) | ||
328 | if (tolower(*QUAKEOPT++) == 'm') | ||
329 | { | ||
330 | *size = atof(QUAKEOPT) * 1024*1024; | ||
331 | break; | ||
332 | } | ||
333 | } | ||
334 | return malloc (*size); | ||
335 | |||
336 | } | ||
337 | |||
338 | void Sys_LineRefresh(void) | ||
339 | { | ||
340 | } | ||
341 | |||
342 | void Sys_Sleep(void) | ||
343 | { | ||
344 | SDL_Delay(1); | ||
345 | } | ||
346 | |||
347 | void floating_point_exception_handler(int whatever) | ||
348 | { | ||
349 | // Sys_Warn("floating point exception\n"); | ||
350 | } | ||
351 | |||
352 | void moncontrol(int x) | ||
353 | { | ||
354 | } | ||
355 | |||
356 | int my_main (int c, char **v) | ||
357 | { | ||
358 | double time, oldtime, newtime; | ||
359 | quakeparms_t parms; | ||
360 | extern int vcrFile; | ||
361 | extern int recording; | ||
362 | static int frame; | ||
363 | |||
364 | moncontrol(0); | ||
365 | |||
366 | // signal(SIGFPE, floating_point_exception_handler); | ||
367 | |||
368 | //rb->splash(0, "quake 1"); | ||
369 | |||
370 | parms.memsize = 8*1024*1024; | ||
371 | parms.membase = malloc (parms.memsize); | ||
372 | parms.basedir = basedir; | ||
373 | parms.cachedir = cachedir; | ||
374 | |||
375 | COM_InitArgv(c, v); | ||
376 | parms.argc = com_argc; | ||
377 | parms.argv = com_argv; | ||
378 | |||
379 | Sys_Init(); | ||
380 | //rb->splash(0, "quake 2"); | ||
381 | |||
382 | Host_Init(&parms); | ||
383 | //rb->splash(0, "quake 3"); | ||
384 | |||
385 | //Cvar_RegisterVariable (&sys_nostdout); | ||
386 | //rb->splash(0, "quake 4"); | ||
387 | |||
388 | oldtime = Sys_FloatTime () - 0.1; | ||
389 | while (1) | ||
390 | { | ||
391 | // find time spent rendering last frame | ||
392 | newtime = Sys_FloatTime (); | ||
393 | time = newtime - oldtime; | ||
394 | |||
395 | if (cls.state == ca_dedicated) | ||
396 | { // play vcrfiles at max speed | ||
397 | if (time < sys_ticrate.value && (vcrFile == -1 || recording) ) | ||
398 | { | ||
399 | rb->yield(); | ||
400 | continue; // not time to run a server only tic yet | ||
401 | } | ||
402 | time = sys_ticrate.value; | ||
403 | } | ||
404 | |||
405 | if (time > sys_ticrate.value*2) | ||
406 | oldtime = newtime; | ||
407 | else | ||
408 | oldtime += time; | ||
409 | |||
410 | if (++frame > 10) | ||
411 | moncontrol(1); // profile only while we do each Quake frame | ||
412 | Host_Frame (time); | ||
413 | moncontrol(0); | ||
414 | |||
415 | // graphic debugging aids | ||
416 | if (sys_linerefresh.value) | ||
417 | Sys_LineRefresh (); | ||
418 | |||
419 | rb->yield(); | ||
420 | } | ||
421 | |||
422 | } | ||
423 | |||
424 | |||
425 | /* | ||
426 | ================ | ||
427 | Sys_MakeCodeWriteable | ||
428 | ================ | ||
429 | */ | ||
430 | void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length) | ||
431 | { | ||
432 | |||
433 | Sys_Error("Protection change failed\n"); | ||
434 | |||
435 | } | ||
436 | |||