fix remapping behavior. Remapping is only necessary if we are rendering on the workbe...
[AROS-Contrib.git] / Games / Quake / sys_aros.c
blob0dd6bbe54838b7b0c178aa5120e6ee6c4d02b0fe
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 *cachedir = "/tmp";
34 cvar_t sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
37 // =======================================================================
38 // AROS Specific types
39 // =======================================================================
41 struct FileNode {
42 struct MinNode node;
43 int handle;
44 BPTR file;
48 // =======================================================================
49 // AROS Specific variables
50 // =======================================================================
52 APTR aros_membase = NULL;
54 // =======================================================================
55 // General routines
56 // =======================================================================
60 void Sys_DebugNumber(int y, int val)
65 void Sys_Printf (char *fmt, ...)
67 va_list argptr;
68 char text[1024];
70 va_start (argptr,fmt);
71 vsprintf (text,fmt,argptr);
72 va_end (argptr);
73 fprintf(stderr, "%s", text);
75 Con_Print (text);
78 void Sys_Printf (char *fmt, ...)
81 va_list argptr;
82 char text[1024], *t_p;
83 int l, r;
85 if (nostdout)
86 return;
88 va_start (argptr,fmt);
89 vsprintf (text,fmt,argptr);
90 va_end (argptr);
92 l = strlen(text);
93 t_p = text;
95 // make sure everything goes through, even though we are non-blocking
96 while (l)
98 r = write (1, text, l);
99 if (r != l)
100 sleep (0);
101 if (r > 0)
103 t_p += r;
104 l -= r;
111 void Sys_Printf (char *fmt, ...)
113 va_list argptr;
114 char text[1024];
115 unsigned char *p;
117 va_start (argptr,fmt);
118 vsprintf (text,fmt,argptr);
119 va_end (argptr);
121 if (strlen(text) > sizeof(text))
122 Sys_Error("memory overwrite in Sys_Printf");
124 if (nostdout)
125 return;
127 for (p = (unsigned char *)text; *p; p++) {
128 *p &= 0x7f;
129 if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
130 printf("[%02x]", *p);
131 else
132 putc(*p, stdout);
136 #if 0
137 static char end1[] =
138 "\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";
139 static char end2[] =
140 "\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";
142 #endif
143 void Sys_Quit (void)
145 printf("Sys_Quit\n");
147 Host_Shutdown();
148 #ifdef FNDELAY
149 /* Set stdin as non-blocking */
150 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
151 #endif
152 #if 0
153 if (registered.value)
154 printf("%s", end2);
155 else
156 printf("%s", end1);
157 #endif
159 if (NULL != aros_membase) {
160 FreeVec(aros_membase);
162 fflush(stdout);
163 exit(0);
166 void Sys_Init(void)
168 #if id386
169 Sys_SetFPCW();
170 #endif
173 void Sys_Error (char *error, ...)
175 va_list argptr;
176 char string[1024];
178 // change stdin to non blocking
179 #ifdef FNDELAY
180 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
181 #endif
183 va_start (argptr,error);
184 vsprintf (string,error,argptr);
185 va_end (argptr);
186 fprintf(stderr, "Error: %s\n", string);
188 Sys_Quit();
190 #if 0
191 Host_Shutdown ();
192 exit (1);
193 #endif
197 void Sys_Warn (char *warning, ...)
199 va_list argptr;
200 char string[1024];
202 va_start (argptr,warning);
203 vsprintf (string,warning,argptr);
204 va_end (argptr);
205 fprintf(stderr, "Warning: %s", string);
209 ============
210 Sys_FileTime
212 returns -1 if not present
213 ============
215 int Sys_FileTime (char *path)
217 struct stat buf;
219 if (stat (path,&buf) == -1)
220 return -1;
222 return buf.st_mtime;
226 void Sys_mkdir (char *path)
228 mkdir (path, 0777);
233 int Sys_FileOpenRead (char *path, int *handle)
235 int h;
236 struct stat fileinfo;
239 h = open (path, O_RDONLY, 0666);
240 *handle = h;
241 if (h == -1)
242 return -1;
244 if (fstat (h,&fileinfo) == -1)
245 Sys_Error ("Error fstating %s", path);
247 return fileinfo.st_size;
250 int Sys_FileOpenWrite (char *path)
252 int handle;
254 umask (0);
256 handle = open(path,O_RDWR | O_CREAT | O_TRUNC
257 , 0666);
259 if (handle == -1)
260 Sys_Error ("Error opening %s: %s", path,strerror(errno));
262 return handle;
265 int Sys_FileWrite (int handle, void *src, int count)
267 return write (handle, src, count);
270 void Sys_FileClose (int handle)
272 close (handle);
275 void Sys_FileSeek (int handle, int position)
277 lseek (handle, position, SEEK_SET);
280 int Sys_FileRead (int handle, void *dest, int count)
282 return read (handle, dest, count);
285 void Sys_DebugLog(char *file, char *fmt, ...)
287 static char data[1024];
288 int fd;
289 va_list argptr;
291 printf("Sys_DebugLog\n");
293 va_start(argptr, fmt);
294 vsprintf(data, fmt, argptr);
295 va_end(argptr);
297 fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
298 write(fd, data, strlen(data));
299 close(fd);
302 void Sys_EditFile(char *filename)
304 char cmd[256];
305 #ifdef __AROS__
306 sprintf(cmd, "edit %s", filename);
307 Execute(cmd, BNULL, BNULL);
308 #else
309 char *term;
310 char *editor;
312 term = getenv("TERM");
313 if (term && !strcmp(term, "xterm"))
315 editor = getenv("VISUAL");
316 if (!editor)
317 editor = getenv("EDITOR");
318 if (!editor)
319 editor = getenv("EDIT");
320 if (!editor)
321 editor = "vi";
322 sprintf(cmd, "xterm -e %s %s", editor, filename);
323 system(cmd);
325 #endif
328 double Sys_FloatTime (void)
330 struct timeval tp;
331 struct timezone tzp;
332 static int secbase;
334 gettimeofday(&tp, &tzp);
336 if (!secbase)
338 secbase = tp.tv_sec;
339 return tp.tv_usec/1000000.0;
342 return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
345 // =======================================================================
346 // Sleeps for microseconds
347 // =======================================================================
349 static volatile int oktogo;
351 void alarm_handler(int x)
353 oktogo=1;
356 void Sys_LineRefresh(void)
360 #if 0
361 void floating_point_exception_handler(int whatever)
363 // Sys_Warn("floating point exception\n");
364 signal(SIGFPE, floating_point_exception_handler);
366 #endif
367 char *Sys_ConsoleInput(void)
369 static char text[256];
371 // printf("Sys_ConsoleInput\n");
373 if (cls.state == ca_dedicated) {
375 FGets(Input(), text, 256);
377 return text;
380 return NULL;
383 #if 0
384 static char text[256];
385 int len;
386 fd_set fdset;
387 struct timeval timeout;
389 if (cls.state == ca_dedicated) {
390 FD_ZERO(&fdset);
391 FD_SET(0, &fdset); // stdin
392 timeout.tv_sec = 0;
393 timeout.tv_usec = 0;
394 if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
395 return NULL;
397 len = read (0, text, sizeof(text));
398 if (len < 1)
399 return NULL;
400 text[len-1] = 0; // rip off the /n and terminate
402 return text;
404 return NULL;
405 #endif
408 #if !id386
409 void Sys_HighFPPrecision (void)
413 void Sys_LowFPPrecision (void)
416 #endif
418 int main (int c, char **v)
421 double time, oldtime, newtime;
422 quakeparms_t parms;
423 extern int vcrFile;
424 extern int recording;
425 int j;
427 static char cwd[1024];
429 // signal(SIGFPE, floating_point_exception_handler);
430 #if 0
431 signal(SIGFPE, SIG_IGN);
432 #endif
434 memset(&parms, 0, sizeof(parms));
436 COM_InitArgv(c, v);
437 parms.argc = com_argc;
438 parms.argv = com_argv;
440 #ifdef GLQUAKE
441 parms.memsize = 16*1024*1024;
442 #else
443 parms.memsize = 8*1024*1024;
444 #endif
446 j = COM_CheckParm("-mem");
447 if (j)
448 parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
449 parms.membase = AllocVec (parms.memsize, MEMF_ANY);
451 aros_membase = parms.membase;
452 printf("parms.membase: %p\n", parms.membase);
454 /* get name of current directory */
455 GetCurrentDirName(cwd,128);
456 parms.basedir = cwd;
457 // caching is disabled by default, use -cachedir to enable
458 // parms.cachedir = cachedir;
460 #ifdef FNDELAY
461 /* Set stdin as non-blocking */
462 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
463 #endif
465 printf("Initing host, memsize=%d\n", parms.memsize);
466 Host_Init(&parms);
468 printf("Initing syst\n");
469 Sys_Init();
471 printf("Check for nostdout\n");
472 if (COM_CheckParm("-nostdout"))
473 nostdout = 1;
474 else {
475 #ifdef FNDELAY
476 /* Set stdin as non-blocking */
477 fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
478 #endif
479 printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
481 printf("Entering main loop\n");
482 oldtime = Sys_FloatTime () - 0.1;
483 while (1)
485 // find time spent rendering last frame
486 newtime = Sys_FloatTime ();
487 time = newtime - oldtime;
489 if (cls.state == ca_dedicated)
490 { // play vcrfiles at max speed
491 if (time < sys_ticrate.value && (vcrFile == -1 || recording) )
493 usleep(1);
494 continue; // not time to run a server only tic yet
496 time = sys_ticrate.value;
499 if (time > sys_ticrate.value*2)
500 oldtime = newtime;
501 else
502 oldtime += time;
504 Host_Frame (time);
506 // graphic debugging aids
507 if (sys_linerefresh.value)
508 Sys_LineRefresh ();
515 ================
516 Sys_MakeCodeWriteable
517 ================
519 void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
521 #ifndef __AROS__
522 int r;
523 unsigned long addr;
524 int psize = getpagesize();
526 addr = (startaddr & ~(psize-1)) - psize;
528 // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
529 // addr, startaddr+length, length);
531 r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
533 if (r < 0)
534 Sys_Error("Protection change failed\n");
536 #endif