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 void SleepUntilInput (int time);
58 void Sys_Error (const char *error, ...)
61 char text[1024], text2[1024];
62 char *text3 = "Press Enter to exit\n";
63 char *text4 = "***********************************\n";
67 static int in_sys_error0 = 0;
68 static int in_sys_error1 = 0;
69 static int in_sys_error2 = 0;
70 static int in_sys_error3 = 0;
75 va_start (argptr, error);
76 vsprintf (text, error, argptr);
79 if (cls.state == ca_dedicated)
81 va_start (argptr, error);
82 vsprintf (text, error, argptr);
85 sprintf (text2, "ERROR: %s\n", text);
86 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
87 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
88 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
89 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
90 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
93 starttime = Sys_DoubleTime ();
94 sc_return_on_enter = true; // so Enter will get us out of here
96 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
101 // switch to windowed so the message box is visible, unless we already
102 // tried that and failed
107 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
110 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
119 // shut down QHOST hooks if necessary
134 CloseHandle (tevent);
136 if (cls.state == ca_dedicated)
139 // shut down QHOST hooks if necessary
151 double Sys_DoubleTime (void)
153 static int first = true;
154 static double oldtime = 0.0, curtime = 0.0;
156 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
157 if (sys_usetimegettime.integer)
159 static int firsttimegettime = true;
162 // Windows 95/98/ME/NT/2000/XP
164 // reasonable accuracy (millisecond)
166 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
168 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
169 if (firsttimegettime)
172 firsttimegettime = false;
175 newtime = (double) timeGetTime () / 1000.0;
179 // QueryPerformanceCounter
181 // Windows 95/98/ME/NT/2000/XP
183 // very accurate (CPU cycles)
185 // does not necessarily match realtime too well (tends to get faster and faster in win98)
186 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
188 LARGE_INTEGER PerformanceFreq;
189 LARGE_INTEGER PerformanceCount;
191 if (!QueryPerformanceFrequency (&PerformanceFreq))
192 Sys_Error ("No hardware timer available");
193 QueryPerformanceCounter (&PerformanceCount);
196 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
197 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
199 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
200 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
210 if (newtime < oldtime)
212 // warn if it's significant
213 if (newtime - oldtime < -0.01)
214 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
217 curtime += newtime - oldtime;
224 char *Sys_ConsoleInput (void)
226 static char text[256];
228 INPUT_RECORD recs[1024];
230 DWORD numread, numevents, dummy;
232 if (cls.state != ca_dedicated)
238 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
239 Sys_Error ("Error getting # of console events");
244 if (!ReadConsoleInput(hinput, recs, 1, &numread))
245 Sys_Error ("Error reading console input");
248 Sys_Error ("Couldn't read console input");
250 if (recs[0].EventType == KEY_EVENT)
252 if (!recs[0].Event.KeyEvent.bKeyDown)
254 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
259 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
267 else if (sc_return_on_enter)
269 // special case to allow exiting from the error handler on Enter
278 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
288 WriteFile(houtput, &ch, 1, &dummy, NULL);
290 len = (len + 1) & 0xff;
303 void Sys_Sleep (void)
309 void Sys_SendKeyEvents (void)
313 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
315 // we always update if there are any event, even if we're paused
318 if (!GetMessage (&msg, NULL, 0, 0))
321 TranslateMessage (&msg);
322 DispatchMessage (&msg);
328 ==============================================================================
332 ==============================================================================
336 void SleepUntilInput (int time)
338 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
347 HINSTANCE global_hInstance;
349 const char *argv[MAX_NUM_ARGVS];
350 char program_name[MAX_OSPATH];
352 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
354 double frameoldtime, framenewtime;
355 MEMORYSTATUS lpBuffer;
358 /* previous instances do not exist in Win32 */
362 global_hInstance = hInstance;
363 global_nCmdShow = nCmdShow;
365 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
366 GlobalMemoryStatus (&lpBuffer);
369 program_name[sizeof(program_name)-1] = 0;
370 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
371 argv[0] = program_name;
373 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
375 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
380 argv[com_argc] = lpCmdLine;
383 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
395 Sys_Shared_EarlyInit();
397 Cvar_RegisterVariable(&sys_usetimegettime);
399 tevent = CreateEvent(NULL, false, false, NULL);
402 Sys_Error ("Couldn't create event");
404 // LordHavoc: can't check cls.state because it hasn't been initialized yet
405 // if (cls.state == ca_dedicated)
406 if (COM_CheckParm("-dedicated"))
408 if (!AllocConsole ())
409 Sys_Error ("Couldn't create dedicated server console");
411 hinput = GetStdHandle (STD_INPUT_HANDLE);
412 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
414 // give QHOST a chance to hook into the console
415 if ((t = COM_CheckParm ("-HFILE")) > 0)
418 hFile = (HANDLE)atoi (com_argv[t+1]);
421 if ((t = COM_CheckParm ("-HPARENT")) > 0)
424 heventParent = (HANDLE)atoi (com_argv[t+1]);
427 if ((t = COM_CheckParm ("-HCHILD")) > 0)
430 heventChild = (HANDLE)atoi (com_argv[t+1]);
433 InitConProc (hFile, heventParent, heventChild);
436 // because sound is off until we become active
441 Sys_Shared_LateInit();
443 frameoldtime = Sys_DoubleTime ();
445 /* main window message loop */
448 if (cls.state != ca_dedicated)
450 // yield the CPU for a little while when paused, minimized, or not the focus
451 if ((cl.paused && !vid_activewindow) || vid_hidden)
453 SleepUntilInput (PAUSE_SLEEP);
454 scr_skipupdate = 1; // no point in bothering to draw
456 else if (!vid_activewindow)
457 SleepUntilInput (NOT_FOCUS_SLEEP);
460 framenewtime = Sys_DoubleTime ();
461 Host_Frame (framenewtime - frameoldtime);
462 frameoldtime = framenewtime;
465 /* return success of application */