Indentation cleanup.
[AROS-Contrib.git] / Games / Quake / sys_aros.c
blob7018a9339725a6de34b035f4cae35903b7a8b108
1 #include <stdlib.h>
2 #include <limits.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <sys/time.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <errno.h>
12 #include <sys/stat.h>
14 #include <proto/dos.h>
15 #include <proto/exec.h>
17 #include <exec/lists.h>
18 #include <exec/memory.h>
20 #include <dos/dos.h>
21 #include <utility/tagitem.h>
23 #include "quakedef.h"
28 qboolean isDedicated;
30 int nostdout = 0;
32 char *basedir = ".";
33 char *cachedir = "/tmp";
35 cvar_t sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
38 // =======================================================================
39 // AROS Specific types
40 // =======================================================================
42 struct FileNode {
43 struct MinNode node;
44 int handle;
45 BPTR file;
49 // =======================================================================
50 // AROS Specific variables
51 // =======================================================================
53 APTR aros_membase = NULL;
55 // =======================================================================
56 // General routines
57 // =======================================================================
61 void Sys_DebugNumber(int y, int val)
66 void Sys_Printf (char *fmt, ...)
68 va_list argptr;
69 char text[1024];
71 va_start (argptr,fmt);
72 vsprintf (text,fmt,argptr);
73 va_end (argptr);
74 fprintf(stderr, "%s", text);
76 Con_Print (text);
79 void Sys_Printf (char *fmt, ...)
82 va_list argptr;
83 char text[1024], *t_p;
84 int l, r;
86 if (nostdout)
87 return;
89 va_start (argptr,fmt);
90 vsprintf (text,fmt,argptr);
91 va_end (argptr);
93 l = strlen(text);
94 t_p = text;
96 // make sure everything goes through, even though we are non-blocking
97 while (l)
99 r = write (1, text, l);
100 if (r != l)
101 sleep (0);
102 if (r > 0)
104 t_p += r;
105 l -= r;
112 void Sys_Printf (char *fmt, ...)
114 va_list argptr;
115 char text[1024];
116 unsigned char *p;
118 va_start (argptr,fmt);
119 vsprintf (text,fmt,argptr);
120 va_end (argptr);
122 if (strlen(text) > sizeof(text))
123 Sys_Error("memory overwrite in Sys_Printf");
125 if (nostdout)
126 return;
128 for (p = (unsigned char *)text; *p; p++) {
129 *p &= 0x7f;
130 if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
131 printf("[%02x]", *p);
132 else
133 putc(*p, stdout);
137 #if 0
138 static char end1[] =
139 "\x1b[?7h\x1b[40m\x1b[2J\x1b[0;1;41m\x1b[1;1H QUAKE: The Doomed Dimension \x1b[33mby \x1b[44mid\x1b[41m Software \x1b[2;1H ---------------------------------------------------------------------------- \x1b[3;1H CALL 1-800-IDGAMES TO ORDER OR FOR TECHNICAL SUPPORT \x1b[4;1H PRICE: $45.00 (PRICES MAY VARY OUTSIDE THE US.) \x1b[5;1H \x1b[6;1H \x1b[37mYes! You only have one fourth of this incredible epic. That is because most \x1b[7;1H of you have paid us nothing or at most, very little. You could steal the \x1b[8;1H game from a friend. But we both know you'll be punished by God if you do. \x1b[9;1H \x1b[33mWHY RISK ETERNAL DAMNATION? CALL 1-800-IDGAMES AND BUY NOW! \x1b[10;1H \x1b[37mRemember, we love you almost as much as He does. \x1b[11;1H \x1b[12;1H \x1b[33mProgramming: \x1b[37mJohn Carmack, Michael Abrash, John Cash \x1b[13;1H \x1b[33mDesign: \x1b[37mJohn Romero, Sandy Petersen, American McGee, Tim Willits \x1b[14;1H \x1b[33mArt: \x1b[37mAdrian Carmack, Kevin Cloud \x1b[15;1H \x1b[33mBiz: \x1b[37mJay Wilbur, Mike Wilson, Donna Jackson \x1b[16;1H \x1b[33mProjects: \x1b[37mShawn Green \x1b[33mSupport: \x1b[37mBarrett Alexander \x1b[17;1H \x1b[33mSound Effects: \x1b[37mTrent Reznor and Nine Inch Nails \x1b[18;1H For other information or details on ordering outside the US, check out the \x1b[19;1H files accompanying QUAKE or our website at http://www.idsoftware.com. \x1b[20;1H \x1b[0;41mQuake is a trademark of Id Software, inc., (c)1996 Id Software, inc. \x1b[21;1H All rights reserved. NIN logo is a registered trademark licensed to \x1b[22;1H Nothing Interactive, Inc. All rights reserved. \x1b[40m\x1b[23;1H\x1b[0m";
140 static char end2[] =
141 "\x1b[?7h\x1b[40m\x1b[2J\x1b[0;1;41m\x1b[1;1H QUAKE \x1b[33mby \x1b[44mid\x1b[41m Software \x1b[2;1H ----------------------------------------------------------------------------- \x1b[3;1H \x1b[37mWhy did you quit from the registered version of QUAKE? Did the \x1b[4;1H scary monsters frighten you? Or did Mr. Sandman tug at your \x1b[5;1H little lids? No matter! What is important is you love our \x1b[6;1H game, and gave us your money. Congratulations, you are probably \x1b[7;1H not a thief. \x1b[8;1H Thank You. \x1b[9;1H \x1b[33;44mid\x1b[41m Software is: \x1b[10;1H PROGRAMMING: \x1b[37mJohn Carmack, Michael Abrash, John Cash \x1b[11;1H \x1b[33mDESIGN: \x1b[37mJohn Romero, Sandy Petersen, American McGee, Tim Willits \x1b[12;1H \x1b[33mART: \x1b[37mAdrian Carmack, Kevin Cloud \x1b[13;1H \x1b[33mBIZ: \x1b[37mJay Wilbur, Mike Wilson \x1b[33mPROJECTS MAN: \x1b[37mShawn Green \x1b[14;1H \x1b[33mBIZ ASSIST: \x1b[37mDonna Jackson \x1b[33mSUPPORT: \x1b[37mBarrett Alexander \x1b[15;1H \x1b[33mSOUND EFFECTS AND MUSIC: \x1b[37mTrent Reznor and Nine Inch Nails \x1b[16;1H \x1b[17;1H If you need help running QUAKE refer to the text files in the \x1b[18;1H QUAKE directory, or our website at http://www.idsoftware.com. \x1b[19;1H If all else fails, call our technical support at 1-800-IDGAMES. \x1b[20;1H \x1b[0;41mQuake is a trademark of Id Software, inc., (c)1996 Id Software, inc. \x1b[21;1H All rights reserved. NIN logo is a registered trademark licensed \x1b[22;1H to Nothing Interactive, Inc. All rights reserved. \x1b[23;1H\x1b[40m\x1b[0m";
143 #endif
144 void Sys_Quit (void)
146 printf("Sys_Quit\n");
148 Host_Shutdown();
149 #ifdef FNDELAY
150 /* Set stdin as non-blocking */
151 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
152 #endif
153 #if 0
154 if (registered.value)
155 printf("%s", end2);
156 else
157 printf("%s", end1);
158 #endif
160 if (NULL != aros_membase) {
161 FreeVec(aros_membase);
163 fflush(stdout);
164 exit(0);
167 void Sys_Init(void)
169 #if id386
170 Sys_SetFPCW();
171 #endif
174 void Sys_Error (char *error, ...)
176 va_list argptr;
177 char string[1024];
179 // change stdin to non blocking
180 #ifdef FNDELAY
181 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
182 #endif
184 va_start (argptr,error);
185 vsprintf (string,error,argptr);
186 va_end (argptr);
187 fprintf(stderr, "Error: %s\n", string);
189 Sys_Quit();
191 #if 0
192 Host_Shutdown ();
193 exit (1);
194 #endif
198 void Sys_Warn (char *warning, ...)
200 va_list argptr;
201 char string[1024];
203 va_start (argptr,warning);
204 vsprintf (string,warning,argptr);
205 va_end (argptr);
206 fprintf(stderr, "Warning: %s", string);
210 ============
211 Sys_FileTime
213 returns -1 if not present
214 ============
216 int Sys_FileTime (char *path)
218 struct stat buf;
220 if (stat (path,&buf) == -1)
221 return -1;
223 return buf.st_mtime;
227 void Sys_mkdir (char *path)
229 mkdir (path, 0777);
234 int Sys_FileOpenRead (char *path, int *handle)
236 int h;
237 struct stat fileinfo;
240 h = open (path, O_RDONLY, 0666);
241 *handle = h;
242 if (h == -1)
243 return -1;
245 if (fstat (h,&fileinfo) == -1)
246 Sys_Error ("Error fstating %s", path);
248 return fileinfo.st_size;
251 int Sys_FileOpenWrite (char *path)
253 int handle;
255 umask (0);
257 handle = open(path,O_RDWR | O_CREAT | O_TRUNC
258 , 0666);
260 if (handle == -1)
261 Sys_Error ("Error opening %s: %s", path,strerror(errno));
263 return handle;
266 int Sys_FileWrite (int handle, void *src, int count)
268 return write (handle, src, count);
271 void Sys_FileClose (int handle)
273 close (handle);
276 void Sys_FileSeek (int handle, int position)
278 lseek (handle, position, SEEK_SET);
281 int Sys_FileRead (int handle, void *dest, int count)
283 return read (handle, dest, count);
286 void Sys_DebugLog(char *file, char *fmt, ...)
288 static char data[1024];
289 int fd;
290 va_list argptr;
292 printf("Sys_DebugLog\n");
294 va_start(argptr, fmt);
295 vsprintf(data, fmt, argptr);
296 va_end(argptr);
298 fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
299 write(fd, data, strlen(data));
300 close(fd);
303 void Sys_EditFile(char *filename)
305 char cmd[256];
306 #ifdef __AROS__
307 sprintf(cmd, "edit %s", filename);
308 Execute(cmd, BNULL, BNULL);
309 #else
310 char *term;
311 char *editor;
313 term = getenv("TERM");
314 if (term && !strcmp(term, "xterm"))
316 editor = getenv("VISUAL");
317 if (!editor)
318 editor = getenv("EDITOR");
319 if (!editor)
320 editor = getenv("EDIT");
321 if (!editor)
322 editor = "vi";
323 sprintf(cmd, "xterm -e %s %s", editor, filename);
324 system(cmd);
326 #endif
329 double Sys_FloatTime (void)
331 struct timeval tp;
332 struct timezone tzp;
333 static int secbase;
335 gettimeofday(&tp, &tzp);
337 if (!secbase)
339 secbase = tp.tv_sec;
340 return tp.tv_usec/1000000.0;
343 return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
346 // =======================================================================
347 // Sleeps for microseconds
348 // =======================================================================
350 static volatile int oktogo;
352 void alarm_handler(int x)
354 oktogo=1;
357 void Sys_LineRefresh(void)
361 #if 0
362 void floating_point_exception_handler(int whatever)
364 // Sys_Warn("floating point exception\n");
365 signal(SIGFPE, floating_point_exception_handler);
367 #endif
368 char *Sys_ConsoleInput(void)
370 static char text[256];
372 // printf("Sys_ConsoleInput\n");
374 if (cls.state == ca_dedicated) {
376 FGets(Input(), text, 256);
378 return text;
381 return NULL;
384 #if 0
385 static char text[256];
386 int len;
387 fd_set fdset;
388 struct timeval timeout;
390 if (cls.state == ca_dedicated) {
391 FD_ZERO(&fdset);
392 FD_SET(0, &fdset); // stdin
393 timeout.tv_sec = 0;
394 timeout.tv_usec = 0;
395 if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
396 return NULL;
398 len = read (0, text, sizeof(text));
399 if (len < 1)
400 return NULL;
401 text[len-1] = 0; // rip off the /n and terminate
403 return text;
405 return NULL;
406 #endif
409 #if !id386
410 void Sys_HighFPPrecision (void)
414 void Sys_LowFPPrecision (void)
417 #endif
419 int main (int c, char **v)
422 double time, oldtime, newtime;
423 quakeparms_t parms;
424 extern int vcrFile;
425 extern int recording;
426 int j;
428 // static char cwd[1024];
430 // signal(SIGFPE, floating_point_exception_handler);
431 #if 0
432 signal(SIGFPE, SIG_IGN);
433 #endif
435 memset(&parms, 0, sizeof(parms));
437 COM_InitArgv(c, v);
438 parms.argc = com_argc;
439 parms.argv = com_argv;
441 #ifdef GLQUAKE
442 parms.memsize = 16*1024*1024;
443 #else
444 parms.memsize = 8*1024*1024;
445 #endif
447 j = COM_CheckParm("-mem");
448 if (j)
449 parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
450 parms.membase = AllocVec (parms.memsize, MEMF_ANY);
452 aros_membase = parms.membase;
453 printf("parms.membase: %p\n", parms.membase);
455 parms.basedir = basedir;
456 // caching is disabled by default, use -cachedir to enable
457 // parms.cachedir = cachedir;
459 #ifdef FNDELAY
460 /* Set stdin as non-blocking */
461 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
462 #endif
464 printf("Initing host, memsize=%d\n", parms.memsize);
465 Host_Init(&parms);
467 printf("Initing syst\n");
468 Sys_Init();
470 printf("Check for nostdout\n");
471 if (COM_CheckParm("-nostdout"))
472 nostdout = 1;
473 else {
474 #ifdef FNDELAY
475 /* Set stdin as non-blocking */
476 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
477 #endif
478 printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
480 printf("Entering main loop\n");
481 oldtime = Sys_FloatTime () - 0.1;
482 while (1)
484 // find time spent rendering last frame
485 newtime = Sys_FloatTime ();
486 time = newtime - oldtime;
488 if (cls.state == ca_dedicated)
489 { // play vcrfiles at max speed
490 if (time < sys_ticrate.value && (vcrFile == -1 || recording) )
492 usleep(1);
493 continue; // not time to run a server only tic yet
495 time = sys_ticrate.value;
498 if (time > sys_ticrate.value*2)
499 oldtime = newtime;
500 else
501 oldtime += time;
503 Host_Frame (time);
505 // graphic debugging aids
506 if (sys_linerefresh.value)
507 Sys_LineRefresh ();
514 ================
515 Sys_MakeCodeWriteable
516 ================
518 void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
520 #ifndef __AROS__
521 int r;
522 unsigned long addr;
523 int psize = getpagesize();
525 addr = (startaddr & ~(psize-1)) - psize;
527 // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
528 // addr, startaddr+length, length);
530 r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
532 if (r < 0)
533 Sys_Error("Protection change failed\n");
535 #endif