sys_win.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // sys_win.c -- Win32 system interface code
  16. #include "quakedef.h"
  17. #include "winquake.h"
  18. #include "errno.h"
  19. #include "resource.h"
  20. #include "conproc.h"
  21. #define MINIMUM_WIN_MEMORY 0x0880000
  22. #define MAXIMUM_WIN_MEMORY 0x1000000
  23. #define CONSOLE_ERROR_TIMEOUT 60.0 // # of seconds to wait on Sys_Error running
  24. // dedicated before exiting
  25. #define PAUSE_SLEEP 50 // sleep time on pause or minimization
  26. #define NOT_FOCUS_SLEEP 20 // sleep time when not focus
  27. int starttime;
  28. qboolean ActiveApp, Minimized;
  29. qboolean WinNT;
  30. static double pfreq;
  31. static double curtime = 0.0;
  32. static double lastcurtime = 0.0;
  33. static int lowshift;
  34. qboolean isDedicated;
  35. static qboolean sc_return_on_enter = false;
  36. HANDLE hinput, houtput;
  37. static char *tracking_tag = "Clams & Mooses";
  38. static HANDLE tevent;
  39. static HANDLE hFile;
  40. static HANDLE heventParent;
  41. static HANDLE heventChild;
  42. void MaskExceptions (void);
  43. void Sys_InitFloatTime (void);
  44. void Sys_PushFPCW_SetHigh (void);
  45. void Sys_PopFPCW (void);
  46. volatile int sys_checksum;
  47. /*
  48. ================
  49. Sys_PageIn
  50. ================
  51. */
  52. void Sys_PageIn (void *ptr, int size)
  53. {
  54. byte *x;
  55. int j, m, n;
  56. // touch all the memory to make sure it's there. The 16-page skip is to
  57. // keep Win 95 from thinking we're trying to page ourselves in (we are
  58. // doing that, of course, but there's no reason we shouldn't)
  59. x = (byte *)ptr;
  60. for (n=0 ; n<4 ; n++)
  61. {
  62. for (m=0 ; m<(size - 16 * 0x1000) ; m += 4)
  63. {
  64. sys_checksum += *(int *)&x[m];
  65. sys_checksum += *(int *)&x[m + 16 * 0x1000];
  66. }
  67. }
  68. }
  69. /*
  70. ===============================================================================
  71. FILE IO
  72. ===============================================================================
  73. */
  74. #define MAX_HANDLES 10
  75. FILE *sys_handles[MAX_HANDLES];
  76. int findhandle (void)
  77. {
  78. int i;
  79. for (i=1 ; i<MAX_HANDLES ; i++)
  80. if (!sys_handles[i])
  81. return i;
  82. Sys_Error ("out of handles");
  83. return -1;
  84. }
  85. /*
  86. ================
  87. filelength
  88. ================
  89. */
  90. int filelength (FILE *f)
  91. {
  92. int pos;
  93. int end;
  94. int t;
  95. t = VID_ForceUnlockedAndReturnState ();
  96. pos = ftell (f);
  97. fseek (f, 0, SEEK_END);
  98. end = ftell (f);
  99. fseek (f, pos, SEEK_SET);
  100. VID_ForceLockState (t);
  101. return end;
  102. }
  103. int Sys_FileOpenRead (char *path, int *hndl)
  104. {
  105. FILE *f;
  106. int i, retval;
  107. int t;
  108. t = VID_ForceUnlockedAndReturnState ();
  109. i = findhandle ();
  110. f = fopen(path, "rb");
  111. if (!f)
  112. {
  113. *hndl = -1;
  114. retval = -1;
  115. }
  116. else
  117. {
  118. sys_handles[i] = f;
  119. *hndl = i;
  120. retval = filelength(f);
  121. }
  122. VID_ForceLockState (t);
  123. return retval;
  124. }
  125. int Sys_FileOpenWrite (char *path)
  126. {
  127. FILE *f;
  128. int i;
  129. int t;
  130. t = VID_ForceUnlockedAndReturnState ();
  131. i = findhandle ();
  132. f = fopen(path, "wb");
  133. if (!f)
  134. Sys_Error ("Error opening %s: %s", path,strerror(errno));
  135. sys_handles[i] = f;
  136. VID_ForceLockState (t);
  137. return i;
  138. }
  139. void Sys_FileClose (int handle)
  140. {
  141. int t;
  142. t = VID_ForceUnlockedAndReturnState ();
  143. fclose (sys_handles[handle]);
  144. sys_handles[handle] = NULL;
  145. VID_ForceLockState (t);
  146. }
  147. void Sys_FileSeek (int handle, int position)
  148. {
  149. int t;
  150. t = VID_ForceUnlockedAndReturnState ();
  151. fseek (sys_handles[handle], position, SEEK_SET);
  152. VID_ForceLockState (t);
  153. }
  154. int Sys_FileRead (int handle, void *dest, int count)
  155. {
  156. int t, x;
  157. t = VID_ForceUnlockedAndReturnState ();
  158. x = fread (dest, 1, count, sys_handles[handle]);
  159. VID_ForceLockState (t);
  160. return x;
  161. }
  162. int Sys_FileWrite (int handle, void *data, int count)
  163. {
  164. int t, x;
  165. t = VID_ForceUnlockedAndReturnState ();
  166. x = fwrite (data, 1, count, sys_handles[handle]);
  167. VID_ForceLockState (t);
  168. return x;
  169. }
  170. int Sys_FileTime (char *path)
  171. {
  172. FILE *f;
  173. int t, retval;
  174. t = VID_ForceUnlockedAndReturnState ();
  175. f = fopen(path, "rb");
  176. if (f)
  177. {
  178. fclose(f);
  179. retval = 1;
  180. }
  181. else
  182. {
  183. retval = -1;
  184. }
  185. VID_ForceLockState (t);
  186. return retval;
  187. }
  188. void Sys_mkdir (char *path)
  189. {
  190. _mkdir (path);
  191. }
  192. /*
  193. ===============================================================================
  194. SYSTEM IO
  195. ===============================================================================
  196. */
  197. /*
  198. ================
  199. Sys_MakeCodeWriteable
  200. ================
  201. */
  202. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  203. {
  204. DWORD flOldProtect;
  205. if (!VirtualProtect((LPVOID)startaddr, length, PAGE_READWRITE, &flOldProtect))
  206. Sys_Error("Protection change failed\n");
  207. }
  208. #ifndef _M_IX86
  209. void Sys_SetFPCW (void)
  210. {
  211. }
  212. void Sys_PushFPCW_SetHigh (void)
  213. {
  214. }
  215. void Sys_PopFPCW (void)
  216. {
  217. }
  218. void MaskExceptions (void)
  219. {
  220. }
  221. #endif
  222. /*
  223. ================
  224. Sys_Init
  225. ================
  226. */
  227. void Sys_Init (void)
  228. {
  229. LARGE_INTEGER PerformanceFreq;
  230. unsigned int lowpart, highpart;
  231. OSVERSIONINFO vinfo;
  232. MaskExceptions ();
  233. Sys_SetFPCW ();
  234. if (!QueryPerformanceFrequency (&PerformanceFreq))
  235. Sys_Error ("No hardware timer available");
  236. // get 32 out of the 64 time bits such that we have around
  237. // 1 microsecond resolution
  238. lowpart = (unsigned int)PerformanceFreq.LowPart;
  239. highpart = (unsigned int)PerformanceFreq.HighPart;
  240. lowshift = 0;
  241. while (highpart || (lowpart > 2000000.0))
  242. {
  243. lowshift++;
  244. lowpart >>= 1;
  245. lowpart |= (highpart & 1) << 31;
  246. highpart >>= 1;
  247. }
  248. pfreq = 1.0 / (double)lowpart;
  249. Sys_InitFloatTime ();
  250. vinfo.dwOSVersionInfoSize = sizeof(vinfo);
  251. if (!GetVersionEx (&vinfo))
  252. Sys_Error ("Couldn't get OS info");
  253. if ((vinfo.dwMajorVersion < 4) ||
  254. (vinfo.dwPlatformId == VER_PLATFORM_WIN32s))
  255. {
  256. Sys_Error ("WinQuake requires at least Win95 or NT 4.0");
  257. }
  258. if (vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  259. WinNT = true;
  260. else
  261. WinNT = false;
  262. }
  263. void Sys_Error (char *error, ...)
  264. {
  265. va_list argptr;
  266. char text[1024], text2[1024];
  267. char *text3 = "Press Enter to exit\n";
  268. char *text4 = "***********************************\n";
  269. char *text5 = "\n";
  270. DWORD dummy;
  271. double starttime;
  272. static int in_sys_error0 = 0;
  273. static int in_sys_error1 = 0;
  274. static int in_sys_error2 = 0;
  275. static int in_sys_error3 = 0;
  276. if (!in_sys_error3)
  277. {
  278. in_sys_error3 = 1;
  279. VID_ForceUnlockedAndReturnState ();
  280. }
  281. va_start (argptr, error);
  282. vsprintf (text, error, argptr);
  283. va_end (argptr);
  284. if (isDedicated)
  285. {
  286. va_start (argptr, error);
  287. vsprintf (text, error, argptr);
  288. va_end (argptr);
  289. sprintf (text2, "ERROR: %s\n", text);
  290. WriteFile (houtput, text5, strlen (text5), &dummy, NULL);
  291. WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
  292. WriteFile (houtput, text2, strlen (text2), &dummy, NULL);
  293. WriteFile (houtput, text3, strlen (text3), &dummy, NULL);
  294. WriteFile (houtput, text4, strlen (text4), &dummy, NULL);
  295. starttime = Sys_FloatTime ();
  296. sc_return_on_enter = true; // so Enter will get us out of here
  297. while (!Sys_ConsoleInput () &&
  298. ((Sys_FloatTime () - starttime) < CONSOLE_ERROR_TIMEOUT))
  299. {
  300. }
  301. }
  302. else
  303. {
  304. // switch to windowed so the message box is visible, unless we already
  305. // tried that and failed
  306. if (!in_sys_error0)
  307. {
  308. in_sys_error0 = 1;
  309. VID_SetDefaultMode ();
  310. MessageBox(NULL, text, "Quake Error",
  311. MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
  312. }
  313. else
  314. {
  315. MessageBox(NULL, text, "Double Quake Error",
  316. MB_OK | MB_SETFOREGROUND | MB_ICONSTOP);
  317. }
  318. }
  319. if (!in_sys_error1)
  320. {
  321. in_sys_error1 = 1;
  322. Host_Shutdown ();
  323. }
  324. // shut down QHOST hooks if necessary
  325. if (!in_sys_error2)
  326. {
  327. in_sys_error2 = 1;
  328. DeinitConProc ();
  329. }
  330. exit (1);
  331. }
  332. void Sys_Printf (char *fmt, ...)
  333. {
  334. va_list argptr;
  335. char text[1024];
  336. DWORD dummy;
  337. if (isDedicated)
  338. {
  339. va_start (argptr,fmt);
  340. vsprintf (text, fmt, argptr);
  341. va_end (argptr);
  342. WriteFile(houtput, text, strlen (text), &dummy, NULL);
  343. }
  344. }
  345. void Sys_Quit (void)
  346. {
  347. VID_ForceUnlockedAndReturnState ();
  348. Host_Shutdown();
  349. if (tevent)
  350. CloseHandle (tevent);
  351. if (isDedicated)
  352. FreeConsole ();
  353. // shut down QHOST hooks if necessary
  354. DeinitConProc ();
  355. exit (0);
  356. }
  357. /*
  358. ================
  359. Sys_FloatTime
  360. ================
  361. */
  362. double Sys_FloatTime (void)
  363. {
  364. static int sametimecount;
  365. static unsigned int oldtime;
  366. static int first = 1;
  367. LARGE_INTEGER PerformanceCount;
  368. unsigned int temp, t2;
  369. double time;
  370. Sys_PushFPCW_SetHigh ();
  371. QueryPerformanceCounter (&PerformanceCount);
  372. temp = ((unsigned int)PerformanceCount.LowPart >> lowshift) |
  373. ((unsigned int)PerformanceCount.HighPart << (32 - lowshift));
  374. if (first)
  375. {
  376. oldtime = temp;
  377. first = 0;
  378. }
  379. else
  380. {
  381. // check for turnover or backward time
  382. if ((temp <= oldtime) && ((oldtime - temp) < 0x10000000))
  383. {
  384. oldtime = temp; // so we can't get stuck
  385. }
  386. else
  387. {
  388. t2 = temp - oldtime;
  389. time = (double)t2 * pfreq;
  390. oldtime = temp;
  391. curtime += time;
  392. if (curtime == lastcurtime)
  393. {
  394. sametimecount++;
  395. if (sametimecount > 100000)
  396. {
  397. curtime += 1.0;
  398. sametimecount = 0;
  399. }
  400. }
  401. else
  402. {
  403. sametimecount = 0;
  404. }
  405. lastcurtime = curtime;
  406. }
  407. }
  408. Sys_PopFPCW ();
  409. return curtime;
  410. }
  411. /*
  412. ================
  413. Sys_InitFloatTime
  414. ================
  415. */
  416. void Sys_InitFloatTime (void)
  417. {
  418. int j;
  419. Sys_FloatTime ();
  420. j = COM_CheckParm("-starttime");
  421. if (j)
  422. {
  423. curtime = (double) (Q_atof(com_argv[j+1]));
  424. }
  425. else
  426. {
  427. curtime = 0.0;
  428. }
  429. lastcurtime = curtime;
  430. }
  431. char *Sys_ConsoleInput (void)
  432. {
  433. static char text[256];
  434. static int len;
  435. INPUT_RECORD recs[1024];
  436. int count;
  437. int i, dummy;
  438. int ch, numread, numevents;
  439. if (!isDedicated)
  440. return NULL;
  441. for ( ;; )
  442. {
  443. if (!GetNumberOfConsoleInputEvents (hinput, &numevents))
  444. Sys_Error ("Error getting # of console events");
  445. if (numevents <= 0)
  446. break;
  447. if (!ReadConsoleInput(hinput, recs, 1, &numread))
  448. Sys_Error ("Error reading console input");
  449. if (numread != 1)
  450. Sys_Error ("Couldn't read console input");
  451. if (recs[0].EventType == KEY_EVENT)
  452. {
  453. if (!recs[0].Event.KeyEvent.bKeyDown)
  454. {
  455. ch = recs[0].Event.KeyEvent.uChar.AsciiChar;
  456. switch (ch)
  457. {
  458. case '\r':
  459. WriteFile(houtput, "\r\n", 2, &dummy, NULL);
  460. if (len)
  461. {
  462. text[len] = 0;
  463. len = 0;
  464. return text;
  465. }
  466. else if (sc_return_on_enter)
  467. {
  468. // special case to allow exiting from the error handler on Enter
  469. text[0] = '\r';
  470. len = 0;
  471. return text;
  472. }
  473. break;
  474. case '\b':
  475. WriteFile(houtput, "\b \b", 3, &dummy, NULL);
  476. if (len)
  477. {
  478. len--;
  479. }
  480. break;
  481. default:
  482. if (ch >= ' ')
  483. {
  484. WriteFile(houtput, &ch, 1, &dummy, NULL);
  485. text[len] = ch;
  486. len = (len + 1) & 0xff;
  487. }
  488. break;
  489. }
  490. }
  491. }
  492. }
  493. return NULL;
  494. }
  495. void Sys_Sleep (void)
  496. {
  497. Sleep (1);
  498. }
  499. void Sys_SendKeyEvents (void)
  500. {
  501. MSG msg;
  502. while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
  503. {
  504. // we always update if there are any event, even if we're paused
  505. scr_skipupdate = 0;
  506. if (!GetMessage (&msg, NULL, 0, 0))
  507. Sys_Quit ();
  508. TranslateMessage (&msg);
  509. DispatchMessage (&msg);
  510. }
  511. }
  512. /*
  513. ==============================================================================
  514. WINDOWS CRAP
  515. ==============================================================================
  516. */
  517. /*
  518. ==================
  519. WinMain
  520. ==================
  521. */
  522. void SleepUntilInput (int time)
  523. {
  524. MsgWaitForMultipleObjects(1, &tevent, FALSE, time, QS_ALLINPUT);
  525. }
  526. /*
  527. ==================
  528. WinMain
  529. ==================
  530. */
  531. HINSTANCE global_hInstance;
  532. int global_nCmdShow;
  533. char *argv[MAX_NUM_ARGVS];
  534. static char *empty_string = "";
  535. HWND hwnd_dialog;
  536. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  537. {
  538. MSG msg;
  539. quakeparms_t parms;
  540. double time, oldtime, newtime;
  541. MEMORYSTATUS lpBuffer;
  542. static char cwd[1024];
  543. int t;
  544. RECT rect;
  545. /* previous instances do not exist in Win32 */
  546. if (hPrevInstance)
  547. return 0;
  548. global_hInstance = hInstance;
  549. global_nCmdShow = nCmdShow;
  550. lpBuffer.dwLength = sizeof(MEMORYSTATUS);
  551. GlobalMemoryStatus (&lpBuffer);
  552. if (!GetCurrentDirectory (sizeof(cwd), cwd))
  553. Sys_Error ("Couldn't determine current directory");
  554. if (cwd[Q_strlen(cwd)-1] == '/')
  555. cwd[Q_strlen(cwd)-1] = 0;
  556. parms.basedir = cwd;
  557. parms.cachedir = NULL;
  558. parms.argc = 1;
  559. argv[0] = empty_string;
  560. while (*lpCmdLine && (parms.argc < MAX_NUM_ARGVS))
  561. {
  562. while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
  563. lpCmdLine++;
  564. if (*lpCmdLine)
  565. {
  566. argv[parms.argc] = lpCmdLine;
  567. parms.argc++;
  568. while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
  569. lpCmdLine++;
  570. if (*lpCmdLine)
  571. {
  572. *lpCmdLine = 0;
  573. lpCmdLine++;
  574. }
  575. }
  576. }
  577. parms.argv = argv;
  578. COM_InitArgv (parms.argc, parms.argv);
  579. parms.argc = com_argc;
  580. parms.argv = com_argv;
  581. isDedicated = (COM_CheckParm ("-dedicated") != 0);
  582. if (!isDedicated)
  583. {
  584. hwnd_dialog = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);
  585. if (hwnd_dialog)
  586. {
  587. if (GetWindowRect (hwnd_dialog, &rect))
  588. {
  589. if (rect.left > (rect.top * 2))
  590. {
  591. SetWindowPos (hwnd_dialog, 0,
  592. (rect.left / 2) - ((rect.right - rect.left) / 2),
  593. rect.top, 0, 0,
  594. SWP_NOZORDER | SWP_NOSIZE);
  595. }
  596. }
  597. ShowWindow (hwnd_dialog, SW_SHOWDEFAULT);
  598. UpdateWindow (hwnd_dialog);
  599. SetForegroundWindow (hwnd_dialog);
  600. }
  601. }
  602. // take the greater of all the available memory or half the total memory,
  603. // but at least 8 Mb and no more than 16 Mb, unless they explicitly
  604. // request otherwise
  605. parms.memsize = lpBuffer.dwAvailPhys;
  606. if (parms.memsize < MINIMUM_WIN_MEMORY)
  607. parms.memsize = MINIMUM_WIN_MEMORY;
  608. if (parms.memsize < (lpBuffer.dwTotalPhys >> 1))
  609. parms.memsize = lpBuffer.dwTotalPhys >> 1;
  610. if (parms.memsize > MAXIMUM_WIN_MEMORY)
  611. parms.memsize = MAXIMUM_WIN_MEMORY;
  612. if (COM_CheckParm ("-heapsize"))
  613. {
  614. t = COM_CheckParm("-heapsize") + 1;
  615. if (t < com_argc)
  616. parms.memsize = Q_atoi (com_argv[t]) * 1024;
  617. }
  618. parms.membase = malloc (parms.memsize);
  619. if (!parms.membase)
  620. Sys_Error ("Not enough memory free; check disk space\n");
  621. Sys_PageIn (parms.membase, parms.memsize);
  622. tevent = CreateEvent(NULL, FALSE, FALSE, NULL);
  623. if (!tevent)
  624. Sys_Error ("Couldn't create event");
  625. if (isDedicated)
  626. {
  627. if (!AllocConsole ())
  628. {
  629. Sys_Error ("Couldn't create dedicated server console");
  630. }
  631. hinput = GetStdHandle (STD_INPUT_HANDLE);
  632. houtput = GetStdHandle (STD_OUTPUT_HANDLE);
  633. // give QHOST a chance to hook into the console
  634. if ((t = COM_CheckParm ("-HFILE")) > 0)
  635. {
  636. if (t < com_argc)
  637. hFile = (HANDLE)Q_atoi (com_argv[t+1]);
  638. }
  639. if ((t = COM_CheckParm ("-HPARENT")) > 0)
  640. {
  641. if (t < com_argc)
  642. heventParent = (HANDLE)Q_atoi (com_argv[t+1]);
  643. }
  644. if ((t = COM_CheckParm ("-HCHILD")) > 0)
  645. {
  646. if (t < com_argc)
  647. heventChild = (HANDLE)Q_atoi (com_argv[t+1]);
  648. }
  649. InitConProc (hFile, heventParent, heventChild);
  650. }
  651. Sys_Init ();
  652. // because sound is off until we become active
  653. S_BlockSound ();
  654. Sys_Printf ("Host_Init\n");
  655. Host_Init (&parms);
  656. oldtime = Sys_FloatTime ();
  657. /* main window message loop */
  658. while (1)
  659. {
  660. if (isDedicated)
  661. {
  662. newtime = Sys_FloatTime ();
  663. time = newtime - oldtime;
  664. while (time < sys_ticrate.value )
  665. {
  666. Sys_Sleep();
  667. newtime = Sys_FloatTime ();
  668. time = newtime - oldtime;
  669. }
  670. }
  671. else
  672. {
  673. // yield the CPU for a little while when paused, minimized, or not the focus
  674. if ((cl.paused && (!ActiveApp && !DDActive)) || Minimized || block_drawing)
  675. {
  676. SleepUntilInput (PAUSE_SLEEP);
  677. scr_skipupdate = 1; // no point in bothering to draw
  678. }
  679. else if (!ActiveApp && !DDActive)
  680. {
  681. SleepUntilInput (NOT_FOCUS_SLEEP);
  682. }
  683. newtime = Sys_FloatTime ();
  684. time = newtime - oldtime;
  685. }
  686. Host_Frame (time);
  687. oldtime = newtime;
  688. }
  689. /* return success of application */
  690. return TRUE;
  691. }