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

📄 misc.c

📁 国外游戏开发者杂志1997年第九期配套代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  Copyright (C) 1995, 1996 Microsoft Corporation. All Rights Reserved.
 *
 *  File: misc.c
 *
 *  Miscellaneous functions not involving DD and D3D.  Part of D3DApp.
 *
 *  D3DApp is a collection of helper functions for Direct3D applications.
 *  D3DApp consists of the following files:
 *	d3dapp.h    Main D3DApp header to be included by application
 *      d3dappi.h   Internal header
 *      d3dapp.c    D3DApp functions seen by application.
 *      ddcalls.c   All calls to DirectDraw objects except textures
 *      d3dcalls.c  All calls to Direct3D objects except textures
 *      texture.c   Texture loading and managing texture list
 *      misc.c	    Miscellaneous calls
 */

#include "d3dappi.h"

/***************************************************************************/
/*                          Setting Defaults                               */
/***************************************************************************/
/*
 * D3DAppISetDefaults
 * Set all the global variables to their default values.  Do not reset the
 * image files.
 */
void
D3DAppISetDefaults(void)
{
    int n;
    char backup[D3DAPP_MAXTEXTURES][50];

    n = d3dappi.NumTextures;
    memcpy(&backup[0][0], &d3dappi.ImageFile[0][0], 50 * D3DAPP_MAXTEXTURES);
    ZEROMEM(d3dappi);
    memcpy(&d3dappi.ImageFile[0][0], &backup[0][0], 50 * D3DAPP_MAXTEXTURES);
    d3dappi.NumTextures = n;
    d3dappi.bAppActive = TRUE;
    ZEROMEM(d3dapprs);
    d3dapprs.bZBufferOn = FALSE; //TRUE;
    d3dapprs.bPerspCorrect = TRUE;
    d3dapprs.ShadeMode = D3DSHADE_GOURAUD;
    d3dapprs.TextureFilter = D3DFILTER_LINEAR; //D3DFILTER_NEAREST;
    d3dapprs.TextureBlend = D3DTBLEND_MODULATE;
    d3dapprs.FillMode = D3DFILL_SOLID;
    d3dapprs.bDithering = FALSE;
    d3dapprs.bSpecular = FALSE;		//TRUE;
    d3dapprs.bAntialiasing = FALSE;
    d3dapprs.bFogEnabled = FALSE;
    d3dapprs.FogColor = RGB_MAKE(0, 0, 0);
    d3dapprs.FogMode = D3DFOG_LINEAR;
    d3dapprs.FogStart = D3DVAL(6.0);
    d3dapprs.FogEnd = D3DVAL(11.0);

    lpClipper = NULL;
    lpPalette = NULL;
    bPrimaryPalettized = FALSE;
    bPaletteActivate = FALSE;
    bIgnoreWM_SIZE = FALSE;
    ZEROMEM(ppe);
    ZEROMEM(Originalppe);
    LastError = DD_OK;
    ZEROMEM(LastErrorString);
    D3DDeviceDestroyCallback = NULL;
    D3DDeviceDestroyCallbackContext = NULL;
    D3DDeviceCreateCallback = NULL;
    D3DDeviceCreateCallbackContext = NULL;
}

/***************************************************************************/
/*                Calling Device Create And Destroy Callbacks              */
/***************************************************************************/
BOOL
D3DAppICallDeviceDestroyCallback(void)
{
    if (D3DDeviceDestroyCallback) {
	if (CallbackRefCount) {
	    --CallbackRefCount;
	    return (D3DDeviceDestroyCallback)(D3DDeviceDestroyCallbackContext);
	}
    }
    return TRUE;
}

BOOL
D3DAppICallDeviceCreateCallback(int w, int h)
{
    if (D3DDeviceCreateCallback) {
	++CallbackRefCount;
	return (D3DDeviceCreateCallback)(w, h, &d3dappi.lpD3DViewport,
					 D3DDeviceCreateCallbackContext);
    }
    return TRUE;
}

