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
29 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
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
40 static qboolean sc_return_on_enter = false;
41 HANDLE hinput, houtput;
45 static HANDLE heventParent;
46 static HANDLE heventChild;
49 ===============================================================================
53 ===============================================================================
56 // LordHavoc: 256 pak files (was 10)
57 #define MAX_HANDLES 256
58 QFile *sys_handles[MAX_HANDLES];
64 for (i=1 ; i<MAX_HANDLES ; i++)
67 Sys_Error ("out of handles");
76 int Sys_FileLength (QFile *f)
82 Qseek (f, 0, SEEK_END);
84 Qseek (f, pos, SEEK_SET);
89 int Sys_FileOpenRead (char *path, int *hndl)
96 f = Qopen(path, "rbz");
107 retval = Sys_FileLength(f);
113 int Sys_FileOpenWrite (char *path)
120 f = Qopen(path, "wb");
123 Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
131 void Sys_FileClose (int handle)
133 Qclose (sys_handles[handle]);
134 sys_handles[handle] = NULL;
137 void Sys_FileSeek (int handle, int position)
139 Qseek (sys_handles[handle], position, SEEK_SET);
142 int Sys_FileRead (int handle, void *dest, int count)
144 return Qread (sys_handles[handle], dest, count);
147 int Sys_FileWrite (int handle, void *data, int count)
149 return Qwrite (sys_handles[handle], data, count);
152 int Sys_FileTime (char *path)
156 f = Qopen(path, "rb");
166 void Sys_mkdir (char *path)
173 ===============================================================================
177 ===============================================================================
180 void SleepUntilInput (int time);
182 void Sys_Error (char *error, ...)
185 char text[1024], text2[1024];
186 char *text3 = "Press Enter to exit\n";
187 char *text4 = "***********************************\n";
191 static int in_sys_error0 = 0;
192 static int in_sys_error1 = 0;
193 static int in_sys_error2 = 0;
194 static int in_sys_error3 = 0;
199 va_start (argptr, error);
200 vsprintf (text, error, argptr);
203 if (cls.state == ca_dedicated)
205 va_start (argptr, error);
206 vsprintf (text, error, argptr);
209 sprintf (text2, "ERROR: %s\n", text);
210 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
211 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
212 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
213 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
214 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
217 starttime = Sys_DoubleTime ();
218 sc_return_on_enter = true; // so Enter will get us out of here
220 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
225 // switch to windowed so the message box is visible, unless we already
226 // tried that and failed
230 VID_SetDefaultMode ();
231 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
234 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
243 // shut down QHOST hooks if necessary
258 CloseHandle (tevent);
260 if (cls.state == ca_dedicated)
263 // shut down QHOST hooks if necessary
275 double Sys_DoubleTime (void)
277 static int first = true;
278 static double oldtime = 0.0, curtime = 0.0;
280 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
281 if (sys_usetimegettime.integer)
283 static int firsttimegettime = true;
286 // Windows 95/98/ME/NT/2000/XP
288 // reasonable accuracy (millisecond)
290 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
292 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
293 if (firsttimegettime)
296 firsttimegettime = false;
299 newtime = (double) timeGetTime () / 1000.0;
303 // QueryPerformanceCounter
305 // Windows 95/98/ME/NT/2000/XP
307 // very accurate (CPU cycles)
309 // does not necessarily match realtime too well (tends to get faster and faster in win98)
310 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
312 LARGE_INTEGER PerformanceFreq;
313 LARGE_INTEGER PerformanceCount;
315 if (!QueryPerformanceFrequency (&PerformanceFreq))
316 Sys_Error ("No hardware timer available");
317 QueryPerformanceCounter (&PerformanceCount);
320 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
321 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
323 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
324 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
334 if (newtime < oldtime)
336 // warn if it's significant
337 if (newtime - oldtime < -0.01)
338 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
341 curtime += newtime - oldtime;
348 char *Sys_ConsoleInput (void)
350 static char text[256];
352 INPUT_RECORD recs[1024];
354 DWORD numread, numevents, dummy;
356 if (cls.state != ca_dedicated)
362 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
363 Sys_Error ("Error getting # of console events");
368 if (!ReadConsoleInput(hinput, recs, 1, &numread))
369 Sys_Error ("Error reading console input");
372 Sys_Error ("Couldn't read console input");
374 if (recs[0].EventType == KEY_EVENT)
376 if (!recs[0].Event.KeyEvent.bKeyDown)
378 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
383 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
391 else if (sc_return_on_enter)
393 // special case to allow exiting from the error handler on Enter
402 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
412 WriteFile(houtput, &ch, 1, &dummy, NULL);
414 len = (len + 1) & 0xff;
427 void Sys_Sleep (void)
433 void Sys_SendKeyEvents (void)
437 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
439 // we always update if there are any event, even if we're paused
442 if (!GetMessage (&msg, NULL, 0, 0))
445 TranslateMessage (&msg);
446 DispatchMessage (&msg);
452 ==============================================================================
456 ==============================================================================
460 void SleepUntilInput (int time)
462 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
471 HINSTANCE global_hInstance;
473 char *argv[MAX_NUM_ARGVS];
474 static char *empty_string = "";
476 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
478 double frameoldtime, framenewtime;
479 MEMORYSTATUS lpBuffer;
480 static char cwd[1024];
483 /* previous instances do not exist in Win32 */
487 global_hInstance = hInstance;
488 global_nCmdShow = nCmdShow;
490 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
491 GlobalMemoryStatus (&lpBuffer);
493 if (!GetCurrentDirectory (sizeof(cwd), cwd))
494 Sys_Error ("Couldn't determine current directory");
496 if (cwd[strlen(cwd)-1] == '/')
497 cwd[strlen(cwd)-1] = 0;
499 memset(&host_parms, 0, sizeof(host_parms));
501 host_parms.basedir = cwd;
504 argv[0] = empty_string;
506 while (*lpCmdLine && (host_parms.argc < MAX_NUM_ARGVS))
508 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
513 argv[host_parms.argc] = lpCmdLine;
516 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
528 host_parms.argv = argv;
530 COM_InitArgv (host_parms.argc, host_parms.argv);
532 host_parms.argc = com_argc;
533 host_parms.argv = com_argv;
535 Sys_Shared_EarlyInit();
537 Cvar_RegisterVariable(&sys_usetimegettime);
539 tevent = CreateEvent(NULL, false, false, NULL);
542 Sys_Error ("Couldn't create event");
544 // LordHavoc: can't check cls.state because it hasn't been initialized yet
545 // if (cls.state == ca_dedicated)
546 if (COM_CheckParm("-dedicated"))
548 if (!AllocConsole ())
549 Sys_Error ("Couldn't create dedicated server console");
551 hinput = GetStdHandle (STD_INPUT_HANDLE);
552 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
554 // give QHOST a chance to hook into the console
555 if ((t = COM_CheckParm ("-HFILE")) > 0)
558 hFile = (HANDLE)atoi (com_argv[t+1]);
561 if ((t = COM_CheckParm ("-HPARENT")) > 0)
564 heventParent = (HANDLE)atoi (com_argv[t+1]);
567 if ((t = COM_CheckParm ("-HCHILD")) > 0)
570 heventChild = (HANDLE)atoi (com_argv[t+1]);
573 InitConProc (hFile, heventParent, heventChild);
576 // because sound is off until we become active
581 Sys_Shared_LateInit();
583 frameoldtime = Sys_DoubleTime ();
585 /* main window message loop */
588 if (cls.state != ca_dedicated)
590 // yield the CPU for a little while when paused, minimized, or not the focus
591 if ((cl.paused && !vid_activewindow) || vid_hidden)
593 SleepUntilInput (PAUSE_SLEEP);
594 scr_skipupdate = 1; // no point in bothering to draw
596 else if (!vid_activewindow)
597 SleepUntilInput (NOT_FOCUS_SLEEP);
600 framenewtime = Sys_DoubleTime ();
601 Host_Frame (framenewtime - frameoldtime);
602 frameoldtime = framenewtime;
605 /* return success of application */