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 **************************************************************************/
26 #include "glimports.hpp"
33 class WglDrawable : public Drawable
40 PIXELFORMATDESCRIPTOR pfd;
43 WglDrawable(const Visual *vis) :
46 static bool first = TRUE;
51 memset(&wc, 0, sizeof wc);
52 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
53 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
54 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
55 wc.lpfnWndProc = DefWindowProc;
56 wc.lpszClassName = "glretrace";
57 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
63 dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_OVERLAPPEDWINDOW;
65 int x = 0, y = 0, width = 256, height = 256;
69 rect.right = rect.left + width;
70 rect.bottom = rect.top + height;
72 AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
74 hWnd = CreateWindowEx(dwExStyle,
75 "glretrace", /* wc.lpszClassName */
78 CW_USEDEFAULT, /* x */
79 CW_USEDEFAULT, /* y */
80 rect.right - rect.left, /* width */
81 rect.bottom - rect.top, /* height */
88 memset(&pfd, 0, sizeof pfd);
95 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
96 pfd.iLayerType = PFD_MAIN_PLANE;
97 pfd.iPixelType = PFD_TYPE_RGBA;
98 pfd.nSize = sizeof(pfd);
101 if (visual->doubleBuffer) {
102 pfd.dwFlags |= PFD_DOUBLEBUFFER;
105 iPixelFormat = ChoosePixelFormat(hDC, &pfd);
107 SetPixelFormat(hDC, iPixelFormat, &pfd);
111 ReleaseDC(hWnd, hDC);
116 resize(unsigned w, unsigned h) {
117 Drawable::resize(w, h);
118 RECT rClient, rWindow;
119 GetClientRect(hWnd, &rClient);
120 GetWindowRect(hWnd, &rWindow);
121 w += (rWindow.right - rWindow.left) - rClient.right;
122 h += (rWindow.bottom - rWindow.top) - rClient.bottom;
123 MoveWindow(hWnd, rWindow.left, rWindow.top, w, h, TRUE);
126 void swapBuffers(void) {
132 class WglContext : public Context
137 WglContext(const Visual *vis) :
144 wglDeleteContext(hglrc);
150 class WglWindowSystem : public WindowSystem
154 createVisual(bool doubleBuffer) {
155 Visual *visual = new Visual();
157 visual->doubleBuffer = doubleBuffer;
163 createDrawable(const Visual *visual)
165 return new WglDrawable(visual);
169 createContext(const Visual *visual)
171 return new WglContext(visual);
175 makeCurrent(Drawable *drawable, Context *context)
177 if (!drawable || !context) {
178 return wglMakeCurrent(NULL, NULL);
180 WglDrawable *wglDrawable = dynamic_cast<WglDrawable *>(drawable);
181 WglContext *wglContext = dynamic_cast<WglContext *>(context);
183 if (!wglContext->hglrc) {
184 wglContext->hglrc = wglCreateContext(wglDrawable->hDC);
185 if (!wglContext->hglrc) {
190 return wglMakeCurrent(wglDrawable->hDC, wglContext->hglrc);
195 processEvents(void) {
202 WindowSystem *createNativeWindowSystem(void) {
203 return new WglWindowSystem();
207 } /* namespace glretrace */