2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sys_win.c -- Win32 system interface code
22 #define WIN32_USETIMEGETTIME 0
31 #define CONSOLE_ERROR_TIMEOUT 60.0 // # of seconds to wait on Sys_Error running
32 // dedicated before exiting
33 #define PAUSE_SLEEP 50 // sleep time on pause or minimization
34 #define NOT_FOCUS_SLEEP 20 // sleep time when not focus
37 qboolean ActiveApp, Minimized;
39 static qboolean sc_return_on_enter = false;
40 HANDLE hinput, houtput;
44 static HANDLE heventParent;
45 static HANDLE heventChild;
48 ===============================================================================
52 ===============================================================================
55 // LordHavoc: 256 pak files (was 10)
56 #define MAX_HANDLES 256
57 QFile *sys_handles[MAX_HANDLES];
63 for (i=1 ; i<MAX_HANDLES ; i++)
66 Sys_Error ("out of handles");
75 int Sys_FileLength (QFile *f)
81 Qseek (f, 0, SEEK_END);
83 Qseek (f, pos, SEEK_SET);
88 int Sys_FileOpenRead (char *path, int *hndl)
95 f = Qopen(path, "rbz");
106 retval = Sys_FileLength(f);
112 int Sys_FileOpenWrite (char *path)
119 f = Qopen(path, "wb");
122 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
130 void Sys_FileClose (int handle)
132 Qclose (sys_handles[handle]);
133 sys_handles[handle] = NULL;
136 void Sys_FileSeek (int handle, int position)
138 Qseek (sys_handles[handle], position, SEEK_SET);
141 int Sys_FileRead (int handle, void *dest, int count)
143 return Qread (sys_handles[handle], dest, count);
146 int Sys_FileWrite (int handle, void *data, int count)
148 return Qwrite (sys_handles[handle], data, count);
151 int Sys_FileTime (char *path)
155 f = Qopen(path, "rb");
165 void Sys_mkdir (char *path)
172 ===============================================================================
176 ===============================================================================
179 void Sys_Error (char *error, ...)
182 char text[1024], text2[1024];
183 char *text3 = "Press Enter to exit\n";
184 char *text4 = "***********************************\n";
188 static int in_sys_error0 = 0;
189 static int in_sys_error1 = 0;
190 static int in_sys_error2 = 0;
191 static int in_sys_error3 = 0;
196 va_start (argptr, error);
197 vsprintf (text, error, argptr);
200 if (cls.state == ca_dedicated)
202 va_start (argptr, error);
203 vsprintf (text, error, argptr);
206 sprintf (text2, "ERROR: %s\n", text);
207 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
208 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
209 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
210 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
211 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
214 starttime = Sys_DoubleTime ();
215 sc_return_on_enter = true; // so Enter will get us out of here
217 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
223 // switch to windowed so the message box is visible, unless we already
224 // tried that and failed
228 VID_SetDefaultMode ();
229 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
233 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
243 // shut down QHOST hooks if necessary
259 CloseHandle (tevent);
261 if (cls.state == ca_dedicated)
264 // shut down QHOST hooks if necessary
276 double Sys_DoubleTime (void)
278 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
279 #if WIN32_USETIMEGETTIME
282 // Windows 95/98/ME/NT/2000
284 // reasonable accuracy (millisecond)
287 static int first = true;
288 static double oldtime = 0.0, basetime = 0.0, old = 0.0;
291 now = (double) timeGetTime () + basetime;
303 basetime += (65536.0 * 65536.0);
304 now += (65536.0 * 65536.0);
308 newtime = now / 1000.0;
310 if (newtime < oldtime)
311 Sys_Error("Sys_DoubleTime: time running backwards??\n");
317 // QueryPerformanceCounter
319 // Windows 95/98/ME/NT/2000
321 // very accurate (CPU cycles)
323 // does not necessarily match realtime too well (tends to get faster and faster in win98)
324 static int first = true;
325 static double oldtime = 0.0, basetime = 0.0, timescale = 0.0;
327 LARGE_INTEGER PerformanceFreq;
328 LARGE_INTEGER PerformanceCount;
332 if (!QueryPerformanceFrequency (&PerformanceFreq))
333 Sys_Error ("No hardware timer available");
336 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
338 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
342 QueryPerformanceCounter (&PerformanceCount);
345 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale - basetime;
347 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale - basetime;
357 if (newtime < oldtime)
358 Sys_Error("Sys_DoubleTime: time running backwards??\n");
367 char *Sys_ConsoleInput (void)
369 static char text[256];
371 INPUT_RECORD recs[1024];
373 DWORD numread, numevents, dummy;
375 if (cls.state != ca_dedicated)
381 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
382 Sys_Error ("Error getting # of console events");
387 if (!ReadConsoleInput(hinput, recs, 1, &numread))
388 Sys_Error ("Error reading console input");
391 Sys_Error ("Couldn't read console input");
393 if (recs[0].EventType == KEY_EVENT)
395 if (!recs[0].Event.KeyEvent.bKeyDown)
397 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
402 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
410 else if (sc_return_on_enter)
412 // special case to allow exiting from the error handler on Enter
421 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
431 WriteFile(houtput, &ch, 1, &dummy, NULL);
433 len = (len + 1) & 0xff;
446 void Sys_Sleep (void)
452 void Sys_SendKeyEvents (void)
456 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
458 // we always update if there are any event, even if we're paused
461 if (!GetMessage (&msg, NULL, 0, 0))
464 TranslateMessage (&msg);
465 DispatchMessage (&msg);
471 ==============================================================================
475 ==============================================================================
479 void SleepUntilInput (int time)
481 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
490 HINSTANCE global_hInstance;
492 char *argv[MAX_NUM_ARGVS];
493 static char *empty_string = "";
495 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
497 double oldtime, newtime;
498 MEMORYSTATUS lpBuffer;
499 static char cwd[1024];
502 /* previous instances do not exist in Win32 */
506 global_hInstance = hInstance;
507 global_nCmdShow = nCmdShow;
509 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
510 GlobalMemoryStatus (&lpBuffer);
512 if (!GetCurrentDirectory (sizeof(cwd), cwd))
513 Sys_Error ("Couldn't determine current directory");
515 if (cwd[strlen(cwd)-1] == '/')
516 cwd[strlen(cwd)-1] = 0;
518 memset(&host_parms, 0, sizeof(host_parms));
520 host_parms.basedir = cwd;
523 argv[0] = empty_string;
525 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
527 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
532 argv[host_parms.argc] = lpCmdLine;
535 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
547 host_parms.argv = argv;
549 COM_InitArgv (host_parms.argc, host_parms.argv);
551 host_parms.argc = com_argc;
552 host_parms.argv = com_argv;
554 Sys_Shared_EarlyInit();
556 tevent = CreateEvent(NULL, false, false, NULL);
559 Sys_Error ("Couldn't create event");
561 // LordHavoc: can't check cls.state because it hasn't been initialized yet
562 // if (cls.state == ca_dedicated)
563 if (COM_CheckParm("-dedicated"))
565 if (!AllocConsole ())
566 Sys_Error ("Couldn't create dedicated server console");
568 hinput = GetStdHandle (STD_INPUT_HANDLE);
569 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
571 // give QHOST a chance to hook into the console
572 if ((t = COM_CheckParm ("-HFILE")) > 0)
575 hFile = (HANDLE)atoi (com_argv[t+1]);
578 if ((t = COM_CheckParm ("-HPARENT")) > 0)
581 heventParent = (HANDLE)atoi (com_argv[t+1]);
584 if ((t = COM_CheckParm ("-HCHILD")) > 0)
587 heventChild = (HANDLE)atoi (com_argv[t+1]);
590 InitConProc (hFile, heventParent, heventChild);
593 // because sound is off until we become active
596 #if WIN32_USETIMEGETTIME
597 // make sure the timer is high precision, otherwise NT gets 18ms resolution
599 // Windows 2000 Advanced Server (and possibly other versions)
600 // apparently have a broken timer, because it runs at more like 10x speed
601 // if this isn't used, heh
607 Sys_Shared_LateInit();
609 oldtime = Sys_DoubleTime ();
611 /* main window message loop */
614 if (cls.state != ca_dedicated)
616 // yield the CPU for a little while when paused, minimized, or not the focus
617 if ((cl.paused && !ActiveApp) || Minimized)
619 SleepUntilInput (PAUSE_SLEEP);
620 scr_skipupdate = 1; // no point in bothering to draw
623 SleepUntilInput (NOT_FOCUS_SLEEP);
626 newtime = Sys_DoubleTime ();
627 Host_Frame (newtime - oldtime);
631 /* return success of application */