/***************************************************************************/
/*            Choosing and verifying the driver and display mode           */
/***************************************************************************/
/*
 * D3DAppIPickDriver
 * Choose a driver from the list of available drivers which can render to one
 * of the given depths.  Hardware is prefered.  Mono-lighting drivers are
 * prefered over RGB.
 */
BOOL
D3DAppIPickDriver(int* driver, DWORD depths)
{
    int i, j;
    j = 0;
    for (i = 0; i < d3dappi.NumDrivers; i++)
	if (d3dappi.Driver[i].Desc.dwDeviceRenderBitDepth & depths)
	    break;
    if (i >= d3dappi.NumDrivers) {
	*driver = D3DAPP_BOGUS;
	return TRUE;
    }
    j = i;
    for (i = 0; i < d3dappi.NumDrivers; i++) {
	if (d3dappi.Driver[i].Desc.dwDeviceRenderBitDepth & depths) {
	    if (d3dappi.Driver[i].bIsHardware &&
					      !d3dappi.Driver[j].bIsHardware)
					          j = i;
	    else if (d3dappi.Driver[i].bIsHardware ==
					     d3dappi.Driver[j].bIsHardware) {
		if (d3dappi.Driver[i].Desc.dcmColorModel & D3DCOLOR_MONO &&
		    !(d3dappi.Driver[j].Desc.dcmColorModel & D3DCOLOR_MONO))
			j = i;
	    }
	}
    }
    if (j >= d3dappi.NumDrivers)
	*driver = D3DAPP_BOGUS;
    else
	*driver = j;
    return TRUE;
}

/*
 * D3DAppIFilterDisplayModes
 * Set the bThisDriverCanDo flag for each display mode if the given driver
 * can render in that depth.  Also checks to make sure there is enough
 * total video memory for front/back/z-buffer in video memory if it's a
 * hardware device.
 */
BOOL
D3DAppIFilterDisplayModes(int driver)
{
    int i;
    DWORD depths = d3dappi.Driver[driver].Desc.dwDeviceRenderBitDepth;

    for (i = 0; i < d3dappi.NumModes; i++) {
	d3dappi.Mode[i].bThisDriverCanDo = FALSE;
	if (!(D3DAppIBPPToDDBD(d3dappi.Mode[i].bpp) & depths))
	    continue;
	d3dappi.Mode[i].bThisDriverCanDo = TRUE;
	    
    }
    d3dappi.ThisMode.bThisDriverCanDo =
			     d3dappi.Mode[d3dappi.CurrMode].bThisDriverCanDo;
    return TRUE;
}

/*
 * D3DAppIPickDisplayMode
 * Pick a display mode of one of the given depths.  640x480x16 is prefered.
 */
BOOL
D3DAppIPickDisplayMode(int *mode, DWORD depths)
{
    int i, j;
    for (i = 0; i < d3dappi.NumModes; i++)
	if (D3DAppIBPPToDDBD(d3dappi.Mode[i].bpp) & depths)
	    break;
    j = i;
    for (; i < d3dappi.NumModes; i++) {
	if (!(D3DAppIBPPToDDBD(d3dappi.Mode[i].bpp) & depths))
	    continue;
	if (d3dappi.Mode[i].w == 640 && d3dappi.Mode[i].h == 480 &&
	    d3dappi.Mode[i].bpp == 16) {
	    j = i;
	    break;
	}
    }
    if (j >= d3dappi.NumModes)
	*mode = D3DAPP_BOGUS;
    else
	*mode = j;
    return TRUE;
}

/*
 * D3DAppIVerifyDriverAndMode
 * Verifies the selected driver and mode combination.  If the driver is
 * specified, the mode will be changed to accomodate the driver if it's not
 * compatible.  If the driver is not specified, one will be selected which is
 * compatible with the specified mode.  If neither are specified, a suitable
 * pair will be returned.
 */
