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 // # of seconds to wait on Sys_Error running dedicated before exiting
32 #define CONSOLE_ERROR_TIMEOUT 60.0
33 // sleep time on pause or minimization
34 #define PAUSE_SLEEP 50
35 // sleep time when not focus
36 #define NOT_FOCUS_SLEEP 20
39 qboolean ActiveApp, Minimized;
41 static qboolean sc_return_on_enter = false;
42 HANDLE hinput, houtput;
46 static HANDLE heventParent;
47 static HANDLE heventChild;
50 ===============================================================================
54 ===============================================================================
57 // LordHavoc: 256 pak files (was 10)
58 #define MAX_HANDLES 256
59 QFile *sys_handles[MAX_HANDLES];
65 for (i=1 ; i<MAX_HANDLES ; i++)
68 Sys_Error ("out of handles");
77 int Sys_FileLength (QFile *f)
83 Qseek (f, 0, SEEK_END);
85 Qseek (f, pos, SEEK_SET);
90 int Sys_FileOpenRead (char *path, int *hndl)
97 f = Qopen(path, "rbz");
108 retval = Sys_FileLength(f);
114 int Sys_FileOpenWrite (char *path)
121 f = Qopen(path, "wb");
124 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
132 void Sys_FileClose (int handle)
134 Qclose (sys_handles[handle]);
135 sys_handles[handle] = NULL;
138 void Sys_FileSeek (int handle, int position)
140 Qseek (sys_handles[handle], position, SEEK_SET);
143 int Sys_FileRead (int handle, void *dest, int count)
145 return Qread (sys_handles[handle], dest, count);
148 int Sys_FileWrite (int handle, void *data, int count)
150 return Qwrite (sys_handles[handle], data, count);
153 int Sys_FileTime (char *path)
157 f = Qopen(path, "rb");
167 void Sys_mkdir (char *path)
174 ===============================================================================
178 ===============================================================================
181 void SleepUntilInput (int time);
183 void Sys_Error (char *error, ...)
186 char text[1024], text2[1024];
187 char *text3 = "Press Enter to exit\n";
188 char *text4 = "***********************************\n";
192 static int in_sys_error0 = 0;
193 static int in_sys_error1 = 0;
194 static int in_sys_error2 = 0;
195 static int in_sys_error3 = 0;
200 va_start (argptr, error);
201 vsprintf (text, error, argptr);
204 if (cls.state == ca_dedicated)
206 va_start (argptr, error);
207 vsprintf (text, error, argptr);
210 sprintf (text2, "ERROR: %s\n", text);
211 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
212 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
213 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
214 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
215 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
218 starttime = Sys_DoubleTime ();
219 sc_return_on_enter = true; // so Enter will get us out of here
221 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
226 // switch to windowed so the message box is visible, unless we already
227 // tried that and failed
231 VID_SetDefaultMode ();
232 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
235 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
244 // 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 static int first = true;
279 static double oldtime = 0.0, curtime = 0.0;
281 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
282 #if WIN32_USETIMEGETTIME
285 // Windows 95/98/ME/NT/2000
287 // reasonable accuracy (millisecond)
289 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
291 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
295 newtime = (double) timeGetTime () / 1000.0;
297 // QueryPerformanceCounter
299 // Windows 95/98/ME/NT/2000
301 // very accurate (CPU cycles)
303 // does not necessarily match realtime too well (tends to get faster and faster in win98)
304 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
305 static double timescale = 0.0;
306 LARGE_INTEGER PerformanceFreq;
307 LARGE_INTEGER PerformanceCount;
309 if (!QueryPerformanceFrequency (&PerformanceFreq))
310 Sys_Error ("No hardware timer available");
311 QueryPerformanceCounter (&PerformanceCount);
314 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
315 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
317 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
318 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
328 if (newtime < oldtime)
329 Con_Printf("Sys_DoubleTime: time running backwards??\n");
332 curtime += newtime - oldtime;
340 char *Sys_ConsoleInput (void)
342 static char text[256];
344 INPUT_RECORD recs[1024];
346 DWORD numread, numevents, dummy;
348 if (cls.state != ca_dedicated)
354 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
355 Sys_Error ("Error getting # of console events");
360 if (!ReadConsoleInput(hinput, recs, 1, &numread))
361 Sys_Error ("Error reading console input");
364 Sys_Error ("Couldn't read console input");
366 if (recs[0].EventType == KEY_EVENT)
368 if (!recs[0].Event.KeyEvent.bKeyDown)
370 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
375 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
383 else if (sc_return_on_enter)
385 // special case to allow exiting from the error handler on Enter
394 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
404 WriteFile(houtput, &ch, 1, &dummy, NULL);
406 len = (len + 1) & 0xff;
419 void Sys_Sleep (void)
425 void Sys_SendKeyEvents (void)
429 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
431 // we always update if there are any event, even if we're paused
434 if (!GetMessage (&msg, NULL, 0, 0))
437 TranslateMessage (&msg);
438 DispatchMessage (&msg);
444 ==============================================================================
448 ==============================================================================
452 void SleepUntilInput (int time)
454 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
463 HINSTANCE global_hInstance;
465 char *argv[MAX_NUM_ARGVS];
466 static char *empty_string = "";
468 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
470 double oldtime, newtime;
471 MEMORYSTATUS lpBuffer;
472 static char cwd[1024];
475 /* previous instances do not exist in Win32 */
479 global_hInstance = hInstance;
480 global_nCmdShow = nCmdShow;
482 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
483 GlobalMemoryStatus (&lpBuffer);
485 if (!GetCurrentDirectory (sizeof(cwd), cwd))
486 Sys_Error ("Couldn't determine current directory");
488 if (cwd[strlen(cwd)-1] == '/')
489 cwd[strlen(cwd)-1] = 0;
491 memset(&host_parms, 0, sizeof(host_parms));
493 host_parms.basedir = cwd;
496 argv[0] = empty_string;
498 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
500 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
505 argv[host_parms.argc] = lpCmdLine;
508 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
520 host_parms.argv = argv;
522 COM_InitArgv (host_parms.argc, host_parms.argv);
524 host_parms.argc = com_argc;
525 host_parms.argv = com_argv;
527 Sys_Shared_EarlyInit();
529 tevent = CreateEvent(NULL, false, false, NULL);
532 Sys_Error ("Couldn't create event");
534 // LordHavoc: can't check cls.state because it hasn't been initialized yet
535 // if (cls.state == ca_dedicated)
536 if (COM_CheckParm("-dedicated"))
538 if (!AllocConsole ())
539 Sys_Error ("Couldn't create dedicated server console");
541 hinput = GetStdHandle (STD_INPUT_HANDLE);
542 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
544 // give QHOST a chance to hook into the console
545 if ((t = COM_CheckParm ("-HFILE")) > 0)
548 hFile = (HANDLE)atoi (com_argv[t+1]);
551 if ((t = COM_CheckParm ("-HPARENT")) > 0)
554 heventParent = (HANDLE)atoi (com_argv[t+1]);
557 if ((t = COM_CheckParm ("-HCHILD")) > 0)
560 heventChild = (HANDLE)atoi (com_argv[t+1]);
563 InitConProc (hFile, heventParent, heventChild);
566 // because sound is off until we become active
571 Sys_Shared_LateInit();
573 oldtime = Sys_DoubleTime ();
575 /* main window message loop */
578 if (cls.state != ca_dedicated)
580 // yield the CPU for a little while when paused, minimized, or not the focus
581 if ((cl.paused && !ActiveApp) || Minimized)
583 SleepUntilInput (PAUSE_SLEEP);
584 scr_skipupdate = 1; // no point in bothering to draw
587 SleepUntilInput (NOT_FOCUS_SLEEP);
590 newtime = Sys_DoubleTime ();
591 Host_Frame (newtime - oldtime);
595 /* return success of application */