]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/extra/qe4/win_xy.c
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / tools / quake2 / extra / qe4 / win_xy.c
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22
23 // win_xy.c -- windows specific xy view code
24
25 #include "qe3.h"
26
27 static HDC   s_hdcXY;
28 static HGLRC s_hglrcXY;
29
30 static unsigned s_stipple[32] =
31 {
32         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
33         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
34         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
35         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
36         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
37         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
38         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
39         0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
40 };
41
42 /*
43 ============
44 WXY_WndProc
45 ============
46 */
47 LONG WINAPI WXY_WndProc (
48     HWND    hWnd,
49     UINT    uMsg,
50     WPARAM  wParam,
51     LPARAM  lParam)
52 {
53         int             fwKeys, xPos, yPos;
54     RECT        rect;
55
56
57     GetClientRect(hWnd, &rect);
58
59     switch (uMsg)
60     {
61         case WM_CREATE:
62
63         s_hdcXY = GetDC(hWnd);
64
65                 QEW_SetupPixelFormat(s_hdcXY, false);
66
67                 if ( ( s_hglrcXY = wglCreateContext( s_hdcXY ) ) == 0 )
68                         Error( "wglCreateContext in WXY_WndProc failed" );
69
70         if (!wglMakeCurrent( s_hdcXY, s_hglrcXY ))
71                         Error ("wglMakeCurrent failed");
72
73                 if (!wglShareLists( g_qeglobals.d_hglrcBase, s_hglrcXY ) )
74                         Error( "wglShareLists in WXY_WndProc failed" );
75
76                 glPolygonStipple ((char *)s_stipple);
77                 glLineStipple (3, 0xaaaa);
78
79                 return 0;
80
81         case WM_DESTROY:
82                 QEW_StopGL( hWnd, s_hglrcXY, s_hdcXY );
83                 return 0;
84
85         case WM_PAINT:
86         {
87                     PAINTSTRUCT ps;
88
89                     BeginPaint(hWnd, &ps);
90
91             if (!wglMakeCurrent( s_hdcXY, s_hglrcXY ))
92                                 Error ("wglMakeCurrent failed");
93
94                         QE_CheckOpenGLForErrors();
95                         XY_Draw ();
96                         QE_CheckOpenGLForErrors();
97
98                         SwapBuffers(s_hdcXY);
99
100                         EndPaint(hWnd, &ps);
101         }
102                 return 0;
103
104         case WM_KEYDOWN:
105                 return QE_KeyDown (wParam);
106
107         case WM_MBUTTONDOWN:
108         case WM_RBUTTONDOWN:
109         case WM_LBUTTONDOWN:
110                 if ( GetTopWindow( g_qeglobals.d_hwndMain ) != hWnd)
111                         BringWindowToTop(hWnd);
112                 SetFocus( g_qeglobals.d_hwndXY );
113                 SetCapture( g_qeglobals.d_hwndXY );
114                 fwKeys = wParam;        // key flags
115                 xPos = (short)LOWORD(lParam);  // horizontal position of cursor
116                 yPos = (short)HIWORD(lParam);  // vertical position of cursor
117                 yPos = (int)rect.bottom - 1 - yPos;
118                 XY_MouseDown (xPos, yPos, fwKeys);
119                 return 0;
120
121         case WM_MBUTTONUP:
122         case WM_RBUTTONUP:
123         case WM_LBUTTONUP:
124                 fwKeys = wParam;        // key flags
125                 xPos = (short)LOWORD(lParam);  // horizontal position of cursor
126                 yPos = (short)HIWORD(lParam);  // vertical position of cursor
127                 yPos = (int)rect.bottom - 1 - yPos;
128                 XY_MouseUp (xPos, yPos, fwKeys);
129                 if (! (fwKeys & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
130                         ReleaseCapture ();
131                 return 0;
132
133         case WM_MOUSEMOVE:
134                 fwKeys = wParam;        // key flags
135                 xPos = (short)LOWORD(lParam);  // horizontal position of cursor
136                 yPos = (short)HIWORD(lParam);  // vertical position of cursor
137                 yPos = (int)rect.bottom - 1 - yPos;
138                 XY_MouseMoved (xPos, yPos, fwKeys);
139                 return 0;
140
141     case WM_SIZE:
142                 g_qeglobals.d_xy.width = rect.right;
143                 g_qeglobals.d_xy.height = rect.bottom;
144                 InvalidateRect( g_qeglobals.d_hwndXY, NULL, false);
145                 return 0;
146
147         case WM_NCCALCSIZE:// don't let windows copy pixels
148                 DefWindowProc (hWnd, uMsg, wParam, lParam);
149                 return WVR_REDRAW;
150
151         case WM_KILLFOCUS:
152         case WM_SETFOCUS:
153                 SendMessage( hWnd, WM_NCACTIVATE, uMsg == WM_SETFOCUS, 0 );
154                 return 0;
155
156         case WM_CLOSE:
157         DestroyWindow (hWnd);
158                 return 0;
159     }
160
161         return DefWindowProc (hWnd, uMsg, wParam, lParam);
162 }
163
164
165 /*
166 ==============
167 WXY_Create
168 ==============
169 */
170 void WXY_Create (HINSTANCE hInstance)
171 {
172     WNDCLASS   wc;
173
174     /* Register the camera class */
175         memset (&wc, 0, sizeof(wc));
176
177     wc.style         = 0;
178     wc.lpfnWndProc   = (WNDPROC)WXY_WndProc;
179     wc.cbClsExtra    = 0;
180     wc.cbWndExtra    = 0;
181     wc.hInstance     = hInstance;
182     wc.hIcon         = 0;
183     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
184     wc.hbrBackground = NULL; //(HBRUSH)(COLOR_WINDOW+1);
185     wc.lpszMenuName  = NULL;
186     wc.lpszClassName = XY_WINDOW_CLASS;
187
188     if (!RegisterClass (&wc) )
189         Error ("RegisterClass: failed");
190
191         g_qeglobals.d_hwndXY = CreateWindow (XY_WINDOW_CLASS ,
192                 "XY View",
193                 QE3_STYLE ,
194                 ZWIN_WIDTH,
195                 (int)(screen_height*CWIN_SIZE)-20,
196                 screen_width-ZWIN_WIDTH,
197                 (int)(screen_height*(1.0-CWIN_SIZE)-38),        // size
198
199                 g_qeglobals.d_hwndMain, // parent
200                 0,              // no menu
201                 hInstance,
202                 NULL);
203
204         if (!g_qeglobals.d_hwndXY )
205                 Error ("Couldn't create XY View");
206
207         LoadWindowState(g_qeglobals.d_hwndXY, "xywindow");
208     ShowWindow(g_qeglobals.d_hwndXY, SW_SHOWDEFAULT);
209 }
210
211 static void WXY_InitPixelFormat( PIXELFORMATDESCRIPTOR *pPFD )
212 {
213         memset( pPFD, 0, sizeof( *pPFD ) );
214
215         pPFD->nSize    = sizeof( PIXELFORMATDESCRIPTOR );
216         pPFD->nVersion = 1;
217         pPFD->dwFlags  = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
218         pPFD->iPixelType = PFD_TYPE_RGBA;
219         pPFD->cColorBits = 24;
220         pPFD->cDepthBits = 32;
221         pPFD->iLayerType = PFD_MAIN_PLANE;
222
223 }
224
225 void WXY_Print( void )
226 {
227         DOCINFO di;
228
229         PRINTDLG pd;
230
231         /*
232         ** initialize the PRINTDLG struct and execute it
233         */
234         memset( &pd, 0, sizeof( pd ) );
235         pd.lStructSize = sizeof( pd );
236         pd.hwndOwner = g_qeglobals.d_hwndXY;
237         pd.Flags = PD_RETURNDC;
238         pd.hInstance = 0;
239         if ( !PrintDlg( &pd ) || !pd.hDC )
240         {
241                 MessageBox( g_qeglobals.d_hwndMain, "Could not PrintDlg()", "QE4 Print Error", MB_OK | MB_ICONERROR );
242                 return;
243         }
244
245         /*
246         ** StartDoc
247         */
248         memset( &di, 0, sizeof( di ) );
249         di.cbSize = sizeof( di );
250         di.lpszDocName = "QE4";
251         if ( StartDoc( pd.hDC, &di ) <= 0 )
252         {
253                 MessageBox( g_qeglobals.d_hwndMain, "Could not StartDoc()", "QE4 Print Error", MB_OK | MB_ICONERROR );
254                 return;
255         }
256
257         /*
258         ** StartPage
259         */
260         if ( StartPage( pd.hDC ) <= 0 )
261         {
262                 MessageBox( g_qeglobals.d_hwndMain, "Could not StartPage()", "QE4 Print Error", MB_OK | MB_ICONERROR );
263                 return;
264         }
265
266         /*
267         ** read pixels from the XY window
268         */
269         {
270                 int bmwidth = 320, bmheight = 320;
271                 int pwidth, pheight;
272
273                 RECT r;
274
275                 GetWindowRect( g_qeglobals.d_hwndXY, &r );
276
277                 bmwidth  = r.right - r.left;
278                 bmheight = r.bottom - r.top;
279
280                 pwidth  = GetDeviceCaps( pd.hDC, PHYSICALWIDTH ) - GetDeviceCaps( pd.hDC, PHYSICALOFFSETX );
281                 pheight = GetDeviceCaps( pd.hDC, PHYSICALHEIGHT ) - GetDeviceCaps( pd.hDC, PHYSICALOFFSETY );
282
283                 StretchBlt( pd.hDC,
284                         0, 0,
285                         pwidth, pheight,
286                         s_hdcXY,
287                         0, 0,
288                         bmwidth, bmheight,
289                         SRCCOPY );
290         }
291
292         /*
293         ** EndPage and EndDoc
294         */
295         if ( EndPage( pd.hDC ) <= 0 )
296         {
297                 MessageBox( g_qeglobals.d_hwndMain, "QE4 Print Error", "Could not EndPage()", MB_OK | MB_ICONERROR );
298                 return;
299         }
300
301         if ( EndDoc( pd.hDC ) <= 0 )
302         {
303                 MessageBox( g_qeglobals.d_hwndMain, "QE4 Print Error", "Could not EndDoc()", MB_OK | MB_ICONERROR );
304                 return;
305         }
306 }