sys_rhap.m (5944B)
1 #include <libc.h> 2 #import <AppKit/AppKit.h> 3 #include "../qcommon/qcommon.h" 4 5 int curtime; 6 int sys_frame_time; 7 8 void Sys_UnloadGame (void) 9 { 10 } 11 12 void *GetGameAPI (void *import); 13 14 void *Sys_GetGameAPI (void *parms) 15 { 16 // we are hard-linked in, so no need to load anything 17 return GetGameAPI (parms); 18 } 19 20 void Sys_CopyProtect (void) 21 { 22 } 23 24 char *Sys_GetClipboardData( void ) 25 { 26 return NULL; 27 } 28 29 30 //=========================================================================== 31 32 int hunkcount; 33 34 byte *membase; 35 int hunkmaxsize; 36 int cursize; 37 38 //#define VIRTUAL_ALLOC 39 40 void *Hunk_Begin (int maxsize) 41 { 42 // reserve a huge chunk of memory, but don't commit any yet 43 cursize = 0; 44 hunkmaxsize = maxsize; 45 #ifdef VIRTUAL_ALLOC 46 membase = VirtualAlloc (NULL, maxsize, MEM_RESERVE, PAGE_NOACCESS); 47 #else 48 membase = malloc (maxsize); 49 memset (membase, 0, maxsize); 50 #endif 51 if (!membase) 52 Sys_Error ("VirtualAlloc reserve failed"); 53 return (void *)membase; 54 } 55 56 void *Hunk_Alloc (int size) 57 { 58 void *buf; 59 60 // round to cacheline 61 size = (size+31)&~31; 62 63 #ifdef VIRTUAL_ALLOC 64 // commit pages as needed 65 // buf = VirtualAlloc (membase+cursize, size, MEM_COMMIT, PAGE_READWRITE); 66 buf = VirtualAlloc (membase, cursize+size, MEM_COMMIT, PAGE_READWRITE); 67 if (!buf) 68 { 69 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buf, 0, NULL); 70 Sys_Error ("VirtualAlloc commit failed.\n%s", buf); 71 } 72 #endif 73 cursize += size; 74 if (cursize > hunkmaxsize) 75 Sys_Error ("Hunk_Alloc overflow"); 76 77 return (void *)(membase+cursize-size); 78 } 79 80 int Hunk_End (void) 81 { 82 83 // free the remaining unused virtual memory 84 #if 0 85 void *buf; 86 87 // write protect it 88 buf = VirtualAlloc (membase, cursize, MEM_COMMIT, PAGE_READONLY); 89 if (!buf) 90 Sys_Error ("VirtualAlloc commit failed"); 91 #endif 92 93 hunkcount++; 94 //Com_Printf ("hunkcount: %i\n", hunkcount); 95 return cursize; 96 } 97 98 void Hunk_Free (void *base) 99 { 100 if ( base ) 101 #ifdef VIRTUAL_ALLOC 102 VirtualFree (base, 0, MEM_RELEASE); 103 #else 104 free (base); 105 #endif 106 107 hunkcount--; 108 } 109 110 111 //=========================================================================== 112 113 114 void Sys_Mkdir (char *path) 115 { 116 if (mkdir (path, 0777) != -1) 117 return; 118 if (errno != EEXIST) 119 Com_Error (ERR_FATAL, "mkdir %s: %s",path, strerror(errno)); 120 } 121 122 char *Sys_FindFirst (char *path, unsigned musthave, unsigned canthave) 123 { 124 return NULL; 125 } 126 127 char *Sys_FindNext (unsigned musthave, unsigned canthave) 128 { 129 return NULL; 130 } 131 132 void Sys_FindClose (void) 133 { 134 } 135 136 /* 137 ================ 138 Sys_Milliseconds 139 ================ 140 */ 141 int Sys_Milliseconds (void) 142 { 143 struct timeval tp; 144 struct timezone tzp; 145 static int secbase; 146 147 gettimeofday(&tp, &tzp); 148 149 if (!secbase) 150 { 151 secbase = tp.tv_sec; 152 return tp.tv_usec/1000; 153 } 154 155 curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000; 156 157 return curtime; 158 } 159 160 /* 161 ================ 162 Sys_Error 163 ================ 164 */ 165 void Sys_Error (char *error, ...) 166 { 167 va_list argptr; 168 char string[1024]; 169 170 // change stdin to non blocking 171 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY); 172 173 va_start (argptr,error); 174 vsprintf (string,error,argptr); 175 va_end (argptr); 176 printf ("Fatal error: %s\n",string); 177 178 if (!NSApp) 179 { // appkit isn't running, so don't try to pop up a panel 180 exit (1); 181 } 182 NSRunAlertPanel (@"Fatal error",[NSString stringWithCString: string] 183 ,@"exit",NULL,NULL); 184 [NSApp terminate: NULL]; 185 exit(1); 186 } 187 188 /* 189 ================ 190 Sys_Printf 191 ================ 192 */ 193 void Sys_ConsoleOutput (char *text) 194 { 195 char *t_p; 196 int l, r; 197 198 l = strlen(text); 199 t_p = text; 200 201 // make sure everything goes through, even though we are non-blocking 202 while (l) 203 { 204 r = write (1, text, l); 205 if (r != l) 206 sleep (0); 207 if (r > 0) 208 { 209 t_p += r; 210 l -= r; 211 } 212 } 213 } 214 215 /* 216 ================ 217 Sys_Quit 218 ================ 219 */ 220 void Sys_Quit (void) 221 { 222 // change stdin to blocking 223 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY); 224 225 if (!NSApp) 226 exit (0); // appkit isn't running 227 228 [NSApp terminate:nil]; 229 } 230 231 232 /* 233 ================ 234 Sys_Init 235 ================ 236 */ 237 void Sys_Init(void) 238 { 239 moncontrol(0); // turn off profiling except during real Quake work 240 241 // change stdin to non blocking 242 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); 243 } 244 245 246 extern NSWindow *vid_window_i; 247 248 void Sys_AppActivate (void) 249 { 250 [vid_window_i makeKeyAndOrderFront: nil]; 251 } 252 253 254 /* 255 ================ 256 Sys_SendKeyEvents 257 258 service any pending appkit events 259 ================ 260 */ 261 void Sys_SendKeyEvents (void) 262 { 263 NSEvent *event; 264 NSDate *date; 265 266 date = [NSDate date]; 267 do 268 { 269 event = [NSApp 270 nextEventMatchingMask: 0xffffffff 271 untilDate: date 272 inMode: @"NSDefaultRunLoopMode" 273 dequeue: YES]; 274 if (event) 275 [NSApp sendEvent: event]; 276 } while (event); 277 278 // grab frame time 279 sys_frame_time = Sys_Milliseconds(); 280 } 281 282 283 /* 284 ================ 285 Sys_ConsoleInput 286 287 Checks for a complete line of text typed in at the console, then forwards 288 it to the host command processor 289 ================ 290 */ 291 char *Sys_ConsoleInput (void) 292 { 293 static char text[256]; 294 int len; 295 296 len = read (0, text, sizeof(text)); 297 if (len < 1) 298 return NULL; 299 text[len-1] = 0; // rip off the /n and terminate 300 301 return text; 302 } 303 304 305 /* 306 ============= 307 main 308 ============= 309 */ 310 void main (int argc, char **argv) 311 { 312 int frame; 313 NSAutoreleasePool *pool; 314 int oldtime, t; 315 316 pool = [[NSAutoreleasePool alloc] init]; 317 318 Qcommon_Init (argc, argv); 319 320 [pool release]; 321 322 oldtime = Sys_Milliseconds (); 323 while (1) 324 { 325 pool =[[NSAutoreleasePool alloc] init]; 326 327 if (++frame > 10) 328 moncontrol(1);// profile only while we do each Quake frame 329 330 t = Sys_Milliseconds (); 331 Qcommon_Frame (t - oldtime); 332 oldtime = t; 333 moncontrol(0); 334 335 [pool release]; 336 } 337 } 338