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);
93 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
94 pfd.iLayerType = PFD_MAIN_PLANE;
95 pfd.iPixelType = PFD_TYPE_RGBA;
96 pfd.nSize = sizeof(pfd);
99 if (visual->doubleBuffer) {
100 pfd.dwFlags |= PFD_DOUBLEBUFFER;
103 iPixelFormat = ChoosePixelFormat(hDC, &pfd);
105 SetPixelFormat(hDC, iPixelFormat, &pfd);
109 ReleaseDC(hWnd, hDC);
114 resize(unsigned w, unsigned h) {
115 Drawable::resize(w, h);
116 RECT rClient, rWindow;
117 GetClientRect(hWnd, &rClient);
118 GetWindowRect(hWnd, &rWindow);
119 w += (rWindow.right - rWindow.left) - rClient.right;
120 h += (rWindow.bottom - rWindow.top) - rClient.bottom;
121 MoveWindow(hWnd, rWindow.left, rWindow.top, w, h, TRUE);
124 void swapBuffers(void) {
130 class WglContext : public Context
135 WglContext(const Visual *vis) :
142 wglDeleteContext(hglrc);
148 class WglWindowSystem : public WindowSystem
152 createVisual(bool doubleBuffer) {
153 Visual *visual = new Visual();
155 visual->doubleBuffer = doubleBuffer;
161 createDrawable(const Visual *visual)
163 return new WglDrawable(visual);
167 createContext(const Visual *visual)
169 return new WglContext(visual);
173 makeCurrent(Drawable *drawable, Context *context)
175 if (!drawable || !context) {
176 return wglMakeCurrent(NULL, NULL);
178 WglDrawable *wglDrawable = dynamic_cast<WglDrawable *>(drawable);
179 WglContext *wglContext = dynamic_cast<WglContext *>(context);
181 if (!wglContext->hglrc) {
182 wglContext->hglrc = wglCreateContext(wglDrawable->hDC);
183 if (!wglContext->hglrc) {
188 return wglMakeCurrent(wglDrawable->hDC, wglContext->hglrc);
193 processEvents(void) {
200 WindowSystem *createNativeWindowSystem(void) {
201 return new WglWindowSystem();
205 } /* namespace glretrace */