win_xy.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // win_xy.c -- windows specific xy view code
  19. #include "qe3.h"
  20. static HDC s_hdcXY;
  21. static HGLRC s_hglrcXY;
  22. static unsigned s_stipple[32] =
  23. {
  24. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  25. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  26. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  27. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  28. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  29. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  30. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  31. 0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  32. };
  33. /*
  34. ============
  35. WXY_WndProc
  36. ============
  37. */
  38. LONG WINAPI WXY_WndProc (
  39. HWND hWnd,
  40. UINT uMsg,
  41. WPARAM wParam,
  42. LPARAM lParam)
  43. {
  44. int fwKeys, xPos, yPos;
  45. RECT rect;
  46. GetClientRect(hWnd, &rect);
  47. switch (uMsg)
  48. {
  49. case WM_CREATE:
  50. s_hdcXY = GetDC(hWnd);
  51. QEW_SetupPixelFormat(s_hdcXY, false);
  52. if ( ( s_hglrcXY = wglCreateContext( s_hdcXY ) ) == 0 )
  53. Error( "wglCreateContext in WXY_WndProc failed" );
  54. if (!wglMakeCurrent( s_hdcXY, s_hglrcXY ))
  55. Error ("wglMakeCurrent failed");
  56. if (!wglShareLists( g_qeglobals.d_hglrcBase, s_hglrcXY ) )
  57. Error( "wglShareLists in WXY_WndProc failed" );
  58. glPolygonStipple ((char *)s_stipple);
  59. glLineStipple (3, 0xaaaa);
  60. return 0;
  61. case WM_DESTROY:
  62. QEW_StopGL( hWnd, s_hglrcXY, s_hdcXY );
  63. return 0;
  64. case WM_PAINT:
  65. {
  66. PAINTSTRUCT ps;
  67. BeginPaint(hWnd, &ps);
  68. if (!wglMakeCurrent( s_hdcXY, s_hglrcXY ))
  69. Error ("wglMakeCurrent failed");
  70. QE_CheckOpenGLForErrors();
  71. XY_Draw ();
  72. QE_CheckOpenGLForErrors();
  73. SwapBuffers(s_hdcXY);
  74. EndPaint(hWnd, &ps);
  75. }
  76. return 0;
  77. case WM_KEYDOWN:
  78. return QE_KeyDown (wParam);
  79. case WM_MBUTTONDOWN:
  80. case WM_RBUTTONDOWN:
  81. case WM_LBUTTONDOWN:
  82. if ( GetTopWindow( g_qeglobals.d_hwndMain ) != hWnd)
  83. BringWindowToTop(hWnd);
  84. SetFocus( g_qeglobals.d_hwndXY );
  85. SetCapture( g_qeglobals.d_hwndXY );
  86. fwKeys = wParam; // key flags
  87. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  88. yPos = (short)HIWORD(lParam); // vertical position of cursor
  89. yPos = (int)rect.bottom - 1 - yPos;
  90. XY_MouseDown (xPos, yPos, fwKeys);
  91. return 0;
  92. case WM_MBUTTONUP:
  93. case WM_RBUTTONUP:
  94. case WM_LBUTTONUP:
  95. fwKeys = wParam; // key flags
  96. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  97. yPos = (short)HIWORD(lParam); // vertical position of cursor
  98. yPos = (int)rect.bottom - 1 - yPos;
  99. XY_MouseUp (xPos, yPos, fwKeys);
  100. if (! (fwKeys & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
  101. ReleaseCapture ();
  102. return 0;
  103. case WM_MOUSEMOVE:
  104. fwKeys = wParam; // key flags
  105. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  106. yPos = (short)HIWORD(lParam); // vertical position of cursor
  107. yPos = (int)rect.bottom - 1 - yPos;
  108. XY_MouseMoved (xPos, yPos, fwKeys);
  109. return 0;
  110. case WM_SIZE:
  111. g_qeglobals.d_xy.width = rect.right;
  112. g_qeglobals.d_xy.height = rect.bottom;
  113. InvalidateRect( g_qeglobals.d_hwndXY, NULL, false);
  114. return 0;
  115. case WM_NCCALCSIZE:// don't let windows copy pixels
  116. DefWindowProc (hWnd, uMsg, wParam, lParam);
  117. return WVR_REDRAW;
  118. case WM_KILLFOCUS:
  119. case WM_SETFOCUS:
  120. SendMessage( hWnd, WM_NCACTIVATE, uMsg == WM_SETFOCUS, 0 );
  121. return 0;
  122. case WM_CLOSE:
  123. DestroyWindow (hWnd);
  124. return 0;
  125. }
  126. return DefWindowProc (hWnd, uMsg, wParam, lParam);
  127. }
  128. /*
  129. ==============
  130. WXY_Create
  131. ==============
  132. */
  133. void WXY_Create (HINSTANCE hInstance)
  134. {
  135. WNDCLASS wc;
  136. /* Register the camera class */
  137. memset (&wc, 0, sizeof(wc));
  138. wc.style = 0;
  139. wc.lpfnWndProc = (WNDPROC)WXY_WndProc;
  140. wc.cbClsExtra = 0;
  141. wc.cbWndExtra = 0;
  142. wc.hInstance = hInstance;
  143. wc.hIcon = 0;
  144. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  145. wc.hbrBackground = NULL; //(HBRUSH)(COLOR_WINDOW+1);
  146. wc.lpszMenuName = NULL;
  147. wc.lpszClassName = XY_WINDOW_CLASS;
  148. if (!RegisterClass (&wc) )
  149. Error ("RegisterClass: failed");
  150. g_qeglobals.d_hwndXY = CreateWindow (XY_WINDOW_CLASS ,
  151. "XY View",
  152. QE3_STYLE ,
  153. ZWIN_WIDTH,
  154. (int)(screen_height*CWIN_SIZE)-20,
  155. screen_width-ZWIN_WIDTH,
  156. (int)(screen_height*(1.0-CWIN_SIZE)-38), // size
  157. g_qeglobals.d_hwndMain, // parent
  158. 0, // no menu
  159. hInstance,
  160. NULL);
  161. if (!g_qeglobals.d_hwndXY )
  162. Error ("Couldn't create XY View");
  163. LoadWindowState(g_qeglobals.d_hwndXY, "xywindow");
  164. ShowWindow(g_qeglobals.d_hwndXY, SW_SHOWDEFAULT);
  165. }
  166. static void WXY_InitPixelFormat( PIXELFORMATDESCRIPTOR *pPFD )
  167. {
  168. memset( pPFD, 0, sizeof( *pPFD ) );
  169. pPFD->nSize = sizeof( PIXELFORMATDESCRIPTOR );
  170. pPFD->nVersion = 1;
  171. pPFD->dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  172. pPFD->iPixelType = PFD_TYPE_RGBA;
  173. pPFD->cColorBits = 24;
  174. pPFD->cDepthBits = 32;
  175. pPFD->iLayerType = PFD_MAIN_PLANE;
  176. }
  177. void WXY_Print( void )
  178. {
  179. DOCINFO di;
  180. PRINTDLG pd;
  181. /*
  182. ** initialize the PRINTDLG struct and execute it
  183. */
  184. memset( &pd, 0, sizeof( pd ) );
  185. pd.lStructSize = sizeof( pd );
  186. pd.hwndOwner = g_qeglobals.d_hwndXY;
  187. pd.Flags = PD_RETURNDC;
  188. pd.hInstance = 0;
  189. if ( !PrintDlg( &pd ) || !pd.hDC )
  190. {
  191. MessageBox( g_qeglobals.d_hwndMain, "Could not PrintDlg()", "QE4 Print Error", MB_OK | MB_ICONERROR );
  192. return;
  193. }
  194. /*
  195. ** StartDoc
  196. */
  197. memset( &di, 0, sizeof( di ) );
  198. di.cbSize = sizeof( di );
  199. di.lpszDocName = "QE4";
  200. if ( StartDoc( pd.hDC, &di ) <= 0 )
  201. {
  202. MessageBox( g_qeglobals.d_hwndMain, "Could not StartDoc()", "QE4 Print Error", MB_OK | MB_ICONERROR );
  203. return;
  204. }
  205. /*
  206. ** StartPage
  207. */
  208. if ( StartPage( pd.hDC ) <= 0 )
  209. {
  210. MessageBox( g_qeglobals.d_hwndMain, "Could not StartPage()", "QE4 Print Error", MB_OK | MB_ICONERROR );
  211. return;
  212. }
  213. /*
  214. ** read pixels from the XY window
  215. */
  216. {
  217. int bmwidth = 320, bmheight = 320;
  218. int pwidth, pheight;
  219. RECT r;
  220. GetWindowRect( g_qeglobals.d_hwndXY, &r );
  221. bmwidth = r.right - r.left;
  222. bmheight = r.bottom - r.top;
  223. pwidth = GetDeviceCaps( pd.hDC, PHYSICALWIDTH ) - GetDeviceCaps( pd.hDC, PHYSICALOFFSETX );
  224. pheight = GetDeviceCaps( pd.hDC, PHYSICALHEIGHT ) - GetDeviceCaps( pd.hDC, PHYSICALOFFSETY );
  225. StretchBlt( pd.hDC,
  226. 0, 0,
  227. pwidth, pheight,
  228. s_hdcXY,
  229. 0, 0,
  230. bmwidth, bmheight,
  231. SRCCOPY );
  232. }
  233. /*
  234. ** EndPage and EndDoc
  235. */
  236. if ( EndPage( pd.hDC ) <= 0 )
  237. {
  238. MessageBox( g_qeglobals.d_hwndMain, "QE4 Print Error", "Could not EndPage()", MB_OK | MB_ICONERROR );
  239. return;
  240. }
  241. if ( EndDoc( pd.hDC ) <= 0 )
  242. {
  243. MessageBox( g_qeglobals.d_hwndMain, "QE4 Print Error", "Could not EndDoc()", MB_OK | MB_ICONERROR );
  244. return;
  245. }
  246. }