win_qe3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qe3.h"
  19. #include "mru.h"
  20. int screen_width;
  21. int screen_height;
  22. qboolean have_quit;
  23. int update_bits;
  24. HANDLE bsp_process;
  25. //===========================================
  26. void Sys_SetTitle (char *text)
  27. {
  28. SetWindowText (g_qeglobals.d_hwndMain, text);
  29. }
  30. HCURSOR waitcursor;
  31. void Sys_BeginWait (void)
  32. {
  33. waitcursor = SetCursor (LoadCursor (NULL, IDC_WAIT));
  34. }
  35. void Sys_EndWait (void)
  36. {
  37. if (waitcursor)
  38. {
  39. SetCursor (waitcursor);
  40. waitcursor = NULL;
  41. }
  42. }
  43. void Sys_GetCursorPos (int *x, int *y)
  44. {
  45. POINT lpPoint;
  46. GetCursorPos (&lpPoint);
  47. *x = lpPoint.x;
  48. *y = lpPoint.y;
  49. }
  50. void Sys_SetCursorPos (int x, int y)
  51. {
  52. SetCursorPos (x, y);
  53. }
  54. void Sys_UpdateWindows (int bits)
  55. {
  56. // Sys_Printf("updating 0x%X\n", bits);
  57. update_bits |= bits;
  58. //update_bits = -1;
  59. }
  60. void Sys_Beep (void)
  61. {
  62. MessageBeep (MB_ICONASTERISK);
  63. }
  64. char *TranslateString (char *buf)
  65. {
  66. static char buf2[32768];
  67. int i, l;
  68. char *out;
  69. l = strlen(buf);
  70. out = buf2;
  71. for (i=0 ; i<l ; i++)
  72. {
  73. if (buf[i] == '\n')
  74. {
  75. *out++ = '\r';
  76. *out++ = '\n';
  77. }
  78. else
  79. *out++ = buf[i];
  80. }
  81. *out++ = 0;
  82. return buf2;
  83. }
  84. void Sys_ClearPrintf (void)
  85. {
  86. char text[4];
  87. text[0] = 0;
  88. SendMessage (g_qeglobals.d_hwndEdit,
  89. WM_SETTEXT,
  90. 0,
  91. (LPARAM)text);
  92. }
  93. void Sys_Printf (char *text, ...)
  94. {
  95. va_list argptr;
  96. char buf[32768];
  97. char *out;
  98. va_start (argptr,text);
  99. vsprintf (buf, text,argptr);
  100. va_end (argptr);
  101. out = TranslateString (buf);
  102. #ifdef LATER
  103. Sys_Status(out);
  104. #else
  105. SendMessage (g_qeglobals.d_hwndEdit,
  106. EM_REPLACESEL,
  107. 0,
  108. (LPARAM)out);
  109. #endif
  110. }
  111. double Sys_DoubleTime (void)
  112. {
  113. return clock()/ 1000.0;
  114. }
  115. void PrintPixels (HDC hDC)
  116. {
  117. int i;
  118. PIXELFORMATDESCRIPTOR p[64];
  119. printf ("### flags color layer\n");
  120. for (i=1 ; i<64 ; i++)
  121. {
  122. if (!DescribePixelFormat ( hDC, i, sizeof(p[0]), &p[i]))
  123. break;
  124. printf ("%3i %5i %5i %5i\n", i,
  125. p[i].dwFlags,
  126. p[i].cColorBits,
  127. p[i].bReserved);
  128. }
  129. printf ("%i modes\n", i-1);
  130. }
  131. //==========================================================================
  132. void QEW_StopGL( HWND hWnd, HGLRC hGLRC, HDC hDC )
  133. {
  134. wglMakeCurrent( NULL, NULL );
  135. wglDeleteContext( hGLRC );
  136. ReleaseDC( hWnd, hDC );
  137. }
  138. int QEW_SetupPixelFormat(HDC hDC, qboolean zbuffer )
  139. {
  140. static PIXELFORMATDESCRIPTOR pfd = {
  141. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  142. 1, // version number
  143. PFD_DRAW_TO_WINDOW | // support window
  144. PFD_SUPPORT_OPENGL | // support OpenGL
  145. PFD_DOUBLEBUFFER, // double buffered
  146. PFD_TYPE_RGBA, // RGBA type
  147. 24, // 24-bit color depth
  148. 0, 0, 0, 0, 0, 0, // color bits ignored
  149. 0, // no alpha buffer
  150. 0, // shift bit ignored
  151. 0, // no accumulation buffer
  152. 0, 0, 0, 0, // accum bits ignored
  153. 32, // depth bits
  154. 0, // no stencil buffer
  155. 0, // no auxiliary buffer
  156. PFD_MAIN_PLANE, // main layer
  157. 0, // reserved
  158. 0, 0, 0 // layer masks ignored
  159. };
  160. int pixelformat = 0;
  161. zbuffer = true;
  162. if ( !zbuffer )
  163. pfd.cDepthBits = 0;
  164. if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
  165. {
  166. printf("%d",GetLastError());
  167. Error ("ChoosePixelFormat failed");
  168. }
  169. if (!SetPixelFormat(hDC, pixelformat, &pfd))
  170. Error ("SetPixelFormat failed");
  171. return pixelformat;
  172. }
  173. /*
  174. =================
  175. Error
  176. For abnormal program terminations
  177. =================
  178. */
  179. void Error (char *error, ...)
  180. {
  181. va_list argptr;
  182. char text[1024];
  183. char text2[1024];
  184. int err;
  185. err = GetLastError ();
  186. va_start (argptr,error);
  187. vsprintf (text, error,argptr);
  188. va_end (argptr);
  189. sprintf (text2, "%s\nGetLastError() = %i", text, err);
  190. MessageBox(g_qeglobals.d_hwndMain, text2, "Error", 0 /* MB_OK */ );
  191. exit (1);
  192. }
  193. /*
  194. ======================================================================
  195. FILE DIALOGS
  196. ======================================================================
  197. */
  198. qboolean ConfirmModified (void)
  199. {
  200. if (!modified)
  201. return true;
  202. if (MessageBox (g_qeglobals.d_hwndMain, "This will lose changes to the map"
  203. , "warning", MB_OKCANCEL) == IDCANCEL)
  204. return false;
  205. return true;
  206. }
  207. static OPENFILENAME ofn; /* common dialog box structure */
  208. static char szDirName[MAX_PATH]; /* directory string */
  209. static char szFile[260]; /* filename string */
  210. static char szFileTitle[260]; /* file title string */
  211. static char szFilter[260] = /* filter string */
  212. "QuakeEd file (*.map)\0*.map\0\0";
  213. static char szProjectFilter[260] = /* filter string */
  214. "QuakeEd project (*.qe4)\0*.qe4\0\0";
  215. static char chReplace; /* string separator for szFilter */
  216. static int i, cbString; /* integer count variables */
  217. static HANDLE hf; /* file handle */
  218. void OpenDialog (void)
  219. {
  220. /*
  221. * Obtain the system directory name and
  222. * store it in szDirName.
  223. */
  224. strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "basepath") );
  225. strcat (szDirName, "\\maps");
  226. /* Place the terminating null character in the szFile. */
  227. szFile[0] = '\0';
  228. /* Set the members of the OPENFILENAME structure. */
  229. ofn.lStructSize = sizeof(OPENFILENAME);
  230. ofn.hwndOwner = g_qeglobals.d_hwndCamera;
  231. ofn.lpstrFilter = szFilter;
  232. ofn.nFilterIndex = 1;
  233. ofn.lpstrFile = szFile;
  234. ofn.nMaxFile = sizeof(szFile);
  235. ofn.lpstrFileTitle = szFileTitle;
  236. ofn.nMaxFileTitle = sizeof(szFileTitle);
  237. ofn.lpstrInitialDir = szDirName;
  238. ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  239. OFN_FILEMUSTEXIST;
  240. /* Display the Open dialog box. */
  241. if (!GetOpenFileName(&ofn))
  242. return; // canceled
  243. // Add the file in MRU.
  244. AddNewItem( g_qeglobals.d_lpMruMenu, ofn.lpstrFile);
  245. // Refresh the File menu.
  246. PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
  247. ID_FILE_EXIT);
  248. /* Open the file. */
  249. Map_LoadFile (ofn.lpstrFile);
  250. }
  251. void ProjectDialog (void)
  252. {
  253. /*
  254. * Obtain the system directory name and
  255. * store it in szDirName.
  256. */
  257. strcpy (szDirName, ValueForKey(g_qeglobals.d_project_entity, "basepath") );
  258. strcat (szDirName, "\\scripts");
  259. /* Place the terminating null character in the szFile. */
  260. szFile[0] = '\0';
  261. /* Set the members of the OPENFILENAME structure. */
  262. ofn.lStructSize = sizeof(OPENFILENAME);
  263. ofn.hwndOwner = g_qeglobals.d_hwndCamera;
  264. ofn.lpstrFilter = szProjectFilter;
  265. ofn.nFilterIndex = 1;
  266. ofn.lpstrFile = szFile;
  267. ofn.nMaxFile = sizeof(szFile);
  268. ofn.lpstrFileTitle = szFileTitle;
  269. ofn.nMaxFileTitle = sizeof(szFileTitle);
  270. ofn.lpstrInitialDir = szDirName;
  271. ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  272. OFN_FILEMUSTEXIST;
  273. /* Display the Open dialog box. */
  274. if (!GetOpenFileName(&ofn))
  275. return; // canceled
  276. // Refresh the File menu.
  277. PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
  278. ID_FILE_EXIT);
  279. /* Open the file. */
  280. if (!QE_LoadProject(ofn.lpstrFile))
  281. Error ("Couldn't load project file");
  282. }
  283. void SaveAsDialog (void)
  284. {
  285. strcpy (szDirName, ValueForKey (g_qeglobals.d_project_entity, "basepath") );
  286. strcat (szDirName, "\\maps");
  287. /* Place the terminating null character in the szFile. */
  288. szFile[0] = '\0';
  289. /* Set the members of the OPENFILENAME structure. */
  290. ofn.lStructSize = sizeof(OPENFILENAME);
  291. ofn.hwndOwner = g_qeglobals.d_hwndCamera;
  292. ofn.lpstrFilter = szFilter;
  293. ofn.nFilterIndex = 1;
  294. ofn.lpstrFile = szFile;
  295. ofn.nMaxFile = sizeof(szFile);
  296. ofn.lpstrFileTitle = szFileTitle;
  297. ofn.nMaxFileTitle = sizeof(szFileTitle);
  298. ofn.lpstrInitialDir = szDirName;
  299. ofn.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST |
  300. OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
  301. /* Display the Open dialog box. */
  302. if (!GetSaveFileName(&ofn))
  303. return; // canceled
  304. DefaultExtension (ofn.lpstrFile, ".map");
  305. strcpy (currentmap, ofn.lpstrFile);
  306. // Add the file in MRU.
  307. AddNewItem(g_qeglobals.d_lpMruMenu, ofn.lpstrFile);
  308. // Refresh the File menu.
  309. PlaceMenuMRUItem(g_qeglobals.d_lpMruMenu,GetSubMenu(GetMenu(g_qeglobals.d_hwndMain),0),
  310. ID_FILE_EXIT);
  311. Map_SaveFile (ofn.lpstrFile, false); // ignore region
  312. }
  313. /*
  314. =======================================================
  315. Menu modifications
  316. =======================================================
  317. */
  318. /*
  319. ==================
  320. FillBSPMenu
  321. ==================
  322. */
  323. char *bsp_commands[256];
  324. void FillBSPMenu (void)
  325. {
  326. HMENU hmenu;
  327. epair_t *ep;
  328. int i;
  329. static int count;
  330. hmenu = GetSubMenu (GetMenu(g_qeglobals.d_hwndMain), MENU_BSP);
  331. for (i=0 ; i<count ; i++)
  332. DeleteMenu (hmenu, CMD_BSPCOMMAND+i, MF_BYCOMMAND);
  333. count = 0;
  334. i = 0;
  335. for (ep = g_qeglobals.d_project_entity->epairs ; ep ; ep=ep->next)
  336. {
  337. if (ep->key[0] == 'b' && ep->key[1] == 's' && ep->key[2] == 'p')
  338. {
  339. bsp_commands[i] = ep->key;
  340. AppendMenu (hmenu, MF_ENABLED|MF_STRING,
  341. CMD_BSPCOMMAND+i, (LPCTSTR)ep->key);
  342. i++;
  343. }
  344. }
  345. count = i;
  346. }
  347. //==============================================
  348. /*
  349. ===============
  350. CheckBspProcess
  351. See if the BSP is done yet
  352. ===============
  353. */
  354. void CheckBspProcess (void)
  355. {
  356. char outputpath[1024];
  357. char temppath[512];
  358. DWORD exitcode;
  359. char *out;
  360. BOOL ret;
  361. if (!bsp_process)
  362. return;
  363. ret = GetExitCodeProcess (bsp_process, &exitcode);
  364. if (!ret)
  365. Error ("GetExitCodeProcess failed");
  366. if (exitcode == STILL_ACTIVE)
  367. return;
  368. bsp_process = 0;
  369. GetTempPath(512, temppath);
  370. sprintf (outputpath, "%sjunk.txt", temppath);
  371. LoadFile (outputpath, (void *)&out);
  372. Sys_Printf ("%s", out);
  373. Sys_Printf ("\ncompleted.\n");
  374. free (out);
  375. Sys_Beep ();
  376. Pointfile_Check ();
  377. }
  378. extern int cambuttonstate;
  379. /*
  380. ==================
  381. WinMain
  382. ==================
  383. */
  384. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance
  385. ,LPSTR lpCmdLine, int nCmdShow)
  386. {
  387. MSG msg;
  388. double time, oldtime, delta;
  389. HACCEL accelerators;
  390. g_qeglobals.d_hInstance = hInstance;
  391. InitCommonControls ();
  392. screen_width = GetSystemMetrics (SM_CXFULLSCREEN);
  393. screen_height = GetSystemMetrics (SM_CYFULLSCREEN);
  394. // hack for broken NT 4.0 dual screen
  395. if (screen_width > 2*screen_height)
  396. screen_width /= 2;
  397. accelerators = LoadAccelerators (hInstance
  398. , MAKEINTRESOURCE(IDR_ACCELERATOR1));
  399. if (!accelerators)
  400. Error ("LoadAccelerators failed");
  401. Main_Create (hInstance);
  402. WCam_Create (hInstance);
  403. WXY_Create (hInstance);
  404. WZ_Create (hInstance);
  405. CreateEntityWindow(hInstance);
  406. // the project file can be specified on the command line,
  407. // or implicitly found in the scripts directory
  408. if (lpCmdLine && strlen(lpCmdLine))
  409. {
  410. ParseCommandLine (lpCmdLine);
  411. if (!QE_LoadProject(argv[1]))
  412. Error ("Couldn't load %s project file", argv[1]);
  413. }
  414. else if (!QE_LoadProject("scripts/quake.qe4"))
  415. Error ("Couldn't load scripts/quake.qe4 project file");
  416. QE_Init ();
  417. Sys_Printf ("Entering message loop\n");
  418. oldtime = Sys_DoubleTime ();
  419. while (!have_quit)
  420. {
  421. Sys_EndWait (); // remove wait cursor if active
  422. while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  423. {
  424. if (!TranslateAccelerator(g_qeglobals.d_hwndMain, accelerators, &msg) )
  425. {
  426. TranslateMessage (&msg);
  427. DispatchMessage (&msg);
  428. }
  429. if (msg.message == WM_QUIT)
  430. have_quit = true;
  431. }
  432. CheckBspProcess ();
  433. time = Sys_DoubleTime ();
  434. delta = time - oldtime;
  435. oldtime = time;
  436. if (delta > 0.2)
  437. delta = 0.2;
  438. // run time dependant behavior
  439. Cam_MouseControl (delta);
  440. // update any windows now
  441. if (update_bits & W_CAMERA)
  442. {
  443. InvalidateRect(g_qeglobals.d_hwndCamera, NULL, false);
  444. UpdateWindow (g_qeglobals.d_hwndCamera);
  445. }
  446. if (update_bits & (W_Z | W_Z_OVERLAY) )
  447. {
  448. InvalidateRect(g_qeglobals.d_hwndZ, NULL, false);
  449. UpdateWindow (g_qeglobals.d_hwndZ);
  450. }
  451. if ( update_bits & W_TEXTURE )
  452. {
  453. InvalidateRect(g_qeglobals.d_hwndTexture, NULL, false);
  454. UpdateWindow (g_qeglobals.d_hwndEntity);
  455. }
  456. if (update_bits & (W_XY | W_XY_OVERLAY))
  457. {
  458. InvalidateRect(g_qeglobals.d_hwndXY, NULL, false);
  459. UpdateWindow (g_qeglobals.d_hwndXY);
  460. }
  461. update_bits = 0;
  462. if (!cambuttonstate && !have_quit)
  463. { // if not driving in the camera view, block
  464. WaitMessage ();
  465. }
  466. }
  467. /* return success of application */
  468. return TRUE;
  469. }