3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
22 * Some things originated from the 3Dfx WGL functions
26 * This file contains the implementation of the wgl* functions for
27 * Mesa on Windows. Since these functions are provided by Windows in
28 * GDI/OpenGL, we must supply our versions that work with Mesa here.
32 /* We're essentially building part of GDI here, so define this so that
33 * we get the right export linkage. */
41 # if defined(BUILD_GL32)
42 # define WINGDIAPI __declspec(dllexport)
44 # define __W32API_USE_DLLIMPORT__
48 #include "GL/mesa_wgl.h"
59 #include "GL/wmesa.h" /* protos for wmesa* functions */
62 * Pixel Format Descriptors
65 /* Extend the PFD to include DB flag */
66 struct __pixelformat__
68 PIXELFORMATDESCRIPTOR pfd
;
69 GLboolean doubleBuffered
;
72 /* These are the PFD's supported by this driver. */
73 struct __pixelformat__ pfd
[] =
76 /* Double Buffer, alpha */
79 sizeof(PIXELFORMATDESCRIPTOR
), 1,
80 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
81 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
95 /* Single Buffer, alpha */
98 sizeof(PIXELFORMATDESCRIPTOR
), 1,
99 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
115 /* Double Buffer, no alpha */
118 sizeof(PIXELFORMATDESCRIPTOR
), 1,
119 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
120 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
134 /* Single Buffer, no alpha */
137 sizeof(PIXELFORMATDESCRIPTOR
), 1,
138 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
155 int npfd
= sizeof(pfd
) / sizeof(pfd
[0]);
166 #define MESAWGL_CTX_MAX_COUNT 20
168 static MesaWglCtx wgl_ctx
[MESAWGL_CTX_MAX_COUNT
];
170 static unsigned ctx_count
= 0;
171 static int ctx_current
= -1;
172 static unsigned curPFD
= 0;
174 static HDC CurrentHDC
= 0;
177 WINGDIAPI HGLRC GLAPIENTRY
wglCreateContext(HDC hdc
)
181 for(i
=0;i
<MESAWGL_CTX_MAX_COUNT
;i
++) {
182 wgl_ctx
[i
].ctx
= NULL
;
185 for( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
186 if ( wgl_ctx
[i
].ctx
== NULL
) {
188 WMesaCreateContext(hdc
, NULL
, (GLboolean
)GL_TRUE
,
189 (GLboolean
) (pfd
[curPFD
-1].doubleBuffered
?
191 (GLboolean
)(pfd
[curPFD
-1].pfd
.cAlphaBits
?
192 GL_TRUE
: GL_FALSE
) );
193 if (wgl_ctx
[i
].ctx
== NULL
)
196 return ((HGLRC
)wgl_ctx
[i
].ctx
);
203 WINGDIAPI BOOL GLAPIENTRY
wglDeleteContext(HGLRC hglrc
)
206 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
207 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
){
208 WMesaMakeCurrent((WMesaContext
) hglrc
, NULL
);
209 WMesaDestroyContext(wgl_ctx
[i
].ctx
);
210 wgl_ctx
[i
].ctx
= NULL
;
219 WINGDIAPI HGLRC GLAPIENTRY
wglGetCurrentContext(VOID
)
224 return (HGLRC
) wgl_ctx
[ctx_current
].ctx
;
227 WINGDIAPI HDC GLAPIENTRY
wglGetCurrentDC(VOID
)
232 WINGDIAPI BOOL GLAPIENTRY
wglMakeCurrent(HDC hdc
, HGLRC hglrc
)
238 if (!hdc
|| !hglrc
) {
239 WMesaMakeCurrent(NULL
, NULL
);
244 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
245 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
) {
246 WMesaMakeCurrent( (WMesaContext
) hglrc
, hdc
);
255 WINGDIAPI
int GLAPIENTRY
wglChoosePixelFormat(HDC hdc
,
257 PIXELFORMATDESCRIPTOR
*ppfd
)
259 int i
,best
= -1,bestdelta
= 0x7FFFFFFF,delta
;
262 if(ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
) || ppfd
->nVersion
!= 1)
267 for(i
= 0; i
< npfd
;i
++)
271 (ppfd
->dwFlags
& PFD_DRAW_TO_WINDOW
) &&
272 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_WINDOW
))
275 (ppfd
->dwFlags
& PFD_DRAW_TO_BITMAP
) &&
276 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_BITMAP
))
279 (ppfd
->dwFlags
& PFD_SUPPORT_GDI
) &&
280 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_GDI
))
283 (ppfd
->dwFlags
& PFD_SUPPORT_OPENGL
) &&
284 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_OPENGL
))
287 !(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
288 ((ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
289 (pfd
[i
].pfd
.dwFlags
& PFD_DOUBLEBUFFER
)))
292 !(ppfd
->dwFlags
& PFD_STEREO_DONTCARE
) &&
293 ((ppfd
->dwFlags
& PFD_STEREO
) !=
294 (pfd
[i
].pfd
.dwFlags
& PFD_STEREO
)))
296 if(ppfd
->iPixelType
!= pfd
[i
].pfd
.iPixelType
)
298 if(ppfd
->cAlphaBits
!= pfd
[i
].pfd
.cAlphaBits
)
300 if(delta
< bestdelta
)
316 WINGDIAPI
int GLAPIENTRY
wglDescribePixelFormat(HDC hdc
,
319 LPPIXELFORMATDESCRIPTOR ppfd
)
325 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
326 nBytes
!= sizeof(PIXELFORMATDESCRIPTOR
))
331 *ppfd
= pfd
[iPixelFormat
- 1].pfd
;
335 WINGDIAPI PROC GLAPIENTRY
wglGetProcAddress(LPCSTR lpszProc
)
337 PROC p
= (PROC
) _glapi_get_proc_address((const char *) lpszProc
);
345 WINGDIAPI
int GLAPIENTRY
wglGetPixelFormat(HDC hdc
)
355 WINGDIAPI BOOL GLAPIENTRY
wglSetPixelFormat(HDC hdc
,int iPixelFormat
,
356 const PIXELFORMATDESCRIPTOR
*ppfd
)
360 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
361 ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
)) {
365 curPFD
= iPixelFormat
;
369 WINGDIAPI BOOL GLAPIENTRY
wglSwapBuffers(HDC hdc
)
371 WMesaSwapBuffers(hdc
);
375 static FIXED
FixedFromDouble(double d
)
377 long l
= (long) (d
* 65536L);
378 return *(FIXED
*) (void *) &l
;
383 ** This is cribbed from FX/fxwgl.c, and seems to implement support
384 ** for bitmap fonts where the wglUseFontBitmapsA() code implements
385 ** support for outline fonts. In combination they hopefully give
386 ** fairly generic support for fonts.
388 static BOOL
wglUseFontBitmaps_FX(HDC fontDevice
, DWORD firstChar
,
389 DWORD numChars
, DWORD listBase
)
399 VERIFY(GetTextMetrics(fontDevice
, &metric
));
401 dibInfo
= (BITMAPINFO
*) calloc(sizeof(BITMAPINFO
) + sizeof(RGBQUAD
), 1);
402 dibInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
403 dibInfo
->bmiHeader
.biPlanes
= 1;
404 dibInfo
->bmiHeader
.biBitCount
= 1;
405 dibInfo
->bmiHeader
.biCompression
= BI_RGB
;
407 bitDevice
= CreateCompatibleDC(fontDevice
);
409 /* Swap fore and back colors so the bitmap has the right polarity */
410 tempColor
= GetBkColor(bitDevice
);
411 SetBkColor(bitDevice
, GetTextColor(bitDevice
));
412 SetTextColor(bitDevice
, tempColor
);
414 /* Place chars based on base line */
415 VERIFY(SetTextAlign(bitDevice
, TA_BASELINE
) != GDI_ERROR
? 1 : 0);
417 for(i
= 0; i
< (int)numChars
; i
++) {
420 int charWidth
,charHeight
,bmapWidth
,bmapHeight
,numBytes
,res
;
425 curChar
= (char)(i
+ firstChar
);
427 /* Find how high/wide this character is */
428 VERIFY(GetTextExtentPoint32(bitDevice
, &curChar
, 1, &size
));
430 /* Create the output bitmap */
432 charHeight
= size
.cy
;
433 /* Round up to the next multiple of 32 bits */
434 bmapWidth
= ((charWidth
+ 31) / 32) * 32;
435 bmapHeight
= charHeight
;
436 bitObject
= CreateCompatibleBitmap(bitDevice
,
439 /* VERIFY(bitObject); */
441 /* Assign the output bitmap to the device */
442 origBmap
= SelectObject(bitDevice
, bitObject
);
443 (void) VERIFY(origBmap
);
445 VERIFY( PatBlt( bitDevice
, 0, 0, bmapWidth
, bmapHeight
,BLACKNESS
) );
447 /* Use our source font on the device */
448 VERIFY(SelectObject(bitDevice
, GetCurrentObject(fontDevice
,OBJ_FONT
)));
450 /* Draw the character */
451 VERIFY(TextOut(bitDevice
, 0, metric
.tmAscent
, &curChar
, 1));
453 /* Unselect our bmap object */
454 VERIFY(SelectObject(bitDevice
, origBmap
));
456 /* Convert the display dependant representation to a 1 bit deep DIB */
457 numBytes
= (bmapWidth
* bmapHeight
) / 8;
458 bmap
= malloc(numBytes
);
459 dibInfo
->bmiHeader
.biWidth
= bmapWidth
;
460 dibInfo
->bmiHeader
.biHeight
= bmapHeight
;
461 res
= GetDIBits(bitDevice
, bitObject
, 0, bmapHeight
, bmap
,
466 /* Create the GL object */
467 glNewList(i
+ listBase
, GL_COMPILE
);
468 glBitmap(bmapWidth
, bmapHeight
, 0.0, (GLfloat
)metric
.tmDescent
,
469 (GLfloat
)charWidth
, 0.0,
474 /* Destroy the bmap object */
475 DeleteObject(bitObject
);
477 /* Deallocate the bitmap data */
482 VERIFY(DeleteDC(bitDevice
));
490 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsA(HDC hdc
, DWORD first
,
491 DWORD count
, DWORD listBase
)
505 font_list
= listBase
;
507 mat
.eM11
= FixedFromDouble(1);
508 mat
.eM12
= FixedFromDouble(0);
509 mat
.eM21
= FixedFromDouble(0);
510 mat
.eM22
= FixedFromDouble(-1);
512 memset(&gm
,0,sizeof(gm
));
515 ** If we can't get the glyph outline, it may be because this is a fixed
516 ** font. Try processing it that way.
518 if( GetGlyphOutline(hdc
, first
, GGO_BITMAP
, &gm
, 0, NULL
, &mat
)
520 return wglUseFontBitmaps_FX( hdc
, first
, count
, listBase
);
524 ** Otherwise process all desired characters.
526 for (i
= 0; i
< (int)count
; i
++) {
529 glNewList( font_list
+i
, GL_COMPILE
);
531 /* allocate space for the bitmap/outline */
532 size
= GetGlyphOutline(hdc
, first
+ i
, GGO_BITMAP
,
534 if (size
== GDI_ERROR
) {
536 err
= GetLastError();
541 hBits
= GlobalAlloc(GHND
, size
+1);
542 lpBits
= GlobalLock(hBits
);
545 GetGlyphOutline(hdc
, /* handle to device context */
546 first
+ i
, /* character to query */
547 GGO_BITMAP
, /* format of data to return */
548 &gm
, /* ptr to structure for metrics*/
549 size
, /* size of buffer for data */
550 lpBits
, /* pointer to buffer for data */
551 &mat
/* pointer to transformation */
552 /* matrix structure */
555 if (err
== GDI_ERROR
) {
560 err
= GetLastError();
565 glBitmap(gm
.gmBlackBoxX
,gm
.gmBlackBoxY
,
566 (GLfloat
)-gm
.gmptGlyphOrigin
.x
,
567 (GLfloat
)gm
.gmptGlyphOrigin
.y
,
568 (GLfloat
)gm
.gmCellIncX
,
569 (GLfloat
)gm
.gmCellIncY
,
570 (const GLubyte
* )lpBits
);
583 /* NOT IMPLEMENTED YET */
584 WINGDIAPI BOOL GLAPIENTRY
wglCopyContext(HGLRC hglrcSrc
,
588 (void) hglrcSrc
; (void) hglrcDst
; (void) mask
;
592 WINGDIAPI HGLRC GLAPIENTRY
wglCreateLayerContext(HDC hdc
,
595 (void) hdc
; (void) iLayerPlane
;
600 WINGDIAPI BOOL GLAPIENTRY
wglShareLists(HGLRC hglrc1
,
603 (void) hglrc1
; (void) hglrc2
;
608 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsW(HDC hdc
,
613 (void) hdc
; (void) first
; (void) count
; (void) listBase
;
617 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesA(HDC hdc
,
624 LPGLYPHMETRICSFLOAT lpgmf
)
626 (void) hdc
; (void) first
; (void) count
;
627 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
633 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesW(HDC hdc
,
640 LPGLYPHMETRICSFLOAT lpgmf
)
642 (void) hdc
; (void) first
; (void) count
;
643 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
649 WINGDIAPI BOOL GLAPIENTRY
wglDescribeLayerPlane(HDC hdc
,
653 LPLAYERPLANEDESCRIPTOR plpd
)
655 (void) hdc
; (void) iPixelFormat
; (void) iLayerPlane
;
656 (void) nBytes
; (void) plpd
;
661 WINGDIAPI
int GLAPIENTRY
wglSetLayerPaletteEntries(HDC hdc
,
667 (void) hdc
; (void) iLayerPlane
; (void) iStart
;
668 (void) cEntries
; (void) pcr
;
673 WINGDIAPI
int GLAPIENTRY
wglGetLayerPaletteEntries(HDC hdc
,
679 (void) hdc
; (void) iLayerPlane
; (void) iStart
; (void) cEntries
; (void) pcr
;
684 WINGDIAPI BOOL GLAPIENTRY
wglRealizeLayerPalette(HDC hdc
,
688 (void) hdc
; (void) iLayerPlane
; (void) bRealize
;
693 WINGDIAPI BOOL GLAPIENTRY
wglSwapLayerBuffers(HDC hdc
,
696 (void) hdc
; (void) fuPlanes
;
701 WINGDIAPI
const char * GLAPIENTRY
wglGetExtensionsStringARB(HDC hdc
)
703 return "WGL_ARB_extensions_string";