123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #include <libc.h>
- #import <AppKit/AppKit.h>
- #include "../qcommon/qcommon.h"
- int curtime;
- int sys_frame_time;
- void Sys_UnloadGame (void)
- {
- }
- void *GetGameAPI (void *import);
- void *Sys_GetGameAPI (void *parms)
- {
-
- return GetGameAPI (parms);
- }
- void Sys_CopyProtect (void)
- {
- }
- char *Sys_GetClipboardData( void )
- {
- return NULL;
- }
- int hunkcount;
- byte *membase;
- int hunkmaxsize;
- int cursize;
- void *Hunk_Begin (int maxsize)
- {
-
- cursize = 0;
- hunkmaxsize = maxsize;
- #ifdef VIRTUAL_ALLOC
- membase = VirtualAlloc (NULL, maxsize, MEM_RESERVE, PAGE_NOACCESS);
- #else
- membase = malloc (maxsize);
- memset (membase, 0, maxsize);
- #endif
- if (!membase)
- Sys_Error ("VirtualAlloc reserve failed");
- return (void *)membase;
- }
- void *Hunk_Alloc (int size)
- {
- void *buf;
-
- size = (size+31)&~31;
- #ifdef VIRTUAL_ALLOC
-
- buf = VirtualAlloc (membase, cursize+size, MEM_COMMIT, PAGE_READWRITE);
- if (!buf)
- {
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buf, 0, NULL);
- Sys_Error ("VirtualAlloc commit failed.\n%s", buf);
- }
- #endif
- cursize += size;
- if (cursize > hunkmaxsize)
- Sys_Error ("Hunk_Alloc overflow");
- return (void *)(membase+cursize-size);
- }
- int Hunk_End (void)
- {
-
- #if 0
- void *buf;
-
- buf = VirtualAlloc (membase, cursize, MEM_COMMIT, PAGE_READONLY);
- if (!buf)
- Sys_Error ("VirtualAlloc commit failed");
- #endif
- hunkcount++;
- return cursize;
- }
- void Hunk_Free (void *base)
- {
- if ( base )
- #ifdef VIRTUAL_ALLOC
- VirtualFree (base, 0, MEM_RELEASE);
- #else
- free (base);
- #endif
- hunkcount--;
- }
- void Sys_Mkdir (char *path)
- {
- if (mkdir (path, 0777) != -1)
- return;
- if (errno != EEXIST)
- Com_Error (ERR_FATAL, "mkdir %s: %s",path, strerror(errno));
- }
- char *Sys_FindFirst (char *path, unsigned musthave, unsigned canthave)
- {
- return NULL;
- }
- char *Sys_FindNext (unsigned musthave, unsigned canthave)
- {
- return NULL;
- }
- void Sys_FindClose (void)
- {
- }
- int Sys_Milliseconds (void)
- {
- struct timeval tp;
- struct timezone tzp;
- static int secbase;
- gettimeofday(&tp, &tzp);
-
- if (!secbase)
- {
- secbase = tp.tv_sec;
- return tp.tv_usec/1000;
- }
-
- curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000;
- return curtime;
- }
- void Sys_Error (char *error, ...)
- {
- va_list argptr;
- char string[1024];
-
- fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
- va_start (argptr,error);
- vsprintf (string,error,argptr);
- va_end (argptr);
- printf ("Fatal error: %s\n",string);
-
- if (!NSApp)
- {
- exit (1);
- }
- NSRunAlertPanel (@"Fatal error",[NSString stringWithCString: string]
- ,@"exit",NULL,NULL);
- [NSApp terminate: NULL];
- exit(1);
- }
- void Sys_ConsoleOutput (char *text)
- {
- char *t_p;
- int l, r;
-
- l = strlen(text);
- t_p = text;
-
- while (l)
- {
- r = write (1, text, l);
- if (r != l)
- sleep (0);
- if (r > 0)
- {
- t_p += r;
- l -= r;
- }
- }
- }
- void Sys_Quit (void)
- {
- fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
- if (!NSApp)
- exit (0);
- [NSApp terminate:nil];
- }
- void Sys_Init(void)
- {
- moncontrol(0);
- fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
- }
- extern NSWindow *vid_window_i;
- void Sys_AppActivate (void)
- {
- [vid_window_i makeKeyAndOrderFront: nil];
- }
- void Sys_SendKeyEvents (void)
- {
- NSEvent *event;
- NSDate *date;
- date = [NSDate date];
- do
- {
- event = [NSApp
- nextEventMatchingMask: 0xffffffff
- untilDate: date
- inMode: @"NSDefaultRunLoopMode"
- dequeue: YES];
- if (event)
- [NSApp sendEvent: event];
- } while (event);
-
- sys_frame_time = Sys_Milliseconds();
- }
- char *Sys_ConsoleInput (void)
- {
- static char text[256];
- int len;
- len = read (0, text, sizeof(text));
- if (len < 1)
- return NULL;
- text[len-1] = 0;
-
- return text;
- }
- void main (int argc, char **argv)
- {
- int frame;
- NSAutoreleasePool *pool;
- int oldtime, t;
-
- pool = [[NSAutoreleasePool alloc] init];
-
- Qcommon_Init (argc, argv);
- [pool release];
- oldtime = Sys_Milliseconds ();
- while (1)
- {
- pool =[[NSAutoreleasePool alloc] init];
- if (++frame > 10)
- moncontrol(1);
- t = Sys_Milliseconds ();
- Qcommon_Frame (t - oldtime);
- oldtime = t;
- moncontrol(0);
- [pool release];
- }
- }
|