1 /* $Id: wgl.c,v 1.12 2006/03/30 07:58:24 kschultz Exp $ */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
23 * Some things originated from the 3Dfx WGL functions
27 * This file contains the implementation of the wgl* functions for
28 * Mesa on Windows. Since these functions are provided by Windows in
29 * GDI/OpenGL, we must supply our versions that work with Mesa here.
33 /* We're essentially building part of GDI here, so define this so that
34 * we get the right export linkage. */
39 #include "GL/wmesa.h" /* protos for wmesa* functions */
42 * Pixel Format Descriptors
45 /* Extend the PFD to include DB flag */
46 struct __pixelformat__
48 PIXELFORMATDESCRIPTOR pfd
;
49 GLboolean doubleBuffered
;
52 /* These are the PFD's supported by this driver. */
53 struct __pixelformat__ pfd
[] =
56 /* Double Buffer, alpha */
59 sizeof(PIXELFORMATDESCRIPTOR
), 1,
60 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
61 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
75 /* Single Buffer, alpha */
78 sizeof(PIXELFORMATDESCRIPTOR
), 1,
79 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
95 /* Double Buffer, no alpha */
98 sizeof(PIXELFORMATDESCRIPTOR
), 1,
99 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
100 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
114 /* Single Buffer, no alpha */
117 sizeof(PIXELFORMATDESCRIPTOR
), 1,
118 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
135 int npfd
= sizeof(pfd
) / sizeof(pfd
[0]);
146 #define MESAWGL_CTX_MAX_COUNT 20
148 static MesaWglCtx wgl_ctx
[MESAWGL_CTX_MAX_COUNT
];
150 static unsigned ctx_count
= 0;
151 static int ctx_current
= -1;
152 static unsigned curPFD
= 0;
154 static HDC CurrentHDC
= 0;
157 WINGDIAPI HGLRC GLAPIENTRY
wglCreateContext(HDC hdc
)
161 for(i
=0;i
<MESAWGL_CTX_MAX_COUNT
;i
++) {
162 wgl_ctx
[i
].ctx
= NULL
;
165 for( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
166 if ( wgl_ctx
[i
].ctx
== NULL
) {
168 WMesaCreateContext(hdc
, NULL
, (GLboolean
)GL_TRUE
,
169 (GLboolean
) (pfd
[curPFD
-1].doubleBuffered
?
171 (GLboolean
)(pfd
[curPFD
-1].pfd
.cAlphaBits
?
172 GL_TRUE
: GL_FALSE
) );
173 if (wgl_ctx
[i
].ctx
== NULL
)
176 return ((HGLRC
)wgl_ctx
[i
].ctx
);
183 WINGDIAPI BOOL GLAPIENTRY
wglDeleteContext(HGLRC hglrc
)
186 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
187 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
){
188 WMesaMakeCurrent((WMesaContext
) hglrc
, NULL
);
189 WMesaDestroyContext(wgl_ctx
[i
].ctx
);
190 wgl_ctx
[i
].ctx
= NULL
;
199 WINGDIAPI HGLRC GLAPIENTRY
wglGetCurrentContext(VOID
)
204 return (HGLRC
) wgl_ctx
[ctx_current
].ctx
;
207 WINGDIAPI HDC GLAPIENTRY
wglGetCurrentDC(VOID
)
212 WINGDIAPI BOOL GLAPIENTRY
wglMakeCurrent(HDC hdc
, HGLRC hglrc
)
218 if (!hdc
|| !hglrc
) {
219 WMesaMakeCurrent(NULL
, NULL
);
224 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
225 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
) {
226 WMesaMakeCurrent( (WMesaContext
) hglrc
, hdc
);
235 WINGDIAPI
int GLAPIENTRY
wglChoosePixelFormat(HDC hdc
,
237 PIXELFORMATDESCRIPTOR
*ppfd
)
239 int i
,best
= -1,bestdelta
= 0x7FFFFFFF,delta
;
242 if(ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
) || ppfd
->nVersion
!= 1)
247 for(i
= 0; i
< npfd
;i
++)
251 (ppfd
->dwFlags
& PFD_DRAW_TO_WINDOW
) &&
252 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_WINDOW
))
255 (ppfd
->dwFlags
& PFD_DRAW_TO_BITMAP
) &&
256 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_BITMAP
))
259 (ppfd
->dwFlags
& PFD_SUPPORT_GDI
) &&
260 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_GDI
))
263 (ppfd
->dwFlags
& PFD_SUPPORT_OPENGL
) &&
264 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_OPENGL
))
267 !(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
268 ((ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
269 (pfd
[i
].pfd
.dwFlags
& PFD_DOUBLEBUFFER
)))
272 !(ppfd
->dwFlags
& PFD_STEREO_DONTCARE
) &&
273 ((ppfd
->dwFlags
& PFD_STEREO
) !=
274 (pfd
[i
].pfd
.dwFlags
& PFD_STEREO
)))
276 if(ppfd
->iPixelType
!= pfd
[i
].pfd
.iPixelType
)
278 if(ppfd
->cAlphaBits
!= pfd
[i
].pfd
.cAlphaBits
)
280 if(delta
< bestdelta
)
296 WINGDIAPI
int GLAPIENTRY
wglDescribePixelFormat(HDC hdc
,
299 LPPIXELFORMATDESCRIPTOR ppfd
)
305 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
306 nBytes
!= sizeof(PIXELFORMATDESCRIPTOR
))
311 *ppfd
= pfd
[iPixelFormat
- 1].pfd
;
315 WINGDIAPI PROC GLAPIENTRY
wglGetProcAddress(LPCSTR lpszProc
)
317 PROC p
= (PROC
) _glapi_get_proc_address((const char *) lpszProc
);
325 WINGDIAPI
int GLAPIENTRY
wglGetPixelFormat(HDC hdc
)
335 WINGDIAPI BOOL GLAPIENTRY
wglSetPixelFormat(HDC hdc
,int iPixelFormat
,
336 PIXELFORMATDESCRIPTOR
*ppfd
)
340 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
341 ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
)) {
345 curPFD
= iPixelFormat
;
349 WINGDIAPI BOOL GLAPIENTRY
wglSwapBuffers(HDC hdc
)
351 WMesaSwapBuffers(hdc
);
355 static FIXED
FixedFromDouble(double d
)
357 long l
= (long) (d
* 65536L);
358 return *(FIXED
*) (void *) &l
;
363 ** This is cribbed from FX/fxwgl.c, and seems to implement support
364 ** for bitmap fonts where the wglUseFontBitmapsA() code implements
365 ** support for outline fonts. In combination they hopefully give
366 ** fairly generic support for fonts.
368 static BOOL
wglUseFontBitmaps_FX(HDC fontDevice
, DWORD firstChar
,
369 DWORD numChars
, DWORD listBase
)
379 VERIFY(GetTextMetrics(fontDevice
, &metric
));
381 dibInfo
= (BITMAPINFO
*) calloc(sizeof(BITMAPINFO
) + sizeof(RGBQUAD
), 1);
382 dibInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
383 dibInfo
->bmiHeader
.biPlanes
= 1;
384 dibInfo
->bmiHeader
.biBitCount
= 1;
385 dibInfo
->bmiHeader
.biCompression
= BI_RGB
;
387 bitDevice
= CreateCompatibleDC(fontDevice
);
389 // Swap fore and back colors so the bitmap has the right polarity
390 tempColor
= GetBkColor(bitDevice
);
391 SetBkColor(bitDevice
, GetTextColor(bitDevice
));
392 SetTextColor(bitDevice
, tempColor
);
394 // Place chars based on base line
395 VERIFY(SetTextAlign(bitDevice
, TA_BASELINE
) != GDI_ERROR
? 1 : 0);
397 for(i
= 0; i
< (int)numChars
; i
++) {
400 int charWidth
,charHeight
,bmapWidth
,bmapHeight
,numBytes
,res
;
405 curChar
= (char)(i
+ firstChar
);
407 // Find how high/wide this character is
408 VERIFY(GetTextExtentPoint32(bitDevice
, &curChar
, 1, &size
));
410 // Create the output bitmap
412 charHeight
= size
.cy
;
413 // Round up to the next multiple of 32 bits
414 bmapWidth
= ((charWidth
+ 31) / 32) * 32;
415 bmapHeight
= charHeight
;
416 bitObject
= CreateCompatibleBitmap(bitDevice
,
421 // Assign the output bitmap to the device
422 origBmap
= SelectObject(bitDevice
, bitObject
);
423 (void) VERIFY(origBmap
);
425 VERIFY( PatBlt( bitDevice
, 0, 0, bmapWidth
, bmapHeight
,BLACKNESS
) );
427 // Use our source font on the device
428 VERIFY(SelectObject(bitDevice
, GetCurrentObject(fontDevice
,OBJ_FONT
)));
430 // Draw the character
431 VERIFY(TextOut(bitDevice
, 0, metric
.tmAscent
, &curChar
, 1));
433 // Unselect our bmap object
434 VERIFY(SelectObject(bitDevice
, origBmap
));
436 // Convert the display dependant representation to a 1 bit deep DIB
437 numBytes
= (bmapWidth
* bmapHeight
) / 8;
438 bmap
= malloc(numBytes
);
439 dibInfo
->bmiHeader
.biWidth
= bmapWidth
;
440 dibInfo
->bmiHeader
.biHeight
= bmapHeight
;
441 res
= GetDIBits(bitDevice
, bitObject
, 0, bmapHeight
, bmap
,
446 // Create the GL object
447 glNewList(i
+ listBase
, GL_COMPILE
);
448 glBitmap(bmapWidth
, bmapHeight
, 0.0, (GLfloat
)metric
.tmDescent
,
449 (GLfloat
)charWidth
, 0.0,
454 // Destroy the bmap object
455 DeleteObject(bitObject
);
457 // Deallocate the bitmap data
462 VERIFY(DeleteDC(bitDevice
));
470 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsA(HDC hdc
, DWORD first
,
471 DWORD count
, DWORD listBase
)
485 font_list
= listBase
;
487 mat
.eM11
= FixedFromDouble(1);
488 mat
.eM12
= FixedFromDouble(0);
489 mat
.eM21
= FixedFromDouble(0);
490 mat
.eM22
= FixedFromDouble(-1);
492 memset(&gm
,0,sizeof(gm
));
495 ** If we can't get the glyph outline, it may be because this is a fixed
496 ** font. Try processing it that way.
498 if( GetGlyphOutline(hdc
, first
, GGO_BITMAP
, &gm
, 0, NULL
, &mat
)
500 return wglUseFontBitmaps_FX( hdc
, first
, count
, listBase
);
504 ** Otherwise process all desired characters.
506 for (i
= 0; i
< (int)count
; i
++) {
509 glNewList( font_list
+i
, GL_COMPILE
);
511 /* allocate space for the bitmap/outline */
512 size
= GetGlyphOutline(hdc
, first
+ i
, GGO_BITMAP
,
514 if (size
== GDI_ERROR
) {
516 err
= GetLastError();
521 hBits
= GlobalAlloc(GHND
, size
+1);
522 lpBits
= GlobalLock(hBits
);
525 GetGlyphOutline(hdc
, /* handle to device context */
526 first
+ i
, /* character to query */
527 GGO_BITMAP
, /* format of data to return */
528 &gm
, /* ptr to structure for metrics*/
529 size
, /* size of buffer for data */
530 lpBits
, /* pointer to buffer for data */
531 &mat
/* pointer to transformation */
532 /* matrix structure */
535 if (err
== GDI_ERROR
) {
540 err
= GetLastError();
545 glBitmap(gm
.gmBlackBoxX
,gm
.gmBlackBoxY
,
546 (GLfloat
)-gm
.gmptGlyphOrigin
.x
,
547 (GLfloat
)gm
.gmptGlyphOrigin
.y
,
548 (GLfloat
)gm
.gmCellIncX
,
549 (GLfloat
)gm
.gmCellIncY
,
550 (const GLubyte
* )lpBits
);
563 /* NOT IMPLEMENTED YET */
564 WINGDIAPI BOOL GLAPIENTRY
wglCopyContext(HGLRC hglrcSrc
,
568 (void) hglrcSrc
; (void) hglrcDst
; (void) mask
;
572 WINGDIAPI HGLRC GLAPIENTRY
wglCreateLayerContext(HDC hdc
,
575 (void) hdc
; (void) iLayerPlane
;
580 WINGDIAPI BOOL GLAPIENTRY
wglShareLists(HGLRC hglrc1
,
583 (void) hglrc1
; (void) hglrc2
;
588 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsW(HDC hdc
,
593 (void) hdc
; (void) first
; (void) count
; (void) listBase
;
597 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesA(HDC hdc
,
604 LPGLYPHMETRICSFLOAT lpgmf
)
606 (void) hdc
; (void) first
; (void) count
;
607 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
613 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesW(HDC hdc
,
620 LPGLYPHMETRICSFLOAT lpgmf
)
622 (void) hdc
; (void) first
; (void) count
;
623 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
629 WINGDIAPI BOOL GLAPIENTRY
wglDescribeLayerPlane(HDC hdc
,
633 LPLAYERPLANEDESCRIPTOR plpd
)
635 (void) hdc
; (void) iPixelFormat
; (void) iLayerPlane
;
636 (void) nBytes
; (void) plpd
;
641 WINGDIAPI
int GLAPIENTRY
wglSetLayerPaletteEntries(HDC hdc
,
647 (void) hdc
; (void) iLayerPlane
; (void) iStart
;
648 (void) cEntries
; (void) pcr
;
653 WINGDIAPI
int GLAPIENTRY
wglGetLayerPaletteEntries(HDC hdc
,
659 (void) hdc
; (void) iLayerPlane
; (void) iStart
; (void) cEntries
; (void) pcr
;
664 WINGDIAPI BOOL GLAPIENTRY
wglRealizeLayerPalette(HDC hdc
,
668 (void) hdc
; (void) iLayerPlane
; (void) bRealize
;
673 WINGDIAPI BOOL GLAPIENTRY
wglSwapLayerBuffers(HDC hdc
,
676 (void) hdc
; (void) fuPlanes
;
681 WINGDIAPI
const char * GLAPIENTRY
wglGetExtensionsStringARB(HDC hdc
)
683 return "WGL_ARB_extensions_string";