⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dglpf.c

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*
*                        Mesa 3-D graphics library
*                        Direct3D Driver Interface
*
*  ========================================================================
*
*   Copyright (C) 1991-2004 SciTech Software, Inc. All rights reserved.
*
*   Permission is hereby granted, free of charge, to any person obtaining a
*   copy of this software and associated documentation files (the "Software"),
*   to deal in the Software without restriction, including without limitation
*   the rights to use, copy, modify, merge, publish, distribute, sublicense,
*   and/or sell copies of the Software, and to permit persons to whom the
*   Software is furnished to do so, subject to the following conditions:
*
*   The above copyright notice and this permission notice shall be included
*   in all copies or substantial portions of the Software.
*
*   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
*   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
*   SCITECH SOFTWARE INC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
*   OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*   SOFTWARE.
*
*  ========================================================================
*
* Language:     ANSI C
* Environment:  Windows 9x (Win32)
*
* Description:  Pixel Formats.
*
****************************************************************************/

#include "dglpf.h"

#ifdef _USE_GLD3_WGL
#include "gld_driver.h"
#endif

// ***********************************************************************

char	szColorDepthWarning[] =
"GLDirect does not support the current desktop\n\
color depth.\n\n\
You may need to change the display resolution to\n\
16 bits per pixel or higher color depth using\n\
the Windows Display Settings control panel\n\
before running this OpenGL application.\n";

// ***********************************************************************
// This pixel format will be used as a template when compiling the list
// of pixel formats supported by the hardware. Many fields will be
// filled in at runtime.
// PFD flag defaults are upgraded to match ChoosePixelFormat() -- DaveM
DGL_pixelFormat pfTemplateHW =
{
    {
	sizeof(PIXELFORMATDESCRIPTOR),	// Size of the data structure
		1,							// Structure version - should be 1
									// Flags:
		PFD_DRAW_TO_WINDOW |		// The buffer can draw to a window or device surface.
		PFD_DRAW_TO_BITMAP |		// The buffer can draw to a bitmap. (DaveM)
		PFD_SUPPORT_GDI |			// The buffer supports GDI drawing. (DaveM)
		PFD_SUPPORT_OPENGL |		// The buffer supports OpenGL drawing.
		PFD_DOUBLEBUFFER |			// The buffer is double-buffered.
		0,							// Placeholder for easy commenting of above flags
		PFD_TYPE_RGBA,				// Pixel type RGBA.
		16,							// Total colour bitplanes (excluding alpha bitplanes)
		5, 0,						// Red bits, shift
		5, 5,						// Green bits, shift
		5, 10,						// Blue bits, shift
		0, 0,						// Alpha bits, shift (destination alpha)
		0,							// Accumulator bits (total)
		0, 0, 0, 0,					// Accumulator bits: Red, Green, Blue, Alpha
		0,							// Depth bits
		0,							// Stencil bits
		0,							// Number of auxiliary buffers
		0,							// Layer type
		0,							// Specifies the number of overlay and underlay planes.
		0,							// Layer mask
		0,							// Specifies the transparent color or index of an underlay plane.
		0							// Damage mask
	},
	-1,	// No depth/stencil buffer
};

// ***********************************************************************
// Return the count of the number of bits in a Bit Mask.
int BitCount(
	DWORD dw)
{
	int i;

	if (dw == 0)
		return 0;	// account for non-RGB mode

	for (i=0; dw; dw=dw>>1)
        i += (dw & 1);
    return i;
}

// ***********************************************************************

DWORD BitShift(
	DWORD dwMaskIn)
{
	DWORD dwShift, dwMask;

	if (dwMaskIn == 0)
		return 0;	// account for non-RGB mode

	for (dwShift=0, dwMask=dwMaskIn; !(dwMask&1); dwShift++, dwMask>>=1);

    return dwShift;
}

// ***********************************************************************

BOOL IsValidPFD(int iPFD)
{
	DGL_pixelFormat *lpPF;

	// Validate license
	if (!dglValidate())
		return FALSE;

	if ((glb.lpPF == NULL) ||
		(glb.nPixelFormatCount == 0))
		return FALSE;

	// Check PFD range
	if ( (iPFD < 1) || (iPFD > glb.nPixelFormatCount) ) {
		ddlogMessage(DDLOG_ERROR, "PFD out of range\n");
		return FALSE; // PFD is invalid
	}

	// Make a pointer to the pixel format
	lpPF = &glb.lpPF[iPFD-1];

	// Check size
	if (lpPF->pfd.nSize != sizeof(PIXELFORMATDESCRIPTOR)) {
		ddlogMessage(DDLOG_ERROR, "Bad PFD size\n");
		return FALSE; // PFD is invalid
	}

	// Check version
	if (lpPF->pfd.nVersion != 1) {
		ddlogMessage(DDLOG_ERROR, "PFD is not Version 1\n");
		return FALSE; // PFD is invalid
	}

	return TRUE; // PFD is valid
}

// ***********************************************************************

#ifndef _USE_GLD3_WGL

int		iEnumCount;			// Enumeration count
DWORD	dwDisplayBitDepth;	// Bit depth of current display mode

