demos: add missing binaries to .gitignore
[mesa-demos.git] / src / wgl / wincopy.c
blobe802aeef4669b1f7a2dd771bd761ff17b8c8b3d6
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * This program opens two windows, renders into one and uses
28 * glCopyPixels to copy the image from the first window into the
29 * second by means of the extension function wglMakeContextCurrentARB().
32 * Author: kenc
34 * Created: March 1, 2013
35 * Copyright (c) 2013, Attachmate Corporation All Rights Reserved
37 #include <windows.h>
38 #include <GL/gl.h>
39 #include <assert.h>
40 #include "wglutil.h"
42 static HGLRC Context = NULL;
43 static HWND Win[2] = {NULL, NULL};
44 static int Width[2] = {0, 0}, Height[2] = {0, 0};
45 static float Angle = 0.0f;
48 static BOOL
49 Redraw(BOOL DrawFront)
51 assert(Context != NULL);
52 if (!wglExtMakeContextCurrent(Win[0], Win[0], Context))
53 return FALSE;
55 Angle += 1.0f;
56 if (DrawFront) {
57 glDrawBuffer(GL_FRONT);
58 glReadBuffer(GL_FRONT);
60 else {
61 glDrawBuffer(GL_BACK);
62 glReadBuffer(GL_BACK);
65 glViewport(0, 0, Width[0], Height[0]);
66 glMatrixMode(GL_PROJECTION);
67 glLoadIdentity();
68 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
69 glMatrixMode(GL_MODELVIEW);
70 glShadeModel(GL_FLAT);
71 glClearColor(0.5, 0.5, 0.5, 0.0);
72 glClear(GL_COLOR_BUFFER_BIT);
74 /* draw blue quad */
75 glColor3f(0.3f, 0.3f, 1.0f);
76 glPushMatrix();
77 glRotatef(Angle, 0, 0, 1);
78 glBegin(GL_POLYGON);
79 glVertex2f(-0.5, -0.25);
80 glVertex2f(0.5, -0.25);
81 glVertex2f(0.5, 0.25);
82 glVertex2f(-0.5, 0.25);
83 glEnd();
84 glPopMatrix();
86 if (DrawFront)
87 glFinish();
88 else
89 wglExtSwapBuffers(Win[0]);
91 /* copy image from window 0 to window 1 */
92 if (!wglExtMakeContextCurrent(Win[1], Win[0], Context))
93 return FALSE;
95 /* copy the image between windows */
96 glClearColor(0.0, 0.0, 0.0, 0.0);
97 glClear(GL_COLOR_BUFFER_BIT);
98 glCopyPixels(0, 0, Width[0], Height[0], GL_COLOR);
99 if (DrawFront)
100 glFinish();
101 else
102 wglExtSwapBuffers(Win[1]);
103 return TRUE;
107 static void
108 Resize(HWND win, int width, int height)
110 int i;
111 HDC hDC;
113 if (win == Win[0])
114 i = 0;
115 else
116 i = 1;
118 if ((hDC = (HDC)(INT_PTR)GetWindowLongPtr(win, GWLP_USERDATA)) != NULL) {
119 BOOL ret;
121 if ((ret = wglMakeCurrent(hDC, Context)) != FALSE) {
122 Width[i] = width;
123 Height[i] = height;
125 assert(ret != FALSE);
129 static LRESULT CALLBACK
130 EventLoop(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
132 HDC hDC;
133 RECT rect;
134 LRESULT ret;
135 LPWINDOWPOS pWinPos;
136 static BOOL drawFront = FALSE;
138 switch (msg) {
139 case WM_CREATE:
140 ret = (LRESULT)storeDC(hWnd);
141 assert(ret == 0);
142 break;
144 case WM_PAINT:
145 GetUpdateRect(hWnd, &rect, FALSE);
146 Redraw(drawFront);
147 ret = 0;
148 break;
150 case WM_WINDOWPOSCHANGED:
151 pWinPos = (LPWINDOWPOS)lParam;
152 //if (!(pWinPos->flags&SWP_NOSIZE))
153 Resize(hWnd, pWinPos->cx, pWinPos->cy);
154 ret = 0;
155 break;
157 case WM_CHAR:
158 if (wParam == 'f') { // 'f' key
159 drawFront = !drawFront;
160 Redraw(drawFront);
162 else if (wParam == VK_ESCAPE)
163 PostQuitMessage(0);
164 ret = 0;
165 break;
167 case WM_DESTROY:
168 if ((hDC = fetchDC(hWnd)) != NULL) {
169 ret = ReleaseDC(hWnd, hDC);
170 assert(ret != FALSE);
172 ret = 1;
173 break;
175 default:
176 ret = DefWindowProc(hWnd, msg, wParam, lParam);
177 break;
179 return ret;
183 static BOOL
184 Init(HINSTANCE hInst, int show)
186 BOOL rval = FALSE;
188 if (wglExtInit(hInst, EventLoop) != FALSE) {
189 const int attribList[] = {
190 WGL_DRAW_TO_WINDOW, GL_TRUE,
191 WGL_SUPPORT_OPENGL, GL_TRUE,
192 WGL_DOUBLE_BUFFER, GL_TRUE,
193 WGL_PIXEL_TYPE, WGL_TYPE_RGBA,
194 WGL_COLOR_BITS, 32,
195 WGL_DEPTH_BITS, 24,
197 int format;
199 if ((format = wglExtChoosePixelFormat(attribList)) != 0) {
200 if ((Win[0] = getWindow(hInst, 0, 0, 300, 300, format)) != NULL) {
201 if ((Win[1] = getWindow(hInst, 350, 0, 300, 300, format)) != NULL) {
202 ShowWindow(Win[0], show);
203 ShowWindow(Win[1], show);
204 rval = TRUE;
209 return rval;
213 int CALLBACK
214 WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
216 int ret = -1;
218 if (Init(hInst, nCmdShow) != FALSE) {
219 MSG msg;
220 HDC hDC;
222 if ((hDC = fetchDC(Win[0])) != NULL) {
223 if ((Context = wglCreateContext(hDC)) != NULL) {
224 BOOL rval;
226 while ((ret = GetMessage(&msg, NULL, 0, 0)) != 0) {
227 TranslateMessage(&msg);
228 DispatchMessage(&msg);
230 if (Win[0] != NULL) {
231 rval = DestroyWindow(Win[0]);
232 assert(rval != FALSE);
234 if (Win[1] != NULL) {
235 rval = DestroyWindow(Win[1]);
236 assert(rval != FALSE);
238 rval = wglDeleteContext(Context);
239 assert(rval != FALSE);
242 wglExtDispose(hInst);
243 ret = (int)msg.wParam;
245 if (Context != NULL)
246 wglDeleteContext(Context);
247 return ret;