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 HANDLE hinput, houtput;
38 static HANDLE heventParent;
39 static HANDLE heventChild;
43 ===============================================================================
47 ===============================================================================
50 void Sys_Error (const char *error, ...)
54 static int in_sys_error0 = 0;
55 static int in_sys_error1 = 0;
56 static int in_sys_error2 = 0;
57 static int in_sys_error3 = 0;
59 va_start (argptr, error);
60 vsnprintf (text, sizeof (text), error, argptr);
63 Con_Printf ("Quake Error: %s\n", text);
65 // close video so the message box is visible, unless we already tried that
66 if (!in_sys_error0 && cls.state != ca_dedicated)
72 if (!in_sys_error3 && cls.state != ca_dedicated)
75 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
84 // shut down QHOST hooks if necessary
94 void Sys_Shutdown (void)
99 if (cls.state == ca_dedicated)
102 // shut down QHOST hooks if necessary
106 void Sys_PrintToTerminal(const char *text)
109 extern HANDLE houtput;
110 if (cls.state == ca_dedicated)
111 WriteFile(houtput, text, strlen (text), &dummy, NULL);
119 double Sys_DoubleTime (void)
121 static int first = true;
122 static double oldtime = 0.0, curtime = 0.0;
124 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
125 if (sys_usetimegettime.integer)
127 static int firsttimegettime = true;
130 // Windows 95/98/ME/NT/2000/XP
132 // reasonable accuracy (millisecond)
134 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
136 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
137 if (firsttimegettime)
140 firsttimegettime = false;
143 newtime = (double) timeGetTime () / 1000.0;
147 // QueryPerformanceCounter
149 // Windows 95/98/ME/NT/2000/XP
151 // very accurate (CPU cycles)
153 // does not necessarily match realtime too well (tends to get faster and faster in win98)
154 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
156 LARGE_INTEGER PerformanceFreq;
157 LARGE_INTEGER PerformanceCount;
159 if (!QueryPerformanceFrequency (&PerformanceFreq))
160 Sys_Error ("No hardware timer available");
161 QueryPerformanceCounter (&PerformanceCount);
164 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
165 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
167 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
168 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
178 if (newtime < oldtime)
180 // warn if it's significant
181 if (newtime - oldtime < -0.01)
182 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
185 curtime += newtime - oldtime;
192 char *Sys_ConsoleInput (void)
194 static char text[256];
196 INPUT_RECORD recs[1024];
198 DWORD numread, numevents, dummy;
200 if (cls.state != ca_dedicated)
206 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
207 Sys_Error ("Error getting # of console events");
212 if (!ReadConsoleInput(hinput, recs, 1, &numread))
213 Sys_Error ("Error reading console input");
216 Sys_Error ("Couldn't read console input");
218 if (recs[0].EventType == KEY_EVENT)
220 if (!recs[0].Event.KeyEvent.bKeyDown)
222 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
227 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
239 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
249 WriteFile(houtput, &ch, 1, &dummy, NULL);
251 len = (len + 1) & 0xff;
264 void Sys_Sleep(int milliseconds)
266 if (milliseconds < 1)
271 char *Sys_GetClipboardData (void)
276 if (OpenClipboard (NULL) != 0)
278 HANDLE hClipboardData;
280 if ((hClipboardData = GetClipboardData (CF_TEXT)) != 0)
282 if ((cliptext = GlobalLock (hClipboardData)) != 0)
284 data = malloc (GlobalSize(hClipboardData)+1);
285 strcpy (data, cliptext);
286 GlobalUnlock (hClipboardData);
295 ==============================================================================
299 ==============================================================================
308 HINSTANCE global_hInstance;
309 const char *argv[MAX_NUM_ARGVS];
310 char program_name[MAX_OSPATH];
312 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
314 double frameoldtime, framenewtime;
315 MEMORYSTATUS lpBuffer;
318 /* previous instances do not exist in Win32 */
322 global_hInstance = hInstance;
324 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
325 GlobalMemoryStatus (&lpBuffer);
327 program_name[sizeof(program_name)-1] = 0;
328 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
332 argv[0] = program_name;
334 // FIXME: this tokenizer is rather redundent, call a more general one
335 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
337 while (*lpCmdLine && *lpCmdLine <= ' ')
343 if (*lpCmdLine == '\"')
347 argv[com_argc] = lpCmdLine;
349 while (*lpCmdLine && (*lpCmdLine != '\"'))
355 argv[com_argc] = lpCmdLine;
357 while (*lpCmdLine && *lpCmdLine > ' ')
368 Sys_Shared_EarlyInit();
370 Cvar_RegisterVariable(&sys_usetimegettime);
372 tevent = CreateEvent(NULL, false, false, NULL);
375 Sys_Error ("Couldn't create event");
377 // LordHavoc: can't check cls.state because it hasn't been initialized yet
378 // if (cls.state == ca_dedicated)
379 if (COM_CheckParm("-dedicated"))
381 if (!AllocConsole ())
382 Sys_Error ("Couldn't create dedicated server console");
384 hinput = GetStdHandle (STD_INPUT_HANDLE);
385 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
387 // give QHOST a chance to hook into the console
388 if ((t = COM_CheckParm ("-HFILE")) > 0)
391 hFile = (HANDLE)atoi (com_argv[t+1]);
394 if ((t = COM_CheckParm ("-HPARENT")) > 0)
397 heventParent = (HANDLE)atoi (com_argv[t+1]);
400 if ((t = COM_CheckParm ("-HCHILD")) > 0)
403 heventChild = (HANDLE)atoi (com_argv[t+1]);
406 InitConProc (hFile, heventParent, heventChild);
409 // because sound is off until we become active
414 Sys_Shared_LateInit();
416 frameoldtime = Sys_DoubleTime ();
418 /* main window message loop */
421 framenewtime = Sys_DoubleTime ();
422 Host_Frame (framenewtime - frameoldtime);
423 frameoldtime = framenewtime;
426 /* return success of application */