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