softpipe: use the polygon stipple utility module
[mesa.git] / src / glut / glx / win32_glx.c
1
2 /* Copyright (c) Nate Robins, 1997. */
3
4 /* This program is freely distributable without licensing fees
5 and is provided without guarantee or warrantee expressed or
6 implied. This program is -not- in the public domain. */
7
8 #include <stdio.h>
9 #include "glutint.h"
10 #include "win32_glx.h"
11
12 /* global current HDC */
13 extern HDC XHDC;
14
15 GLXContext
16 glXCreateContext(Display * display, XVisualInfo * visinfo,
17 GLXContext share, Bool direct)
18 {
19 /* KLUDGE: GLX really expects a display pointer to be passed
20 in as the first parameter, but Win32 needs an HDC instead,
21 so BE SURE that the global XHDC is set before calling this
22 routine. */
23 HGLRC context;
24
25 context = wglCreateContext(XHDC);
26
27 #if 0
28 /* XXX GLUT doesn't support it now, so don't worry about display list
29 and texture object sharing. */
30 if (share) {
31 wglShareLists(share, context);
32 }
33 #endif
34
35 /* Since direct rendering is implicit, the direct flag is
36 ignored. */
37
38 return context;
39 }
40
41 int
42 glXGetConfig(Display * display, XVisualInfo * visual, int attrib, int *value)
43 {
44 if (!visual)
45 return GLX_BAD_VISUAL;
46
47 switch (attrib) {
48 case GLX_USE_GL:
49 if (visual->dwFlags & (PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW)) {
50 /* XXX Brad's Matrix Millenium II has problems creating
51 color index windows in 24-bit mode (lead to GDI crash)
52 and 32-bit mode (lead to black window). The cColorBits
53 filed of the PIXELFORMATDESCRIPTOR returned claims to
54 have 24 and 32 bits respectively of color indices. 2^24
55 and 2^32 are ridiculously huge writable colormaps.
56 Assume that if we get back a color index
57 PIXELFORMATDESCRIPTOR with 24 or more bits, the
58 PIXELFORMATDESCRIPTOR doesn't really work and skip it.
59 -mjk */
60 if (visual->iPixelType == PFD_TYPE_COLORINDEX
61 && visual->cColorBits >= 24) {
62 *value = 0;
63 } else {
64 *value = 1;
65 }
66 } else {
67 *value = 0;
68 }
69 break;
70 case GLX_BUFFER_SIZE:
71 /* KLUDGE: if we're RGBA, return the number of bits/pixel,
72 otherwise, return 8 (we guessed at 256 colors in CI
73 mode). */
74 if (visual->iPixelType == PFD_TYPE_RGBA)
75 *value = visual->cColorBits;
76 else
77 *value = 8;
78 break;
79 case GLX_LEVEL:
80 /* The bReserved flag of the pfd contains the
81 overlay/underlay info. */
82 *value = visual->bReserved;
83 break;
84 case GLX_RGBA:
85 *value = visual->iPixelType == PFD_TYPE_RGBA;
86 break;
87 case GLX_DOUBLEBUFFER:
88 *value = visual->dwFlags & PFD_DOUBLEBUFFER;
89 break;
90 case GLX_STEREO:
91 *value = visual->dwFlags & PFD_STEREO;
92 break;
93 case GLX_AUX_BUFFERS:
94 *value = visual->cAuxBuffers;
95 break;
96 case GLX_RED_SIZE:
97 *value = visual->cRedBits;
98 break;
99 case GLX_GREEN_SIZE:
100 *value = visual->cGreenBits;
101 break;
102 case GLX_BLUE_SIZE:
103 *value = visual->cBlueBits;
104 break;
105 case GLX_ALPHA_SIZE:
106 *value = visual->cAlphaBits;
107 break;
108 case GLX_DEPTH_SIZE:
109 *value = visual->cDepthBits;
110 break;
111 case GLX_STENCIL_SIZE:
112 *value = visual->cStencilBits;
113 break;
114 case GLX_ACCUM_RED_SIZE:
115 *value = visual->cAccumRedBits;
116 break;
117 case GLX_ACCUM_GREEN_SIZE:
118 *value = visual->cAccumGreenBits;
119 break;
120 case GLX_ACCUM_BLUE_SIZE:
121 *value = visual->cAccumBlueBits;
122 break;
123 case GLX_ACCUM_ALPHA_SIZE:
124 *value = visual->cAccumAlphaBits;
125 break;
126 default:
127 return GLX_BAD_ATTRIB;
128 }
129 return 0;
130 }
131
132 XVisualInfo *
133 glXChooseVisual(Display * display, int screen, int *attribList)
134 {
135 /* KLUDGE: since we need the HDC, MAKE SURE to set XHDC
136 before calling this routine. */
137
138 int *p = attribList;
139 int pf;
140 PIXELFORMATDESCRIPTOR pfd;
141 PIXELFORMATDESCRIPTOR *match = NULL;
142 int stereo = 0;
143
144 /* Avoid seg-faults. */
145 if (!p)
146 return NULL;
147
148 memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
149 pfd.nSize = (sizeof(PIXELFORMATDESCRIPTOR));
150 pfd.nVersion = 1;
151
152 /* Defaults. */
153 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
154 pfd.iPixelType = PFD_TYPE_COLORINDEX;
155 pfd.cColorBits = 32;
156 pfd.cDepthBits = 0;
157
158 while (*p) {
159 switch (*p) {
160 case GLX_USE_GL:
161 pfd.dwFlags |= PFD_SUPPORT_OPENGL;
162 break;
163 case GLX_BUFFER_SIZE:
164 pfd.cColorBits = *(++p);
165 break;
166 case GLX_LEVEL:
167 /* the bReserved flag of the pfd contains the
168 overlay/underlay info. */
169 pfd.bReserved = *(++p);
170 break;
171 case GLX_RGBA:
172 pfd.iPixelType = PFD_TYPE_RGBA;
173 break;
174 case GLX_DOUBLEBUFFER:
175 pfd.dwFlags |= PFD_DOUBLEBUFFER;
176 break;
177 case GLX_STEREO:
178 stereo = 1;
179 pfd.dwFlags |= PFD_STEREO;
180 break;
181 case GLX_AUX_BUFFERS:
182 pfd.cAuxBuffers = *(++p);
183 break;
184 case GLX_RED_SIZE:
185 pfd.cRedBits = 8; /* Try to get the maximum. */
186 ++p;
187 break;
188 case GLX_GREEN_SIZE:
189 pfd.cGreenBits = 8;
190 ++p;
191 break;
192 case GLX_BLUE_SIZE:
193 pfd.cBlueBits = 8;
194 ++p;
195 break;
196 case GLX_ALPHA_SIZE:
197 pfd.cAlphaBits = 8;
198 ++p;
199 break;
200 case GLX_DEPTH_SIZE:
201 pfd.cDepthBits = 32;
202 ++p;
203 break;
204 case GLX_STENCIL_SIZE:
205 pfd.cStencilBits = *(++p);
206 break;
207 case GLX_ACCUM_RED_SIZE:
208 case GLX_ACCUM_GREEN_SIZE:
209 case GLX_ACCUM_BLUE_SIZE:
210 case GLX_ACCUM_ALPHA_SIZE:
211 /* I believe that WGL only used the cAccumRedBits,
212 cAccumBlueBits, cAccumGreenBits, and cAccumAlphaBits fields
213 when returning info about the accumulation buffer precision.
214 Only cAccumBits is used for requesting an accumulation
215 buffer. */
216 pfd.cAccumBits = 1;
217 ++p;
218 break;
219 }
220 ++p;
221 }
222
223 /* Let Win32 choose one for us. */
224 pf = ChoosePixelFormat(XHDC, &pfd);
225 if (pf > 0) {
226 match = (PIXELFORMATDESCRIPTOR *) malloc(sizeof(PIXELFORMATDESCRIPTOR));
227 DescribePixelFormat(XHDC, pf, sizeof(PIXELFORMATDESCRIPTOR), match);
228
229 /* ChoosePixelFormat is dumb in that it will return a pixel
230 format that doesn't have stereo even if it was requested
231 so we need to make sure that if stereo was selected, we
232 got it. */
233 if (stereo) {
234 if (!(match->dwFlags & PFD_STEREO)) {
235 free(match);
236 return NULL;
237 }
238 }
239 /* XXX Brad's Matrix Millenium II has problems creating
240 color index windows in 24-bit mode (lead to GDI crash)
241 and 32-bit mode (lead to black window). The cColorBits
242 filed of the PIXELFORMATDESCRIPTOR returned claims to
243 have 24 and 32 bits respectively of color indices. 2^24
244 and 2^32 are ridiculously huge writable colormaps.
245 Assume that if we get back a color index
246 PIXELFORMATDESCRIPTOR with 24 or more bits, the
247 PIXELFORMATDESCRIPTOR doesn't really work and skip it.
248 -mjk */
249 if (match->iPixelType == PFD_TYPE_COLORINDEX
250 && match->cColorBits >= 24) {
251 free(match);
252 return NULL;
253 }
254 }
255 return match;
256 }