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
30 extern void S_BlockSound (void);
31 extern void S_UnblockSound (void);
33 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
35 // # of seconds to wait on Sys_Error running dedicated before exiting
36 #define CONSOLE_ERROR_TIMEOUT 60.0
37 // sleep time on pause or minimization
38 #define PAUSE_SLEEP 50
39 // sleep time when not focus
40 #define NOT_FOCUS_SLEEP 20
42 static qboolean sc_return_on_enter = false;
43 HANDLE hinput, houtput;
47 static HANDLE heventParent;
48 static HANDLE heventChild;
52 ===============================================================================
56 ===============================================================================
59 void SleepUntilInput (int time);
61 void Sys_Error (const char *error, ...)
65 static int in_sys_error0 = 0;
66 static int in_sys_error1 = 0;
67 static int in_sys_error2 = 0;
69 va_start (argptr, error);
70 vsnprintf (text, sizeof (text), error, argptr);
73 // close video so the message box is visible, unless we already tried that
74 if (!in_sys_error0 && cls.state != ca_dedicated)
79 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
87 // shut down QHOST hooks if necessary
102 CloseHandle (tevent);
104 if (cls.state == ca_dedicated)
107 // shut down QHOST hooks if necessary
113 void Sys_Print(const char *text)
116 extern HANDLE houtput;
117 if (cls.state == ca_dedicated)
118 WriteFile(houtput, text, strlen (text), &dummy, NULL);
126 double Sys_DoubleTime (void)
128 static int first = true;
129 static double oldtime = 0.0, curtime = 0.0;
131 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
132 if (sys_usetimegettime.integer)
134 static int firsttimegettime = true;
137 // Windows 95/98/ME/NT/2000/XP
139 // reasonable accuracy (millisecond)
141 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
143 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
144 if (firsttimegettime)
147 firsttimegettime = false;
150 newtime = (double) timeGetTime () / 1000.0;
154 // QueryPerformanceCounter
156 // Windows 95/98/ME/NT/2000/XP
158 // very accurate (CPU cycles)
160 // does not necessarily match realtime too well (tends to get faster and faster in win98)
161 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
163 LARGE_INTEGER PerformanceFreq;
164 LARGE_INTEGER PerformanceCount;
166 if (!QueryPerformanceFrequency (&PerformanceFreq))
167 Sys_Error ("No hardware timer available");
168 QueryPerformanceCounter (&PerformanceCount);
171 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
172 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
174 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
175 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
185 if (newtime < oldtime)
187 // warn if it's significant
188 if (newtime - oldtime < -0.01)
189 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
192 curtime += newtime - oldtime;
199 char *Sys_ConsoleInput (void)
201 static char text[256];
203 INPUT_RECORD recs[1024];
205 DWORD numread, numevents, dummy;
207 if (cls.state != ca_dedicated)
213 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
214 Sys_Error ("Error getting # of console events");
219 if (!ReadConsoleInput(hinput, recs, 1, &numread))
220 Sys_Error ("Error reading console input");
223 Sys_Error ("Couldn't read console input");
225 if (recs[0].EventType == KEY_EVENT)
227 if (!recs[0].Event.KeyEvent.bKeyDown)
229 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
234 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
242 else if (sc_return_on_enter)
244 // special case to allow exiting from the error handler on Enter
253 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
263 WriteFile(houtput, &ch, 1, &dummy, NULL);
265 len = (len + 1) & 0xff;
278 void Sys_Sleep(int milliseconds)
280 if (milliseconds < 1)
286 void Sys_SendKeyEvents (void)
290 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
292 // we always update if there are any event, even if we're paused
295 if (!GetMessage (&msg, NULL, 0, 0))
298 TranslateMessage (&msg);
299 DispatchMessage (&msg);
305 ==============================================================================
309 ==============================================================================
313 void SleepUntilInput (int time)
315 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
324 HINSTANCE global_hInstance;
325 const char *argv[MAX_NUM_ARGVS];
326 char program_name[MAX_OSPATH];
328 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
330 double frameoldtime, framenewtime;
331 MEMORYSTATUS lpBuffer;
334 /* previous instances do not exist in Win32 */
338 global_hInstance = hInstance;
340 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
341 GlobalMemoryStatus (&lpBuffer);
344 program_name[sizeof(program_name)-1] = 0;
345 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
346 argv[0] = program_name;
348 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
350 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
355 argv[com_argc] = lpCmdLine;
358 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
370 Sys_Shared_EarlyInit();
372 Cvar_RegisterVariable(&sys_usetimegettime);
374 tevent = CreateEvent(NULL, false, false, NULL);
377 Sys_Error ("Couldn't create event");
379 // LordHavoc: can't check cls.state because it hasn't been initialized yet
380 // if (cls.state == ca_dedicated)
381 if (COM_CheckParm("-dedicated"))
383 if (!AllocConsole ())
384 Sys_Error ("Couldn't create dedicated server console");
386 hinput = GetStdHandle (STD_INPUT_HANDLE);
387 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
389 // give QHOST a chance to hook into the console
390 if ((t = COM_CheckParm ("-HFILE")) > 0)
393 hFile = (HANDLE)atoi (com_argv[t+1]);
396 if ((t = COM_CheckParm ("-HPARENT")) > 0)
399 heventParent = (HANDLE)atoi (com_argv[t+1]);
402 if ((t = COM_CheckParm ("-HCHILD")) > 0)
405 heventChild = (HANDLE)atoi (com_argv[t+1]);
408 InitConProc (hFile, heventParent, heventChild);
411 // because sound is off until we become active
416 Sys_Shared_LateInit();
418 frameoldtime = Sys_DoubleTime ();
420 /* main window message loop */
423 if (cls.state != ca_dedicated)
425 // yield the CPU for a little while when paused, minimized, or not the focus
426 if ((cl.paused && !vid_activewindow) || vid_hidden)
428 SleepUntilInput (PAUSE_SLEEP);
429 scr_skipupdate = 1; // no point in bothering to draw
431 else if (!vid_activewindow)
432 SleepUntilInput (NOT_FOCUS_SLEEP);
435 framenewtime = Sys_DoubleTime ();
436 Host_Frame (framenewtime - frameoldtime);
437 frameoldtime = framenewtime;
440 /* return success of application */