4 * Created: March 1, 2013
5 * Copyright (c) 2013, Attachmate Corporation All Rights Reserved
12 #define STYLE WS_OVERLAPPEDWINDOW
13 #define EXSTYLE WS_EX_OVERLAPPEDWINDOW
16 static PFNWGLMAKECONTEXTCURRENTARBPROC WGLMakeContextCurrent
= NULL
;
17 static PFNWGLCHOOSEPIXELFORMATARBPROC WGLChoosePixelFormat
= NULL
;
24 if ((hDC
= GetDC(hWnd
)) != NULL
) {
25 SetWindowLongPtr(hWnd
, GWLP_USERDATA
, (LONG_PTR
)hDC
);
27 assert(GetWindowLongPtr(hWnd
, GWLP_USERDATA
) == (LONG_PTR
)hDC
);
38 HDC hDC
= (HDC
)(INT_PTR
)GetWindowLongPtr(hWnd
, GWLP_USERDATA
);
47 getWindow(HINSTANCE hInst
, int x
, int y
, int w
, int h
, int format
)
51 if ((hParent
= GetDesktopWindow()) != NULL
) {
52 HWND hWnd
= CreateWindowEx(EXSTYLE
, GL_CLASS
, "wincopy", STYLE
, x
, y
, w
, h
, hParent
, NULL
, hInst
, NULL
);
57 if ((hDC
= (HDC
)(INT_PTR
)GetWindowLongPtr(hWnd
, GWLP_USERDATA
)) != NULL
) {
58 PIXELFORMATDESCRIPTOR pfd
;
60 if ((ret
= SetPixelFormat(hDC
, format
, &pfd
)) != FALSE
)
62 ret
= DestroyWindow(hWnd
);
66 assert(hParent
!= NULL
);
71 getFormat(HDC hDC
, PIXELFORMATDESCRIPTOR
*pfd
)
75 int size
= sizeof(PIXELFORMATDESCRIPTOR
);
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
);
88 createCurrentContext(HDC hDC
)
92 if ((hGLRC
= wglCreateContext(hDC
)) != NULL
) {
95 if ((current
= wglMakeCurrent(hDC
, hGLRC
)) == FALSE
) {
96 BOOL ret
= wglDeleteContext(hGLRC
);
103 assert(hGLRC
!= NULL
);
108 checkWGLExtensions(HINSTANCE hInst
)
111 BOOL rval
, ret
= FALSE
;
113 if ((hWnd
= CreateWindowEx(EXSTYLE
, GL_CLASS
, "tmp", STYLE
, 0, 0, 1, 1,
114 GetDesktopWindow(), NULL
, hInst
, NULL
)) != NULL
) {
117 if ((hDC
= fetchDC(hWnd
)) != NULL
) {
118 PIXELFORMATDESCRIPTOR pfd
;
121 if ((format
= getFormat(hDC
, &pfd
)) > 0) {
124 if ((setFormat
= SetPixelFormat(hDC
, format
, &pfd
)) != FALSE
) {
125 HGLRC hGLRC
= createCurrentContext(hDC
);
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
)
144 rval
= wglDeleteContext(hGLRC
);
145 assert(rval
!= FALSE
);
150 rval
= DestroyWindow(hWnd
);
151 assert(rval
!= FALSE
);
158 wglExtInit(HINSTANCE hInst
, WNDPROC proc
)
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) {
173 return checkWGLExtensions(hInst
);
177 wglExtDispose(HINSTANCE hInst
)
181 ret
= UnregisterClass(GL_CLASS
, hInst
);
182 assert(ret
!= FALSE
);
186 wglExtMakeContextCurrent(HWND did
, HWND rid
, HGLRC gid
)
191 if ((hDrawDC
= (HDC
)(INT_PTR
)GetWindowLongPtr(did
, GWLP_USERDATA
)) != NULL
) {
195 if ((hReadDC
= (HDC
)(INT_PTR
)GetWindowLongPtr(rid
, GWLP_USERDATA
)) != NULL
) {
196 ret
= (*WGLMakeContextCurrent
)(hDrawDC
, hReadDC
, gid
);
197 assert(ret
!= FALSE
);
201 ret
= wglMakeCurrent(hDrawDC
, gid
);
203 assert(ret
!= FALSE
);
208 wglExtSwapBuffers(HWND hWnd
)
212 if ((hDC
= (HDC
)(INT_PTR
)GetWindowLongPtr(hWnd
, GWLP_USERDATA
)) != NULL
);
213 return wglSwapLayerBuffers(hDC
, WGL_SWAP_MAIN_PLANE
);
218 wglExtChoosePixelFormat(const int *attrs
)
221 HWND hWnd
= GetDesktopWindow();
223 if ((hDC
= (HDC
)GetDC(hWnd
)) != NULL
) {
228 WGLChoosePixelFormat(hDC
, attrs
, NULL
, 1, &format
, &nFormats
);
229 assert(nFormats
> 0 && format
!= 0);
230 ret
= ReleaseDC(hWnd
, hDC
);
231 assert(ret
!= FALSE
);