win_cam.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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_cam.c -- windows specific camera view code
  19. #include "qe3.h"
  20. /*
  21. ============
  22. CameraWndProc
  23. ============
  24. */
  25. LONG WINAPI WCam_WndProc (
  26. HWND hWnd,
  27. UINT uMsg,
  28. WPARAM wParam,
  29. LPARAM lParam)
  30. {
  31. int fwKeys, xPos, yPos;
  32. RECT rect;
  33. GetClientRect(hWnd, &rect);
  34. switch (uMsg)
  35. {
  36. case WM_CREATE:
  37. {
  38. HFONT hfont;
  39. g_qeglobals.d_hdcBase = GetDC(hWnd);
  40. QEW_SetupPixelFormat(g_qeglobals.d_hdcBase, true);
  41. if ( ( g_qeglobals.d_hglrcBase = wglCreateContext( g_qeglobals.d_hdcBase ) ) == 0 )
  42. Error ("wglCreateContext failed");
  43. if (!wglMakeCurrent( g_qeglobals.d_hdcBase, g_qeglobals.d_hglrcBase ))
  44. Error ("wglMakeCurrent failed");
  45. Texture_SetMode(g_qeglobals.d_savedinfo.iTexMenu);
  46. //
  47. // create GL font
  48. //
  49. hfont = CreateFont(
  50. 10, // logical height of font
  51. 7, // logical average character width
  52. 0, // angle of escapement
  53. 0, // base-line orientation angle
  54. 0, // font weight
  55. 0, // italic attribute flag
  56. 0, // underline attribute flag
  57. 0, // strikeout attribute flag
  58. 0, // character set identifier
  59. 0, // output precision
  60. 0, // clipping precision
  61. 0, // output quality
  62. 0, // pitch and family
  63. 0 // pointer to typeface name string
  64. );
  65. if ( !hfont )
  66. Error( "couldn't create font" );
  67. SelectObject (g_qeglobals.d_hdcBase, hfont);
  68. if ( ( g_qeglobals.d_font_list = glGenLists (256) ) == 0 )
  69. Error( "couldn't create font dlists" );
  70. // create the bitmap display lists
  71. // we're making images of glyphs 0 thru 255
  72. if ( !wglUseFontBitmaps (g_qeglobals.d_hdcBase, 1, 255, g_qeglobals.d_font_list) )
  73. Error( "wglUseFontBitmaps faileD" );
  74. // indicate start of glyph display lists
  75. glListBase (g_qeglobals.d_font_list);
  76. // report OpenGL information
  77. Sys_Printf ("GL_VENDOR: %s\n", glGetString (GL_VENDOR));
  78. Sys_Printf ("GL_RENDERER: %s\n", glGetString (GL_RENDERER));
  79. Sys_Printf ("GL_VERSION: %s\n", glGetString (GL_VERSION));
  80. Sys_Printf ("GL_EXTENSIONS: %s\n", glGetString (GL_EXTENSIONS));
  81. }
  82. return 0;
  83. case WM_PAINT:
  84. {
  85. PAINTSTRUCT ps;
  86. if (!wglMakeCurrent( g_qeglobals.d_hdcBase, g_qeglobals.d_hglrcBase ))
  87. Error ("wglMakeCurrent failed");
  88. if ( BeginPaint(hWnd, &ps) )
  89. {
  90. QE_CheckOpenGLForErrors();
  91. Cam_Draw ();
  92. QE_CheckOpenGLForErrors();
  93. EndPaint(hWnd, &ps);
  94. SwapBuffers(g_qeglobals.d_hdcBase);
  95. }
  96. }
  97. return 0;
  98. case WM_USER+267: // benchmark
  99. {
  100. PAINTSTRUCT ps;
  101. WINDOWPLACEMENT wp;
  102. double start, end;
  103. int i;
  104. memset( &wp, 0, sizeof( wp ) );
  105. wp.length = sizeof( wp );
  106. GetWindowPlacement( g_qeglobals.d_hwndCamera, &wp );
  107. MoveWindow( g_qeglobals.d_hwndCamera, 30, 30, 400, 400, TRUE );
  108. BeginPaint(hWnd, &ps);
  109. if (!wglMakeCurrent( g_qeglobals.d_hdcBase, g_qeglobals.d_hglrcBase))
  110. Error ("wglMakeCurrent failed");
  111. glDrawBuffer (GL_FRONT);
  112. start = Sys_DoubleTime ();
  113. for (i=0 ; i<100 ; i++)
  114. {
  115. camera.angles[YAW] = i*4;
  116. Cam_Draw ();
  117. }
  118. SwapBuffers(g_qeglobals.d_hdcBase);
  119. glDrawBuffer (GL_BACK);
  120. end = Sys_DoubleTime ();
  121. EndPaint(hWnd, &ps);
  122. Sys_Printf ("%5.2f seconds\n", end-start);
  123. SetWindowPlacement( g_qeglobals.d_hwndCamera, &wp );
  124. }
  125. break;
  126. case WM_KEYDOWN:
  127. if ( QE_KeyDown (wParam) )
  128. return 0;
  129. else
  130. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  131. case WM_MBUTTONDOWN:
  132. case WM_RBUTTONDOWN:
  133. case WM_LBUTTONDOWN:
  134. if (GetTopWindow(g_qeglobals.d_hwndMain) != hWnd)
  135. BringWindowToTop(hWnd);
  136. SetFocus (g_qeglobals.d_hwndCamera);
  137. SetCapture (g_qeglobals.d_hwndCamera);
  138. fwKeys = wParam; // key flags
  139. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  140. yPos = (short)HIWORD(lParam); // vertical position of cursor
  141. yPos = (int)rect.bottom - 1 - yPos;
  142. Cam_MouseDown (xPos, yPos, fwKeys);
  143. return 0;
  144. case WM_MBUTTONUP:
  145. case WM_RBUTTONUP:
  146. case WM_LBUTTONUP:
  147. fwKeys = wParam; // key flags
  148. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  149. yPos = (short)HIWORD(lParam); // vertical position of cursor
  150. yPos = (int)rect.bottom - 1 - yPos;
  151. Cam_MouseUp (xPos, yPos, fwKeys);
  152. if (! (fwKeys & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
  153. ReleaseCapture ();
  154. return 0;
  155. case WM_MOUSEMOVE:
  156. fwKeys = wParam; // key flags
  157. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  158. yPos = (short)HIWORD(lParam); // vertical position of cursor
  159. yPos = (int)rect.bottom - 1 - yPos;
  160. Cam_MouseMoved (xPos, yPos, fwKeys);
  161. return 0;
  162. case WM_SIZE:
  163. camera.width = rect.right;
  164. camera.height = rect.bottom;
  165. InvalidateRect(g_qeglobals.d_hwndCamera, NULL, false);
  166. return 0;
  167. case WM_KILLFOCUS:
  168. case WM_SETFOCUS:
  169. SendMessage( hWnd, WM_NCACTIVATE, uMsg == WM_SETFOCUS, 0 );
  170. return 0;
  171. case WM_NCCALCSIZE:// don't let windows copy pixels
  172. DefWindowProc (hWnd, uMsg, wParam, lParam);
  173. return WVR_REDRAW;
  174. case WM_CLOSE:
  175. DestroyWindow (hWnd);
  176. return 0;
  177. case WM_DESTROY:
  178. QEW_StopGL( hWnd, g_qeglobals.d_hglrcBase, g_qeglobals.d_hdcBase );
  179. return 0;
  180. }
  181. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  182. }
  183. /*
  184. ==============
  185. WCam_Create
  186. ==============
  187. */
  188. void WCam_Create (HINSTANCE hInstance)
  189. {
  190. WNDCLASS wc;
  191. char *title;
  192. /* Register the camera class */
  193. memset (&wc, 0, sizeof(wc));
  194. wc.style = 0;
  195. wc.lpfnWndProc = (WNDPROC)WCam_WndProc;
  196. wc.cbClsExtra = 0;
  197. wc.cbWndExtra = 0;
  198. wc.hInstance = hInstance;
  199. wc.hIcon = 0;
  200. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  201. wc.hbrBackground = NULL;
  202. wc.lpszMenuName = 0;
  203. wc.lpszClassName = CAMERA_WINDOW_CLASS;
  204. if (!RegisterClass (&wc) )
  205. Error ("WCam_Register: failed");
  206. if ( g_qeglobals.d_savedinfo.exclude & EXCLUDE_DETAIL )
  207. title = "Camera View (DETAIL EXCLUDED)";
  208. else
  209. title = "Camera View";
  210. g_qeglobals.d_hwndCamera = CreateWindow (CAMERA_WINDOW_CLASS ,
  211. title,
  212. QE3_STYLE,
  213. ZWIN_WIDTH,
  214. 20,
  215. (int)(screen_width*CWIN_SIZE),
  216. (int)(screen_height*CWIN_SIZE), // size
  217. g_qeglobals.d_hwndMain, // parent window
  218. 0, // no menu
  219. hInstance,
  220. 0);
  221. if (!g_qeglobals.d_hwndCamera)
  222. Error ("Couldn't create g_qeglobals.d_hwndCamera");
  223. LoadWindowState(g_qeglobals.d_hwndCamera, "camerawindow");
  224. ShowWindow (g_qeglobals.d_hwndCamera, SW_SHOWDEFAULT);
  225. }