// ***********************************************************************

HRESULT WINAPI EnumDisplayModesCallback(
	DDSURFACEDESC2* pddsd,
	void *pvContext)
{
	DWORD			dwModeDepth;
	DDSURFACEDESC2	*lpDisplayMode;
	char			buf[32];

    // Check parameters
	if (pddsd == NULL)
		return DDENUMRET_CANCEL;

    dwModeDepth = pddsd->ddpfPixelFormat.dwRGBBitCount;
	lpDisplayMode = (DDSURFACEDESC2 *)pvContext;

	// Check mode for compatability with device.
	if (dwModeDepth != dwDisplayBitDepth)
		return DDENUMRET_OK;

	if (lpDisplayMode != NULL) {
		memcpy(&lpDisplayMode[iEnumCount], pddsd, sizeof(DDSURFACEDESC2));
		sprintf(buf, TEXT("Mode: %ld x %ld x %ld\n"),
				pddsd->dwWidth, pddsd->dwHeight, dwModeDepth);
		ddlogMessage(DDLOG_INFO, buf);
	}

	iEnumCount++;

	return DDENUMRET_OK;
}

// ***********************************************************************

HRESULT CALLBACK d3dEnumZBufferFormatsCallback(
	DDPIXELFORMAT* pddpf,
	VOID* lpZBufferPF )
{
	char buf[64];

	if(pddpf == NULL)
		return D3DENUMRET_CANCEL;

	if (pddpf->dwFlags & DDPF_ZBUFFER) {
		if (lpZBufferPF == NULL) {
			// Pass 1. Merely counting the PF
			glb.nZBufferPFCount++;
		} else {
			// Pass 2. Save the PF
			if (pddpf->dwFlags & DDPF_STENCILBUFFER) {
				sprintf(buf, " %d: Z=%d S=%d\n",
					iEnumCount,
					pddpf->dwZBufferBitDepth,
					pddpf->dwStencilBitDepth);
			} else {
				sprintf(buf, " %d: Z=%d S=0\n",
					iEnumCount,
					pddpf->dwZBufferBitDepth);
			}
			ddlogMessage(DDLOG_INFO, buf);

			memcpy(&glb.lpZBufferPF[iEnumCount++],
				pddpf,
				sizeof(DDPIXELFORMAT));
		}
	}

	return D3DENUMRET_OK;
}
#endif // _USE_GLD3_WGL

// ***********************************************************************

BOOL IsStencilSupportBroken(LPDIRECTDRAW4 lpDD4)
{
	DDDEVICEIDENTIFIER	dddi; // DX6 device identifier
	BOOL				bBroken = FALSE;

	// Microsoft really fucked up with the GetDeviceIdentifier function
	// on Windows 2000, since it locks up on stock driers on the CD. Updated
	// drivers from vendors appear to work, but we can't identify the drivers
	// without this function!!! For now we skip these tests on Windows 2000.
	if ((GetVersion() & 0x80000000UL) == 0)
		return FALSE;

	// Obtain device info
	if (FAILED(IDirectDraw4_GetDeviceIdentifier(lpDD4, &dddi, 0)))
		return FALSE;

	// Matrox G400 stencil buffer support does not draw anything in AutoCAD,
	// but ordinary Z buffers draw shaded models fine. (DaveM)
	if (dddi.dwVendorId == 0x102B) {		// Matrox
		if (dddi.dwDeviceId == 0x0525) {	// G400
			bBroken = TRUE;
		}
	}

	return bBroken;
}

// ***********************************************************************

void dglBuildPixelFormatList()
{
	int				i;
	char			buf[128];
	char			cat[8];
	DGL_pixelFormat	*lpPF;

#ifdef _USE_GLD3_WGL
	_gldDriver.BuildPixelformatList();
#else
	HRESULT			hRes;
	IDirectDraw		*lpDD1 = NULL;
	IDirectDraw4	*lpDD4 = NULL;
	IDirect3D3		*lpD3D3 = NULL;
	DDSURFACEDESC2	ddsdDisplayMode;

	DWORD			dwRb, dwGb, dwBb, dwAb; // Bit counts
	DWORD			dwRs, dwGs, dwBs, dwAs; // Bit shifts
	DWORD			dwPixelType;			// RGB or color index

	// Set defaults
	glb.nPixelFormatCount	= 0;
	glb.lpPF				= NULL;
	glb.nZBufferPFCount		= 0;
	glb.lpZBufferPF			= NULL;
	glb.nDisplayModeCount	= 0;
	glb.lpDisplayModes		= NULL;

	//
	// Examine the hardware for depth and stencil
	//

	if (glb.bPrimary)
		hRes = DirectDrawCreate(NULL, &lpDD1, NULL);
	else
		hRes = DirectDrawCreate(&glb.ddGuid, &lpDD1, NULL);
		
	if (FAILED(hRes)) {
		ddlogError(DDLOG_ERROR, "dglBPFL: DirectDrawCreate failed", hRes);
		return;
	}

	// Query for DX6 IDirectDraw4.
	hRes = IDirectDraw_QueryInterface(

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -