]> git.cworth.org Git - apitrace/blob - glws_wgl.cpp
Dump the full set of constants on D3D.
[apitrace] / glws_wgl.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include "glimports.hpp"
27 #include "glws.hpp"
28
29
30 namespace glws {
31
32
33 class WglDrawable : public Drawable
34 {
35 public:
36     DWORD dwExStyle;
37     DWORD dwStyle;
38     HWND hWnd;
39     HDC hDC;
40     PIXELFORMATDESCRIPTOR pfd;
41     int iPixelFormat;
42
43     WglDrawable(const Visual *vis) :
44         Drawable(vis)
45     {
46         static bool first = TRUE;
47         RECT rect;
48
49         if (first) {
50             WNDCLASS wc;
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;
58             RegisterClass(&wc);
59             first = FALSE;
60         }
61
62         dwExStyle = 0;
63         dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_OVERLAPPEDWINDOW;
64
65         int x = 0, y = 0, width = 256, height = 256;
66
67         rect.left = x;
68         rect.top = y;
69         rect.right = rect.left + width;
70         rect.bottom = rect.top + height;
71
72         AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
73
74         hWnd = CreateWindowEx(dwExStyle,
75                               "glretrace", /* wc.lpszClassName */
76                               NULL,
77                               dwStyle,
78                               CW_USEDEFAULT, /* x */
79                               CW_USEDEFAULT, /* y */
80                               rect.right - rect.left, /* width */
81                               rect.bottom - rect.top, /* height */
82                               NULL,
83                               NULL,
84                               NULL,
85                               NULL);
86         hDC = GetDC(hWnd);
87    
88         memset(&pfd, 0, sizeof pfd);
89         pfd.cColorBits = 3;
90         pfd.cRedBits = 1;
91         pfd.cGreenBits = 1;
92         pfd.cBlueBits = 1;
93         pfd.cDepthBits = 1;
94         pfd.cStencilBits = 1;
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);
99         pfd.nVersion = 1;
100
101         if (visual->doubleBuffer) {
102            pfd.dwFlags |= PFD_DOUBLEBUFFER;
103         }
104
105         iPixelFormat = ChoosePixelFormat(hDC, &pfd);
106
107         SetPixelFormat(hDC, iPixelFormat, &pfd);
108     }
109
110     ~WglDrawable() {
111         ReleaseDC(hWnd, hDC);
112         DestroyWindow(hWnd);
113     }
114     
115     void
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);
124     }
125
126     void swapBuffers(void) {
127         SwapBuffers(hDC);
128     }
129 };
130
131
132 class WglContext : public Context
133 {
134 public:
135     HGLRC hglrc;
136     
137     WglContext(const Visual *vis) :
138         Context(vis),
139         hglrc(0)
140     {}
141
142     ~WglContext() {
143         if (hglrc) {
144             wglDeleteContext(hglrc);
145         }
146     }
147 };
148
149
150 class WglWindowSystem : public WindowSystem
151 {
152 public:
153     Visual *
154     createVisual(bool doubleBuffer) {
155         Visual *visual = new Visual();
156
157         visual->doubleBuffer = doubleBuffer;
158
159         return visual;
160     }
161     
162     Drawable *
163     createDrawable(const Visual *visual)
164     {
165         return new WglDrawable(visual);
166     }
167
168     Context *
169     createContext(const Visual *visual)
170     {
171         return new WglContext(visual);
172     }
173
174     bool
175     makeCurrent(Drawable *drawable, Context *context)
176     {
177         if (!drawable || !context) {
178             return wglMakeCurrent(NULL, NULL);
179         } else {
180             WglDrawable *wglDrawable = dynamic_cast<WglDrawable *>(drawable);
181             WglContext *wglContext = dynamic_cast<WglContext *>(context);
182
183             if (!wglContext->hglrc) {
184                 wglContext->hglrc = wglCreateContext(wglDrawable->hDC);
185                 if (!wglContext->hglrc) {
186                     return false;
187                 }
188             }
189
190             return wglMakeCurrent(wglDrawable->hDC, wglContext->hglrc);
191         }
192     }
193
194     bool
195     processEvents(void) {
196         // TODO
197         return true;
198     }
199 };
200
201
202 WindowSystem *createNativeWindowSystem(void) {
203     return new WglWindowSystem();
204 }
205
206
207 } /* namespace glretrace */