1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
33 static LRESULT CALLBACK
34 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
38 case WM_GETMINMAXINFO:
39 // Allow to create a window bigger than the desktop
40 pMMI = (MINMAXINFO *)lParam;
41 pMMI->ptMaxSize.x = 60000;
42 pMMI->ptMaxSize.y = 60000;
43 pMMI->ptMaxTrackSize.x = 60000;
44 pMMI->ptMaxTrackSize.y = 60000;
50 return DefWindowProc(hWnd, uMsg, wParam, lParam);
54 class WglDrawable : public Drawable
61 PIXELFORMATDESCRIPTOR pfd;
64 WglDrawable(const Visual *vis, int width, int height) :
65 Drawable(vis, width, height)
67 static bool first = TRUE;
72 memset(&wc, 0, sizeof wc);
73 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
74 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
75 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
76 wc.lpfnWndProc = WndProc;
77 wc.lpszClassName = "glretrace";
78 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
84 dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
90 rect.right = rect.left + width;
91 rect.bottom = rect.top + height;
93 AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
95 hWnd = CreateWindowEx(dwExStyle,
96 "glretrace", /* wc.lpszClassName */
101 rect.right - rect.left, /* width */
102 rect.bottom - rect.top, /* height */
109 memset(&pfd, 0, sizeof pfd);
116 pfd.cStencilBits = 1;
117 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
118 pfd.iLayerType = PFD_MAIN_PLANE;
119 pfd.iPixelType = PFD_TYPE_RGBA;
120 pfd.nSize = sizeof(pfd);
123 if (visual->doubleBuffer) {
124 pfd.dwFlags |= PFD_DOUBLEBUFFER;
127 iPixelFormat = ChoosePixelFormat(hDC, &pfd);
129 SetPixelFormat(hDC, iPixelFormat, &pfd);
133 ReleaseDC(hWnd, hDC);
138 resize(int w, int h) {
139 if (w == width && h == height) {
143 RECT rClient, rWindow;
144 GetClientRect(hWnd, &rClient);
145 GetWindowRect(hWnd, &rWindow);
146 w += (rWindow.right - rWindow.left) - rClient.right;
147 h += (rWindow.bottom - rWindow.top) - rClient.bottom;
148 SetWindowPos(hWnd, NULL, rWindow.left, rWindow.top, w, h, SWP_NOMOVE);
150 Drawable::resize(w, h);
158 ShowWindow(hWnd, SW_SHOW);
163 void swapBuffers(void) {
166 // Drain message queue to prevent window from being considered
169 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
170 TranslateMessage(&msg);
171 DispatchMessage(&msg);
177 class WglContext : public Context
181 WglContext *shareContext;
183 WglContext(const Visual *vis, Profile prof, WglContext *share) :
191 wglDeleteContext(hglrc);
200 * OpenGL library must be loaded by the time we call GDI.
202 __libGlHandle = LoadLibraryA("OPENGL32");
210 createVisual(bool doubleBuffer, Profile profile) {
211 if (profile != PROFILE_COMPAT) {
215 Visual *visual = new Visual();
217 visual->doubleBuffer = doubleBuffer;
223 createDrawable(const Visual *visual, int width, int height)
225 return new WglDrawable(visual, width, height);
229 createContext(const Visual *visual, Context *shareContext, Profile profile)
231 if (profile != PROFILE_COMPAT) {
235 return new WglContext(visual, profile, static_cast<WglContext *>(shareContext));
239 makeCurrent(Drawable *drawable, Context *context)
241 if (!drawable || !context) {
242 return wglMakeCurrent(NULL, NULL);
244 WglDrawable *wglDrawable = static_cast<WglDrawable *>(drawable);
245 WglContext *wglContext = static_cast<WglContext *>(context);
247 if (!wglContext->hglrc) {
248 wglContext->hglrc = wglCreateContext(wglDrawable->hDC);
249 if (!wglContext->hglrc) {
252 if (wglContext->shareContext) {
253 wglShareLists(wglContext->shareContext->hglrc,
258 return wglMakeCurrent(wglDrawable->hDC, wglContext->hglrc);
263 processEvents(void) {
269 } /* namespace glws */