demos: add missing binaries to .gitignore
[mesa-demos.git] / src / wgl / wglutil.c
blob1cd943b24234a995bfe0ba0d6edeee0b9f3214c4
1 /*
2 * Author: kenc
4 * Created: March 1, 2013
5 * Copyright (c) 2013, Attachmate Corporation All Rights Reserved
6 */
7 #include <windows.h>
8 #include <assert.h>
9 #include <GL/gl.h>
10 #include "wglutil.h"
12 #define STYLE WS_OVERLAPPEDWINDOW
13 #define EXSTYLE WS_EX_OVERLAPPEDWINDOW
14 #define GL_CLASS "GL"
16 static PFNWGLMAKECONTEXTCURRENTARBPROC WGLMakeContextCurrent = NULL;
17 static PFNWGLCHOOSEPIXELFORMATARBPROC WGLChoosePixelFormat = NULL;
19 LRESULT
20 storeDC(HWND hWnd)
22 HDC hDC;
24 if ((hDC = GetDC(hWnd)) != NULL) {
25 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)hDC);
27 assert(GetWindowLongPtr(hWnd, GWLP_USERDATA) == (LONG_PTR)hDC);
28 return 0;
30 return -1;
33 HDC
34 fetchDC(HWND hWnd)
36 assert(hWnd != NULL);
37 if (hWnd != NULL) {
38 HDC hDC = (HDC)(INT_PTR)GetWindowLongPtr(hWnd, GWLP_USERDATA);
40 assert(hDC != NULL);
41 return hDC;
43 return NULL;
46 HWND
47 getWindow(HINSTANCE hInst, int x, int y, int w, int h, int format)
49 HWND hParent;
51 if ((hParent = GetDesktopWindow()) != NULL) {
52 HWND hWnd = CreateWindowEx(EXSTYLE, GL_CLASS, "wincopy", STYLE, x, y, w, h, hParent, NULL, hInst, NULL);
53 HDC hDC;
54 BOOL ret;
56 assert(hWnd != NULL);
57 if ((hDC = (HDC)(INT_PTR)GetWindowLongPtr(hWnd, GWLP_USERDATA)) != NULL) {
58 PIXELFORMATDESCRIPTOR pfd;
60 if ((ret = SetPixelFormat(hDC, format, &pfd)) != FALSE)
61 return hWnd;
62 ret = DestroyWindow(hWnd);
63 assert(ret != FALSE);
66 assert(hParent != NULL);
67 return NULL;
70 static int
71 getFormat(HDC hDC, PIXELFORMATDESCRIPTOR *pfd)
73 int format;
75 int size = sizeof(PIXELFORMATDESCRIPTOR);
76 memset(pfd, 0, size);
77 pfd->nSize = size;
78 pfd->nVersion = 1;
79 pfd->dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL;
80 pfd->iLayerType = PFD_MAIN_PLANE;
81 pfd->iPixelType = PFD_TYPE_RGBA;
82 format = ChoosePixelFormat(hDC, pfd);
83 assert(format > 0);
84 return format;
87 static HGLRC
88 createCurrentContext(HDC hDC)
90 HGLRC hGLRC = NULL;
92 if ((hGLRC = wglCreateContext(hDC)) != NULL) {
93 BOOL current;
95 if ((current = wglMakeCurrent(hDC, hGLRC)) == FALSE) {
96 BOOL ret = wglDeleteContext(hGLRC);
98 assert(ret != FALSE);
99 hGLRC = NULL;
102 else
103 assert(hGLRC != NULL);
104 return hGLRC;
107 static BOOL
108 checkWGLExtensions(HINSTANCE hInst)
110 HWND hWnd;
111 BOOL rval, ret = FALSE;
113 if ((hWnd = CreateWindowEx(EXSTYLE, GL_CLASS, "tmp", STYLE, 0, 0, 1, 1,
114 GetDesktopWindow(), NULL, hInst, NULL)) != NULL) {
115 HDC hDC;
117 if ((hDC = fetchDC(hWnd)) != NULL) {
118 PIXELFORMATDESCRIPTOR pfd;
119 int format;
121 if ((format = getFormat(hDC, &pfd)) > 0) {
122 BOOL setFormat;
124 if ((setFormat = SetPixelFormat(hDC, format, &pfd)) != FALSE) {
125 HGLRC hGLRC = createCurrentContext(hDC);
127 if (hGLRC != NULL) {
128 PFNWGLGETEXTENSIONSSTRINGPROC WGLGetExtensionsStringFunc;
130 WGLGetExtensionsStringFunc = (PFNWGLGETEXTENSIONSSTRINGPROC)wglGetProcAddress("wglGetExtensionsStringARB");
131 assert(WGLGetExtensionsStringFunc != NULL);
132 if (WGLGetExtensionsStringFunc != NULL) {
133 const char *wglExtensionString = (*WGLGetExtensionsStringFunc)(hDC);
135 if (wglExtensionString != NULL) {
136 WGLMakeContextCurrent = (PFNWGLMAKECONTEXTCURRENTARBPROC)wglGetProcAddress("wglMakeContextCurrentARB");
137 assert(WGLMakeContextCurrent != NULL);
138 WGLChoosePixelFormat = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
139 assert(WGLChoosePixelFormat != NULL);
140 if (WGLMakeContextCurrent != NULL && WGLChoosePixelFormat != NULL)
141 ret = TRUE;
144 rval = wglDeleteContext(hGLRC);
145 assert(rval != FALSE);
150 rval = DestroyWindow(hWnd);
151 assert(rval != FALSE);
153 return ret;
157 BOOL
158 wglExtInit(HINSTANCE hInst, WNDPROC proc)
160 WNDCLASS glClass;
161 ATOM atom;
163 memset(&glClass, 0, sizeof(WNDCLASS));
164 glClass.style = CS_PARENTDC;
165 glClass.lpfnWndProc = proc;
166 glClass.hInstance = hInst;
167 glClass.lpszClassName = GL_CLASS;
168 glClass.cbWndExtra = sizeof(HANDLE);
169 if ((atom = RegisterClass(&glClass)) == 0) {
170 assert(atom != 0);
171 return FALSE;
173 return checkWGLExtensions(hInst);
176 void
177 wglExtDispose(HINSTANCE hInst)
179 BOOL ret;
181 ret = UnregisterClass(GL_CLASS, hInst);
182 assert(ret != FALSE);
185 BOOL
186 wglExtMakeContextCurrent(HWND did, HWND rid, HGLRC gid)
188 HDC hDrawDC;
189 BOOL ret = FALSE;
191 if ((hDrawDC = (HDC)(INT_PTR)GetWindowLongPtr(did, GWLP_USERDATA)) != NULL) {
192 if (did != rid) {
193 HDC hReadDC;
195 if ((hReadDC = (HDC)(INT_PTR)GetWindowLongPtr(rid, GWLP_USERDATA)) != NULL) {
196 ret = (*WGLMakeContextCurrent)(hDrawDC, hReadDC, gid);
197 assert(ret != FALSE);
200 else
201 ret = wglMakeCurrent(hDrawDC, gid);
203 assert(ret != FALSE);
204 return ret;
207 BOOL
208 wglExtSwapBuffers(HWND hWnd)
210 HDC hDC;
212 if ((hDC = (HDC)(INT_PTR)GetWindowLongPtr(hWnd, GWLP_USERDATA)) != NULL);
213 return wglSwapLayerBuffers(hDC, WGL_SWAP_MAIN_PLANE);
214 return FALSE;
218 wglExtChoosePixelFormat(const int *attrs)
220 HDC hDC;
221 HWND hWnd = GetDesktopWindow();
223 if ((hDC = (HDC)GetDC(hWnd)) != NULL) {
224 int format = 0;
225 UINT nFormats;
226 BOOL ret;
228 WGLChoosePixelFormat(hDC, attrs, NULL, 1, &format, &nFormats);
229 assert(nFormats > 0 && format != 0);
230 ret = ReleaseDC(hWnd, hDC);
231 assert(ret != FALSE);
232 return format;
234 return 0;