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, ...)
87 static int in_sys_error0 = 0;
88 static int in_sys_error1 = 0;
89 static int in_sys_error2 = 0;
91 va_start (argptr, error);
92 vsprintf (text, error, argptr);
95 // close video so the message box is visible, unless we already tried that
96 if (!in_sys_error0 && cls.state != ca_dedicated)
101 MessageBox(NULL, text, "Quake Error", MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
109 // shut down QHOST hooks if necessary
124 CloseHandle (tevent);
126 if (cls.state == ca_dedicated)
129 // shut down QHOST hooks if necessary
135 void Sys_Print(const char *text)
138 extern HANDLE houtput;
139 if (cls.state == ca_dedicated)
140 WriteFile(houtput, text, strlen (text), &dummy, NULL);
148 double Sys_DoubleTime (void)
150 static int first = true;
151 static double oldtime = 0.0, curtime = 0.0;
153 // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine.
154 if (sys_usetimegettime.integer)
156 static int firsttimegettime = true;
159 // Windows 95/98/ME/NT/2000/XP
161 // reasonable accuracy (millisecond)
163 // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter)
165 // make sure the timer is high precision, otherwise different versions of windows have varying accuracy
166 if (firsttimegettime)
169 firsttimegettime = false;
172 newtime = (double) timeGetTime () / 1000.0;
176 // QueryPerformanceCounter
178 // Windows 95/98/ME/NT/2000/XP
180 // very accurate (CPU cycles)
182 // does not necessarily match realtime too well (tends to get faster and faster in win98)
183 // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors)
185 LARGE_INTEGER PerformanceFreq;
186 LARGE_INTEGER PerformanceCount;
188 if (!QueryPerformanceFrequency (&PerformanceFreq))
189 Sys_Error ("No hardware timer available");
190 QueryPerformanceCounter (&PerformanceCount);
193 timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0);
194 newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale;
196 timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0);
197 newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale;
207 if (newtime < oldtime)
209 // warn if it's significant
210 if (newtime - oldtime < -0.01)
211 Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime);
214 curtime += newtime - oldtime;
221 char *Sys_ConsoleInput (void)
223 static char text[256];
225 INPUT_RECORD recs[1024];
227 DWORD numread, numevents, dummy;
229 if (cls.state != ca_dedicated)
235 if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
236 Sys_Error ("Error getting # of console events");
241 if (!ReadConsoleInput(hinput, recs, 1, &numread))
242 Sys_Error ("Error reading console input");
245 Sys_Error ("Couldn't read console input");
247 if (recs[0].EventType == KEY_EVENT)
249 if (!recs[0].Event.KeyEvent.bKeyDown)
251 ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
256 WriteFile(houtput, "\r\n", 2, &dummy, NULL);
264 else if (sc_return_on_enter)
266 // special case to allow exiting from the error handler on Enter
275 WriteFile(houtput, "\b \b", 3, &dummy, NULL);
285 WriteFile(houtput, &ch, 1, &dummy, NULL);
287 len = (len + 1) & 0xff;
300 void Sys_Sleep (void)
306 void Sys_SendKeyEvents (void)
310 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
312 // we always update if there are any event, even if we're paused
315 if (!GetMessage (&msg, NULL, 0, 0))
318 TranslateMessage (&msg);
319 DispatchMessage (&msg);
325 ==============================================================================
329 ==============================================================================
333 void SleepUntilInput (int time)
335 MsgWaitForMultipleObjects(1, &tevent, false, time, QS_ALLINPUT);
344 HINSTANCE global_hInstance;
346 const char *argv[MAX_NUM_ARGVS];
347 char program_name[MAX_OSPATH];
349 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
351 double frameoldtime, framenewtime;
352 MEMORYSTATUS lpBuffer;
355 /* previous instances do not exist in Win32 */
359 global_hInstance = hInstance;
360 global_nCmdShow = nCmdShow;
362 lpBuffer.dwLength = sizeof(MEMORYSTATUS);
363 GlobalMemoryStatus (&lpBuffer);
366 program_name[sizeof(program_name)-1] = 0;
367 GetModuleFileNameA(NULL, program_name, sizeof(program_name) - 1);
368 argv[0] = program_name;
370 while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
372 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
377 argv[com_argc] = lpCmdLine;
380 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
392 Sys_Shared_EarlyInit();
394 Cvar_RegisterVariable(&sys_usetimegettime);
396 tevent = CreateEvent(NULL, false, false, NULL);
399 Sys_Error ("Couldn't create event");
401 // LordHavoc: can't check cls.state because it hasn't been initialized yet
402 // if (cls.state == ca_dedicated)
403 if (COM_CheckParm("-dedicated"))
405 if (!AllocConsole ())
406 Sys_Error ("Couldn't create dedicated server console");
408 hinput = GetStdHandle (STD_INPUT_HANDLE);
409 houtput = GetStdHandle (STD_OUTPUT_HANDLE);
411 // give QHOST a chance to hook into the console
412 if ((t = COM_CheckParm ("-HFILE")) > 0)
415 hFile = (HANDLE)atoi (com_argv[t+1]);
418 if ((t = COM_CheckParm ("-HPARENT")) > 0)
421 heventParent = (HANDLE)atoi (com_argv[t+1]);
424 if ((t = COM_CheckParm ("-HCHILD")) > 0)
427 heventChild = (HANDLE)atoi (com_argv[t+1]);
430 InitConProc (hFile, heventParent, heventChild);
433 // because sound is off until we become active
438 Sys_Shared_LateInit();
440 frameoldtime = Sys_DoubleTime ();
442 /* main window message loop */
445 if (cls.state != ca_dedicated)
447 // yield the CPU for a little while when paused, minimized, or not the focus
448 if ((cl.paused && !vid_activewindow) || vid_hidden)
450 SleepUntilInput (PAUSE_SLEEP);
451 scr_skipupdate = 1; // no point in bothering to draw
453 else if (!vid_activewindow)
454 SleepUntilInput (NOT_FOCUS_SLEEP);
457 framenewtime = Sys_DoubleTime ();
458 Host_Frame (framenewtime - frameoldtime);
459 frameoldtime = framenewtime;
462 /* return success of application */