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);
32 cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"};
34 // # of seconds to wait on Sys_Error running dedicated before exiting
35 #define CONSOLE_ERROR_TIMEOUT 60.0
36 // sleep time on pause or minimization
37 #define PAUSE_SLEEP 50
38 // sleep time when not focus
39 #define NOT_FOCUS_SLEEP 20
41 static qboolean sc_return_on_enter = false;
42 HANDLE hinput, houtput;
46 static HANDLE heventParent;
47 static HANDLE heventChild;
51 ===============================================================================
55 ===============================================================================
58 void SleepUntilInput (int time);
60 void Sys_Error (const char *error, ...)
64 static int in_sys_error0 = 0;
65 static int in_sys_error1 = 0;
66 static int in_sys_error2 = 0;
68 va_start (argptr, error);
69 vsnprintf (text, sizeof (text), error, argptr);
72 // close video so the message box is visible, unless we already tried that
73 if (!in_sys_error0 && cls.state != ca_dedicated)
78 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
86 // shut down QHOST hooks if necessary
96 void Sys_Shutdown (void)
101 if (cls.state == ca_dedicated)
104 // shut down QHOST hooks if necessary
108 void Sys_PrintToTerminal(const char *text)
111 extern HANDLE houtput;
112 if (cls.state == ca_dedicated)
113 WriteFile(houtput, text, strlen (text), &dummy, NULL);
121 double Sys_DoubleTime (void)
123 static int first = true;
124 static double oldtime = 0.0, curtime = 0.0;
126 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
127 if (sys_usetimegettime.integer)
129 static int firsttimegettime = true;
132 // Windows 95/98/ME/NT/2000/XP
134 // reasonable accuracy (millisecond)
136 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
138 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
139 if (firsttimegettime)
142 firsttimegettime = false;
145 newtime = (double) timeGetTime () / 1000.0;
149 // QueryPerformanceCounter
151 // Windows 95/98/ME/NT/2000/XP
153 // very accurate (CPU cycles)
155 // does not necessarily match realtime too well (tends to get faster and faster in win98)
156 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
158 LARGE_INTEGER PerformanceFreq;
159 LARGE_INTEGER PerformanceCount;
161 if (!QueryPerformanceFrequency (&PerformanceFreq))
162 Sys_Error ("No hardware timer available");
163 QueryPerformanceCounter (&PerformanceCount);
166 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
167 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
169 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
170 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
180 if (newtime < oldtime)
182 // warn if it's significant
183 if (newtime - oldtime < -0.01)
184 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
187 curtime += newtime - oldtime;
194 char *Sys_ConsoleInput (void)
196 static char text[256];
198 INPUT_RECORD recs[1024];
200 DWORD numread, numevents, dummy;
202 if (cls.state != ca_dedicated)
208 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
209 Sys_Error ("Error getting # of console events");
214 if (!ReadConsoleInput(hinput, recs, 1, &numread))
215 Sys_Error ("Error reading console input");
218 Sys_Error ("Couldn't read console input");
220 if (recs[0].EventType == KEY_EVENT)
222 if (!recs[0].Event.KeyEvent.bKeyDown)
224 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
229 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
237 else if (sc_return_on_enter)
239 // special case to allow exiting from the error handler on Enter
248 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
258 WriteFile(houtput, &ch, 1, &dummy, NULL);
260 len = (len + 1) & 0xff;
273 void Sys_Sleep(int milliseconds)
275 if (milliseconds < 1)
281 void Sys_SendKeyEvents (void)
285 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
287 // we always update if there are any event, even if we're paused
290 if (!GetMessage (&msg, NULL, 0, 0))
293 TranslateMessage (&msg);
294 DispatchMessage (&msg);
300 ==============================================================================
304 ==============================================================================
308 void SleepUntilInput (int time)
310 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
319 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;
335 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
336 GlobalMemoryStatus (&lpBuffer);
339 program_name[sizeof(program_name)-1] = 0;
340 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
341 argv[0] = program_name;
343 // FIXME: this tokenizer is rather redundent, call a more general one
344 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
346 while (*lpCmdLine && *lpCmdLine <= 32)
351 if (*lpCmdLine == '\"')
354 argv[com_argc] = lpCmdLine;
357 while (*lpCmdLine && (*lpCmdLine != '\"'))
366 if (*lpCmdLine == '\"')
372 argv[com_argc] = lpCmdLine;
375 while (*lpCmdLine && *lpCmdLine > 32)
388 Sys_Shared_EarlyInit();
390 Cvar_RegisterVariable(&sys_usetimegettime);
392 tevent = CreateEvent(NULL, false, false, NULL);
395 Sys_Error ("Couldn't create event");
397 // LordHavoc: can't check cls.state because it hasn't been initialized yet
398 // if (cls.state == ca_dedicated)
399 if (COM_CheckParm("-dedicated"))
401 if (!AllocConsole ())
402 Sys_Error ("Couldn't create dedicated server console");
404 hinput = GetStdHandle (STD_INPUT_HANDLE);
405 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
407 // give QHOST a chance to hook into the console
408 if ((t = COM_CheckParm ("-HFILE")) > 0)
411 hFile = (HANDLE)atoi (com_argv[t+1]);
414 if ((t = COM_CheckParm ("-HPARENT")) > 0)
417 heventParent = (HANDLE)atoi (com_argv[t+1]);
420 if ((t = COM_CheckParm ("-HCHILD")) > 0)
423 heventChild = (HANDLE)atoi (com_argv[t+1]);
426 InitConProc (hFile, heventParent, heventChild);
429 // because sound is off until we become active
434 Sys_Shared_LateInit();
436 frameoldtime = Sys_DoubleTime ();
438 /* main window message loop */
441 if (cls.state != ca_dedicated)
443 // yield the CPU for a little while when paused, minimized, or not the focus
444 if ((cl.paused && !vid_activewindow) || vid_hidden)
446 SleepUntilInput (PAUSE_SLEEP);
447 scr_skipupdate = 1; // no point in bothering to draw
449 else if (!vid_activewindow)
450 SleepUntilInput (NOT_FOCUS_SLEEP);
453 framenewtime = Sys_DoubleTime ();
454 Host_Frame (framenewtime - frameoldtime);
455 frameoldtime = framenewtime;
458 /* return success of application */