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;
50 ===============================================================================
54 ===============================================================================
57 dllhandle_t Sys_LoadLibrary (const char* name)
59 return LoadLibrary (name);
62 void Sys_UnloadLibrary (dllhandle_t handle)
67 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
69 return (void *)GetProcAddress (handle, name);
74 ===============================================================================
78 ===============================================================================
81 void SleepUntilInput (int time);
83 void Sys_Error (const char *error, ...)
86 char text[1024], text2[1024];
87 char *text3 = "Press Enter to exit\n";
88 char *text4 = "***********************************\n";
92 static int in_sys_error0 = 0;
93 static int in_sys_error1 = 0;
94 static int in_sys_error2 = 0;
95 static int in_sys_error3 = 0;
100 va_start (argptr, error);
101 vsprintf (text, error, argptr);
104 if (cls.state == ca_dedicated)
106 va_start (argptr, error);
107 vsprintf (text, error, argptr);
110 sprintf (text2, "ERROR: %s\n", text);
111 WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
112 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
113 WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
114 WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
115 WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
118 starttime = Sys_DoubleTime ();
119 sc_return_on_enter = true; // so Enter will get us out of here
121 while (!Sys_ConsoleInput () && ((Sys_DoubleTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
126 // switch to windowed so the message box is visible, unless we already
127 // tried that and failed
132 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
135 MessageBox(NULL, text, "Double Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
144 // shut down QHOST hooks if necessary
159 CloseHandle (tevent);
161 if (cls.state == ca_dedicated)
164 // shut down QHOST hooks if necessary
176 double Sys_DoubleTime (void)
178 static int first = true;
179 static double oldtime = 0.0, curtime = 0.0;
181 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
182 if (sys_usetimegettime.integer)
184 static int firsttimegettime = true;
187 // Windows 95/98/ME/NT/2000/XP
189 // reasonable accuracy (millisecond)
191 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
193 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
194 if (firsttimegettime)
197 firsttimegettime = false;
200 newtime = (double) timeGetTime () / 1000.0;
204 // QueryPerformanceCounter
206 // Windows 95/98/ME/NT/2000/XP
208 // very accurate (CPU cycles)
210 // does not necessarily match realtime too well (tends to get faster and faster in win98)
211 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
213 LARGE_INTEGER PerformanceFreq;
214 LARGE_INTEGER PerformanceCount;
216 if (!QueryPerformanceFrequency (&PerformanceFreq))
217 Sys_Error ("No hardware timer available");
218 QueryPerformanceCounter (&PerformanceCount);
221 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
222 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
224 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
225 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
235 if (newtime < oldtime)
237 // warn if it's significant
238 if (newtime - oldtime < -0.01)
239 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
242 curtime += newtime - oldtime;
249 char *Sys_ConsoleInput (void)
251 static char text[256];
253 INPUT_RECORD recs[1024];
255 DWORD numread, numevents, dummy;
257 if (cls.state != ca_dedicated)
263 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
264 Sys_Error ("Error getting # of console events");
269 if (!ReadConsoleInput(hinput, recs, 1, &numread))
270 Sys_Error ("Error reading console input");
273 Sys_Error ("Couldn't read console input");
275 if (recs[0].EventType == KEY_EVENT)
277 if (!recs[0].Event.KeyEvent.bKeyDown)
279 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
284 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
292 else if (sc_return_on_enter)
294 // special case to allow exiting from the error handler on Enter
303 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
313 WriteFile(houtput, &ch, 1, &dummy, NULL);
315 len = (len + 1) & 0xff;
328 void Sys_Sleep (void)
334 void Sys_SendKeyEvents (void)
338 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
340 // we always update if there are any event, even if we're paused
343 if (!GetMessage (&msg, NULL, 0, 0))
346 TranslateMessage (&msg);
347 DispatchMessage (&msg);
353 ==============================================================================
357 ==============================================================================
361 void SleepUntilInput (int time)
363 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
372 HINSTANCE global_hInstance;
374 const char *argv[MAX_NUM_ARGVS];
375 char program_name[MAX_OSPATH];
377 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
379 double frameoldtime, framenewtime;
380 MEMORYSTATUS lpBuffer;
383 /* previous instances do not exist in Win32 */
387 global_hInstance = hInstance;
388 global_nCmdShow = nCmdShow;
390 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
391 GlobalMemoryStatus (&lpBuffer);
394 program_name[sizeof(program_name)-1] = 0;
395 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
396 argv[0] = program_name;
398 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
400 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
405 argv[com_argc] = lpCmdLine;
408 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
420 Sys_Shared_EarlyInit();
422 Cvar_RegisterVariable(&sys_usetimegettime);
424 tevent = CreateEvent(NULL, false, false, NULL);
427 Sys_Error ("Couldn't create event");
429 // LordHavoc: can't check cls.state because it hasn't been initialized yet
430 // if (cls.state == ca_dedicated)
431 if (COM_CheckParm("-dedicated"))
433 if (!AllocConsole ())
434 Sys_Error ("Couldn't create dedicated server console");
436 hinput = GetStdHandle (STD_INPUT_HANDLE);
437 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
439 // give QHOST a chance to hook into the console
440 if ((t = COM_CheckParm ("-HFILE")) > 0)
443 hFile = (HANDLE)atoi (com_argv[t+1]);
446 if ((t = COM_CheckParm ("-HPARENT")) > 0)
449 heventParent = (HANDLE)atoi (com_argv[t+1]);
452 if ((t = COM_CheckParm ("-HCHILD")) > 0)
455 heventChild = (HANDLE)atoi (com_argv[t+1]);
458 InitConProc (hFile, heventParent, heventChild);
461 // because sound is off until we become active
466 Sys_Shared_LateInit();
468 frameoldtime = Sys_DoubleTime ();
470 /* main window message loop */
473 if (cls.state != ca_dedicated)
475 // yield the CPU for a little while when paused, minimized, or not the focus
476 if ((cl.paused && !vid_activewindow) || vid_hidden)
478 SleepUntilInput (PAUSE_SLEEP);
479 scr_skipupdate = 1; // no point in bothering to draw
481 else if (!vid_activewindow)
482 SleepUntilInput (NOT_FOCUS_SLEEP);
485 framenewtime = Sys_DoubleTime ();
486 Host_Frame (framenewtime - frameoldtime);
487 frameoldtime = framenewtime;
490 /* return success of application */