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
38 static qboolean sc_return_on_enter = false;
39 HANDLE hinput, houtput;
43 static HANDLE heventParent;
44 static HANDLE heventChild;
48 ===============================================================================
52 ===============================================================================
55 void SleepUntilInput (int time);
57 void Sys_Error (const char *error, ...)
61 static int in_sys_error0 = 0;
62 static int in_sys_error1 = 0;
63 static int in_sys_error2 = 0;
65 va_start (argptr, error);
66 vsprintf (text, error, argptr);
69 // close video so the message box is visible, unless we already tried that
70 if (!in_sys_error0 && cls.state != ca_dedicated)
75 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
83 // shut down QHOST hooks if necessary
100 if (cls.state == ca_dedicated)
103 // shut down QHOST hooks if necessary
109 void Sys_Print(const char *text)
112 extern HANDLE houtput;
113 if (cls.state == ca_dedicated)
114 WriteFile(houtput, text, strlen (text), &dummy, NULL);
122 double Sys_DoubleTime (void)
124 static int first = true;
125 static double oldtime = 0.0, curtime = 0.0;
127 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
128 if (sys_usetimegettime.integer)
130 static int firsttimegettime = true;
133 // Windows 95/98/ME/NT/2000/XP
135 // reasonable accuracy (millisecond)
137 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
139 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
140 if (firsttimegettime)
143 firsttimegettime = false;
146 newtime = (double) timeGetTime () / 1000.0;
150 // QueryPerformanceCounter
152 // Windows 95/98/ME/NT/2000/XP
154 // very accurate (CPU cycles)
156 // does not necessarily match realtime too well (tends to get faster and faster in win98)
157 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
159 LARGE_INTEGER PerformanceFreq;
160 LARGE_INTEGER PerformanceCount;
162 if (!QueryPerformanceFrequency (&PerformanceFreq))
163 Sys_Error ("No hardware timer available");
164 QueryPerformanceCounter (&PerformanceCount);
167 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
168 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
170 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
171 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
181 if (newtime < oldtime)
183 // warn if it's significant
184 if (newtime - oldtime < -0.01)
185 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
188 curtime += newtime - oldtime;
195 char *Sys_ConsoleInput (void)
197 static char text[256];
199 INPUT_RECORD recs[1024];
201 DWORD numread, numevents, dummy;
203 if (cls.state != ca_dedicated)
209 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
210 Sys_Error ("Error getting # of console events");
215 if (!ReadConsoleInput(hinput, recs, 1, &numread))
216 Sys_Error ("Error reading console input");
219 Sys_Error ("Couldn't read console input");
221 if (recs[0].EventType == KEY_EVENT)
223 if (!recs[0].Event.KeyEvent.bKeyDown)
225 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
230 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
238 else if (sc_return_on_enter)
240 // special case to allow exiting from the error handler on Enter
249 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
259 WriteFile(houtput, &ch, 1, &dummy, NULL);
261 len = (len + 1) & 0xff;
274 void Sys_Sleep (void)
280 void Sys_SendKeyEvents (void)
284 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
286 // we always update if there are any event, even if we're paused
289 if (!GetMessage (&msg, NULL, 0, 0))
292 TranslateMessage (&msg);
293 DispatchMessage (&msg);
299 ==============================================================================
303 ==============================================================================
307 void SleepUntilInput (int time)
309 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
318 HINSTANCE global_hInstance;
320 const char *argv[MAX_NUM_ARGVS];
321 char program_name[MAX_OSPATH];
323 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
325 double frameoldtime, framenewtime;
326 MEMORYSTATUS lpBuffer;
329 /* previous instances do not exist in Win32 */
333 global_hInstance = hInstance;
334 global_nCmdShow = nCmdShow;
336 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
337 GlobalMemoryStatus (&lpBuffer);
340 program_name[sizeof(program_name)-1] = 0;
341 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
342 argv[0] = program_name;
344 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
346 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
351 argv[com_argc] = lpCmdLine;
354 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
366 Sys_Shared_EarlyInit();
368 Cvar_RegisterVariable(&sys_usetimegettime);
370 tevent = CreateEvent(NULL, false, false, NULL);
373 Sys_Error ("Couldn't create event");
375 // LordHavoc: can't check cls.state because it hasn't been initialized yet
376 // if (cls.state == ca_dedicated)
377 if (COM_CheckParm("-dedicated"))
379 if (!AllocConsole ())
380 Sys_Error ("Couldn't create dedicated server console");
382 hinput = GetStdHandle (STD_INPUT_HANDLE);
383 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
385 // give QHOST a chance to hook into the console
386 if ((t = COM_CheckParm ("-HFILE")) > 0)
389 hFile = (HANDLE)atoi (com_argv[t+1]);
392 if ((t = COM_CheckParm ("-HPARENT")) > 0)
395 heventParent = (HANDLE)atoi (com_argv[t+1]);
398 if ((t = COM_CheckParm ("-HCHILD")) > 0)
401 heventChild = (HANDLE)atoi (com_argv[t+1]);
404 InitConProc (hFile, heventParent, heventChild);
407 // because sound is off until we become active
412 Sys_Shared_LateInit();
414 frameoldtime = Sys_DoubleTime ();
416 /* main window message loop */
419 if (cls.state != ca_dedicated)
421 // yield the CPU for a little while when paused, minimized, or not the focus
422 if ((cl.paused && !vid_activewindow) || vid_hidden)
424 SleepUntilInput (PAUSE_SLEEP);
425 scr_skipupdate = 1; // no point in bothering to draw
427 else if (!vid_activewindow)
428 SleepUntilInput (NOT_FOCUS_SLEEP);
431 framenewtime = Sys_DoubleTime ();
432 Host_Frame (framenewtime - frameoldtime);
433 frameoldtime = framenewtime;
436 /* return success of application */