BOOL
D3DAppIVerifyDriverAndMode(int* lpdriver, int* lpmode)
{
    DWORD depths;
    int driver, mode, i;
    driver = *lpdriver; mode = *lpmode;

    if (mode == D3DAPP_USEWINDOW && !d3dappi.bIsPrimary) {
	D3DAppISetErrorString("Cannot render to a window when the DirectDraw device is not the primary.\n");
	goto exit_with_error;
    }

    /*
     * If I've been ask to choose a driver, choose one which is compatible
     * with the specified mode.
     */
    if (driver == D3DAPP_YOUDECIDE) {	
	if (mode == D3DAPP_USEWINDOW) {
	    /*
	     * I must find a driver for this display depth
	     */
	    depths = D3DAppIBPPToDDBD(d3dappi.WindowsDisplay.bpp);
	    ATTEMPT(D3DAppIPickDriver(&driver, depths));
	    if (driver == D3DAPP_BOGUS) {
		D3DAppISetErrorString("Cannot find a D3D device driver which is compatible with the current display depth.\n");
		goto exit_with_error;
	    }
	    /*
	     * I don't need to go through the mode selection since I've
	     * verified it here
	     */
	    goto ret_ok;
	} else if (mode == D3DAPP_YOUDECIDE) {
	    /*
	     * I'm free to choose any driver which can use even one
	     * supported depth
	     */
	    if (d3dappi.bIsPrimary)
		depths = D3DAppIBPPToDDBD(d3dappi.WindowsDisplay.bpp);
	    else
		depths = 0;
	    for (i = 0; i < d3dappi.NumModes; i++)
		depths |= D3DAppIBPPToDDBD(d3dappi.Mode[i].bpp);
	    ATTEMPT(D3DAppIPickDriver(&driver, depths));
	    if (driver == D3DAPP_BOGUS) {
		D3DAppISetErrorString("Cannot find a D3D device driver which is compatible with the current display depth or any supported fullscreen mode.\n");
		goto exit_with_error;
	    }
	    /*
	     * The mode will be chosen in the next section
	     */
	} else {
	    /*
	     * Must pick a driver which uses the given mode depth
	     */
	    ATTEMPT(D3DAppIPickDriver(&driver,
				  D3DAppIBPPToDDBD(d3dappi.Mode[mode].bpp)));
	    if (driver == D3DAPP_BOGUS) {
		D3DAppISetErrorString("Cannot find a D3D device driver which is compatible with the specified fullscreen mode.\n");
		goto exit_with_error;
	    }
	    /*
	     * I don't need to go through the mode selection since I've
	     * verified it here
	     */
	    goto ret_ok;
	}
    }

    /* 
     * At this stage, I have a driver I want to match the mode to.
     */
    if (mode == D3DAPP_YOUDECIDE) {
	/*
	 * If it's my choice, I prefer windowed over fullscreen
	 */
#if 0		// choose fullscreen over windowed! (HB)
	if (d3dappi.bIsPrimary) {
	    if (D3DAppIBPPToDDBD(d3dappi.WindowsDisplay.bpp) & 
		    d3dappi.Driver[driver].Desc.dwDeviceRenderBitDepth) {
		mode = D3DAPP_USEWINDOW;
		goto ret_ok;
	    }
	}
#endif
	/*
	 * Either this is not a primary DD device or the driver cannot use
	 * the Windows display depth
	 */
	ATTEMPT(D3DAppIPickDisplayMode(&mode,
			d3dappi.Driver[driver].Desc.dwDeviceRenderBitDepth));
	if (mode == D3DAPP_BOGUS) {
	    D3DAppISetErrorString("The selected D3D device driver is not compatible with the current display depth or any supported fullscreen modes.\n");
	    goto exit_with_error;
	}

⌨️ 快捷键说明

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