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. */
42 # if defined(BUILD_GL32)
43 # define WINGDIAPI __declspec(dllexport)
45 # define __W32API_USE_DLLIMPORT__
49 #include "GL/mesa_wgl.h"
60 #include "GL/wmesa.h" /* protos for wmesa* functions */
63 * Pixel Format Descriptors
66 /* Extend the PFD to include DB flag */
67 struct __pixelformat__
69 PIXELFORMATDESCRIPTOR pfd
;
70 GLboolean doubleBuffered
;
73 /* These are the PFD's supported by this driver. */
74 struct __pixelformat__ pfd
[] =
77 /* Double Buffer, alpha */
80 sizeof(PIXELFORMATDESCRIPTOR
), 1,
81 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
82 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
96 /* Single Buffer, alpha */
99 sizeof(PIXELFORMATDESCRIPTOR
), 1,
100 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
116 /* Double Buffer, no alpha */
119 sizeof(PIXELFORMATDESCRIPTOR
), 1,
120 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
121 PFD_GENERIC_FORMAT
|PFD_DOUBLEBUFFER
|PFD_SWAP_COPY
,
135 /* Single Buffer, no alpha */
138 sizeof(PIXELFORMATDESCRIPTOR
), 1,
139 PFD_DRAW_TO_WINDOW
|PFD_SUPPORT_OPENGL
|
156 int npfd
= sizeof(pfd
) / sizeof(pfd
[0]);
167 #define MESAWGL_CTX_MAX_COUNT 20
169 static MesaWglCtx wgl_ctx
[MESAWGL_CTX_MAX_COUNT
];
171 static unsigned ctx_count
= 0;
172 static int ctx_current
= -1;
173 static unsigned curPFD
= 0;
175 static HDC CurrentHDC
= 0;
178 WINGDIAPI HGLRC GLAPIENTRY
wglCreateContext(HDC hdc
)
182 for(i
=0;i
<MESAWGL_CTX_MAX_COUNT
;i
++) {
183 wgl_ctx
[i
].ctx
= NULL
;
186 for( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
187 if ( wgl_ctx
[i
].ctx
== NULL
) {
189 WMesaCreateContext(hdc
, NULL
, (GLboolean
)GL_TRUE
,
190 (GLboolean
) (pfd
[curPFD
-1].doubleBuffered
?
192 (GLboolean
)(pfd
[curPFD
-1].pfd
.cAlphaBits
?
193 GL_TRUE
: GL_FALSE
) );
194 if (wgl_ctx
[i
].ctx
== NULL
)
197 return ((HGLRC
)wgl_ctx
[i
].ctx
);
204 WINGDIAPI BOOL GLAPIENTRY
wglDeleteContext(HGLRC hglrc
)
207 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
208 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
){
209 WMesaMakeCurrent((WMesaContext
) hglrc
, NULL
);
210 WMesaDestroyContext(wgl_ctx
[i
].ctx
);
211 wgl_ctx
[i
].ctx
= NULL
;
220 WINGDIAPI HGLRC GLAPIENTRY
wglGetCurrentContext(VOID
)
225 return (HGLRC
) wgl_ctx
[ctx_current
].ctx
;
228 WINGDIAPI HDC GLAPIENTRY
wglGetCurrentDC(VOID
)
233 WINGDIAPI BOOL GLAPIENTRY
wglMakeCurrent(HDC hdc
, HGLRC hglrc
)
239 if (!hdc
|| !hglrc
) {
240 WMesaMakeCurrent(NULL
, NULL
);
245 for ( i
= 0; i
< MESAWGL_CTX_MAX_COUNT
; i
++ ) {
246 if ( wgl_ctx
[i
].ctx
== (WMesaContext
) hglrc
) {
247 WMesaMakeCurrent( (WMesaContext
) hglrc
, hdc
);
256 WINGDIAPI
int GLAPIENTRY
wglChoosePixelFormat(HDC hdc
,
258 PIXELFORMATDESCRIPTOR
*ppfd
)
260 int i
,best
= -1,bestdelta
= 0x7FFFFFFF,delta
;
263 if(ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
) || ppfd
->nVersion
!= 1)
268 for(i
= 0; i
< npfd
;i
++)
272 (ppfd
->dwFlags
& PFD_DRAW_TO_WINDOW
) &&
273 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_WINDOW
))
276 (ppfd
->dwFlags
& PFD_DRAW_TO_BITMAP
) &&
277 !(pfd
[i
].pfd
.dwFlags
& PFD_DRAW_TO_BITMAP
))
280 (ppfd
->dwFlags
& PFD_SUPPORT_GDI
) &&
281 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_GDI
))
284 (ppfd
->dwFlags
& PFD_SUPPORT_OPENGL
) &&
285 !(pfd
[i
].pfd
.dwFlags
& PFD_SUPPORT_OPENGL
))
288 !(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
289 ((ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
290 (pfd
[i
].pfd
.dwFlags
& PFD_DOUBLEBUFFER
)))
293 !(ppfd
->dwFlags
& PFD_STEREO_DONTCARE
) &&
294 ((ppfd
->dwFlags
& PFD_STEREO
) !=
295 (pfd
[i
].pfd
.dwFlags
& PFD_STEREO
)))
297 if(ppfd
->iPixelType
!= pfd
[i
].pfd
.iPixelType
)
299 if(ppfd
->cAlphaBits
!= pfd
[i
].pfd
.cAlphaBits
)
301 if(delta
< bestdelta
)
317 WINGDIAPI
int GLAPIENTRY
wglDescribePixelFormat(HDC hdc
,
320 LPPIXELFORMATDESCRIPTOR ppfd
)
326 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
327 nBytes
!= sizeof(PIXELFORMATDESCRIPTOR
))
332 *ppfd
= pfd
[iPixelFormat
- 1].pfd
;
336 WINGDIAPI PROC GLAPIENTRY
wglGetProcAddress(LPCSTR lpszProc
)
338 PROC p
= (PROC
) _glapi_get_proc_address((const char *) lpszProc
);
346 WINGDIAPI
int GLAPIENTRY
wglGetPixelFormat(HDC hdc
)
356 WINGDIAPI BOOL GLAPIENTRY
wglSetPixelFormat(HDC hdc
,int iPixelFormat
,
357 const PIXELFORMATDESCRIPTOR
*ppfd
)
361 if(iPixelFormat
< 1 || iPixelFormat
> npfd
||
362 ppfd
->nSize
!= sizeof(PIXELFORMATDESCRIPTOR
)) {
366 curPFD
= iPixelFormat
;
370 WINGDIAPI BOOL GLAPIENTRY
wglSwapBuffers(HDC hdc
)
372 WMesaSwapBuffers(hdc
);
376 static FIXED
FixedFromDouble(double d
)
378 long l
= (long) (d
* 65536L);
379 return *(FIXED
*) (void *) &l
;
384 ** This is cribbed from FX/fxwgl.c, and seems to implement support
385 ** for bitmap fonts where the wglUseFontBitmapsA() code implements
386 ** support for outline fonts. In combination they hopefully give
387 ** fairly generic support for fonts.
389 static BOOL
wglUseFontBitmaps_FX(HDC fontDevice
, DWORD firstChar
,
390 DWORD numChars
, DWORD listBase
)
400 VERIFY(GetTextMetrics(fontDevice
, &metric
));
402 dibInfo
= (BITMAPINFO
*) calloc(sizeof(BITMAPINFO
) + sizeof(RGBQUAD
), 1);
403 dibInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
404 dibInfo
->bmiHeader
.biPlanes
= 1;
405 dibInfo
->bmiHeader
.biBitCount
= 1;
406 dibInfo
->bmiHeader
.biCompression
= BI_RGB
;
408 bitDevice
= CreateCompatibleDC(fontDevice
);
410 /* Swap fore and back colors so the bitmap has the right polarity */
411 tempColor
= GetBkColor(bitDevice
);
412 SetBkColor(bitDevice
, GetTextColor(bitDevice
));
413 SetTextColor(bitDevice
, tempColor
);
415 /* Place chars based on base line */
416 VERIFY(SetTextAlign(bitDevice
, TA_BASELINE
) != GDI_ERROR
? 1 : 0);
418 for(i
= 0; i
< (int)numChars
; i
++) {
421 int charWidth
,charHeight
,bmapWidth
,bmapHeight
,numBytes
,res
;
426 curChar
= (char)(i
+ firstChar
);
428 /* Find how high/wide this character is */
429 VERIFY(GetTextExtentPoint32(bitDevice
, &curChar
, 1, &size
));
431 /* Create the output bitmap */
433 charHeight
= size
.cy
;
434 /* Round up to the next multiple of 32 bits */
435 bmapWidth
= ((charWidth
+ 31) / 32) * 32;
436 bmapHeight
= charHeight
;
437 bitObject
= CreateCompatibleBitmap(bitDevice
,
440 /* VERIFY(bitObject); */
442 /* Assign the output bitmap to the device */
443 origBmap
= SelectObject(bitDevice
, bitObject
);
444 (void) VERIFY(origBmap
);
446 VERIFY( PatBlt( bitDevice
, 0, 0, bmapWidth
, bmapHeight
,BLACKNESS
) );
448 /* Use our source font on the device */
449 VERIFY(SelectObject(bitDevice
, GetCurrentObject(fontDevice
,OBJ_FONT
)));
451 /* Draw the character */
452 VERIFY(TextOut(bitDevice
, 0, metric
.tmAscent
, &curChar
, 1));
454 /* Unselect our bmap object */
455 VERIFY(SelectObject(bitDevice
, origBmap
));
457 /* Convert the display dependant representation to a 1 bit deep DIB */
458 numBytes
= (bmapWidth
* bmapHeight
) / 8;
459 bmap
= malloc(numBytes
);
460 dibInfo
->bmiHeader
.biWidth
= bmapWidth
;
461 dibInfo
->bmiHeader
.biHeight
= bmapHeight
;
462 res
= GetDIBits(bitDevice
, bitObject
, 0, bmapHeight
, bmap
,
467 /* Create the GL object */
468 glNewList(i
+ listBase
, GL_COMPILE
);
469 glBitmap(bmapWidth
, bmapHeight
, 0.0, (GLfloat
)metric
.tmDescent
,
470 (GLfloat
)charWidth
, 0.0,
475 /* Destroy the bmap object */
476 DeleteObject(bitObject
);
478 /* Deallocate the bitmap data */
483 VERIFY(DeleteDC(bitDevice
));
491 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsA(HDC hdc
, DWORD first
,
492 DWORD count
, DWORD listBase
)
506 font_list
= listBase
;
508 mat
.eM11
= FixedFromDouble(1);
509 mat
.eM12
= FixedFromDouble(0);
510 mat
.eM21
= FixedFromDouble(0);
511 mat
.eM22
= FixedFromDouble(-1);
513 memset(&gm
,0,sizeof(gm
));
516 ** If we can't get the glyph outline, it may be because this is a fixed
517 ** font. Try processing it that way.
519 if( GetGlyphOutline(hdc
, first
, GGO_BITMAP
, &gm
, 0, NULL
, &mat
)
521 return wglUseFontBitmaps_FX( hdc
, first
, count
, listBase
);
525 ** Otherwise process all desired characters.
527 for (i
= 0; i
< (int)count
; i
++) {
530 glNewList( font_list
+i
, GL_COMPILE
);
532 /* allocate space for the bitmap/outline */
533 size
= GetGlyphOutline(hdc
, first
+ i
, GGO_BITMAP
,
535 if (size
== GDI_ERROR
) {
537 err
= GetLastError();
542 hBits
= GlobalAlloc(GHND
, size
+1);
543 lpBits
= GlobalLock(hBits
);
546 GetGlyphOutline(hdc
, /* handle to device context */
547 first
+ i
, /* character to query */
548 GGO_BITMAP
, /* format of data to return */
549 &gm
, /* ptr to structure for metrics*/
550 size
, /* size of buffer for data */
551 lpBits
, /* pointer to buffer for data */
552 &mat
/* pointer to transformation */
553 /* matrix structure */
556 if (err
== GDI_ERROR
) {
561 err
= GetLastError();
566 glBitmap(gm
.gmBlackBoxX
,gm
.gmBlackBoxY
,
567 (GLfloat
)-gm
.gmptGlyphOrigin
.x
,
568 (GLfloat
)gm
.gmptGlyphOrigin
.y
,
569 (GLfloat
)gm
.gmCellIncX
,
570 (GLfloat
)gm
.gmCellIncY
,
571 (const GLubyte
* )lpBits
);
584 /* NOT IMPLEMENTED YET */
585 WINGDIAPI BOOL GLAPIENTRY
wglCopyContext(HGLRC hglrcSrc
,
589 (void) hglrcSrc
; (void) hglrcDst
; (void) mask
;
593 WINGDIAPI HGLRC GLAPIENTRY
wglCreateLayerContext(HDC hdc
,
596 (void) hdc
; (void) iLayerPlane
;
601 WINGDIAPI BOOL GLAPIENTRY
wglShareLists(HGLRC hglrc1
,
604 WMesaShareLists(hglrc1
, hglrc2
);
609 WINGDIAPI BOOL GLAPIENTRY
wglUseFontBitmapsW(HDC hdc
,
614 (void) hdc
; (void) first
; (void) count
; (void) listBase
;
618 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesA(HDC hdc
,
625 LPGLYPHMETRICSFLOAT lpgmf
)
627 (void) hdc
; (void) first
; (void) count
;
628 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
634 WINGDIAPI BOOL GLAPIENTRY
wglUseFontOutlinesW(HDC hdc
,
641 LPGLYPHMETRICSFLOAT lpgmf
)
643 (void) hdc
; (void) first
; (void) count
;
644 (void) listBase
; (void) deviation
; (void) extrusion
; (void) format
;
650 WINGDIAPI BOOL GLAPIENTRY
wglDescribeLayerPlane(HDC hdc
,
654 LPLAYERPLANEDESCRIPTOR plpd
)
656 (void) hdc
; (void) iPixelFormat
; (void) iLayerPlane
;
657 (void) nBytes
; (void) plpd
;
662 WINGDIAPI
int GLAPIENTRY
wglSetLayerPaletteEntries(HDC hdc
,
668 (void) hdc
; (void) iLayerPlane
; (void) iStart
;
669 (void) cEntries
; (void) pcr
;
674 WINGDIAPI
int GLAPIENTRY
wglGetLayerPaletteEntries(HDC hdc
,
680 (void) hdc
; (void) iLayerPlane
; (void) iStart
; (void) cEntries
; (void) pcr
;
685 WINGDIAPI BOOL GLAPIENTRY
wglRealizeLayerPalette(HDC hdc
,
689 (void) hdc
; (void) iLayerPlane
; (void) bRealize
;
694 WINGDIAPI BOOL GLAPIENTRY
wglSwapLayerBuffers(HDC hdc
,
697 (void) hdc
; (void) fuPlanes
;
702 WINGDIAPI
const char * GLAPIENTRY
wglGetExtensionsStringARB(HDC hdc
)
704 return "WGL_ARB_extensions_string";