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

📄 ddcalls.c

📁 国外游戏开发者杂志1997年第九期配套代码
💻 C
📖 第 1 页 / 共 3 页
字号:
     */
    if (d3dappi.bFullscreen) {
	D3DAppIValidateDirtyRects();
	D3DAppCheckForLostSurfaces();
	d3dappi.bMinimized = FALSE;
	*lresult = DefWindowProc(hwnd, message, wParam, lParam);
	return TRUE;
    }
    /*
     * If we are minimized, this is the un-minimized size message.
     */
    if (d3dappi.bMinimized) {
	/*
	 * Restore our surfaces and update the dirty rectangle info
	 */
	D3DAppIValidateDirtyRects();
	D3DAppCheckForLostSurfaces();
	d3dappi.bMinimized = FALSE;
	*lresult = DefWindowProc(hwnd, message, wParam, lParam);
	return TRUE;
    }
    /*
     * Since we are still here, this must be a regular, window resize
     * message.  A new viewport will definitely be needed, but the
     * device and buffers will only be re-created if they have gotten bigger
     * or change size by a very large amount.
     */
    D3DAppIGetClientWin(hwnd);
    w = LOWORD(lParam);
    h = HIWORD(lParam);
    /*
     * If w and h are under the minimum, create buffers of the minimum size
     */
    if (w < D3DAPP_WINDOWMINIMUM)
        w = D3DAPP_WINDOWMINIMUM;
    if (h < D3DAPP_WINDOWMINIMUM)
        h = D3DAPP_WINDOWMINIMUM;
    /*
     * Destroy the viewport and all execute buffers
     */
    d3dappi.bRenderingIsOK = FALSE;
    ATTEMPT(D3DAppICallDeviceDestroyCallback());
    /*
     * Only create a new device and buffers if they changed significantly,
     * otherwise just make sure the old buffers aren't lost.
     */
    if ((w > szBuffers.cx || h > szBuffers.cy) ||
	(w < szBuffers.cx / 2 || h < szBuffers.cy / 2)) {
	/*
	 * Release the device
	 */
	RELEASE(d3dappi.lpD3DDevice);
	/*
	 * Release the old buffers
	 */
	RELEASE(d3dappi.lpZBuffer);
	RELEASE(lpPalette);
	RELEASE(lpClipper);
	RELEASE(d3dappi.lpBackBuffer);
	RELEASE(d3dappi.lpFrontBuffer);
	/*
	 * Create new ones
	 */
	ATTEMPT(D3DAppICreateBuffers(hwnd, w, h, D3DAPP_BOGUS, FALSE, d3dappi.ThisDriver.bIsHardware));
	ATTEMPT(D3DAppICheckForPalettized());
	ATTEMPT(D3DAppICreateZBuffer(w, h, d3dappi.CurrDriver));
	/*
	 * Create the driver
	 */
	ATTEMPT(D3DAppICreateDevice(d3dappi.CurrDriver));
	/*
	 * Since the driver did not change, the texture surfaces are still valid.
	 * We just need to get new handles.
	 */
	if (d3dappi.ThisDriver.bDoesTextures) {
	    for (i = 0; i < d3dappi.NumUsableTextures; i++) {
		D3DAppIGetTextureHandle(i);
	    }
	}
    } else {
	D3DAppCheckForLostSurfaces();
    }
    /*
     * Call the device create callback to create the viewport, set the render
     * state and clear the dirty rectangle info
     */
    ATTEMPT(D3DAppICallDeviceCreateCallback(w, h));
    ATTEMPT(D3DAppISetRenderState());
    D3DAppIValidateDirtyRects();
    d3dappi.bRenderingIsOK = TRUE;
    /*
     * Call the default window proc
     */
    *lresult = DefWindowProc(hwnd, message, wParam, lParam);
    return TRUE;
exit_with_error:
    D3DAppICallDeviceDestroyCallback();
    RELEASE(d3dappi.lpD3DDevice);
    RELEASE(d3dappi.lpZBuffer);
    RELEASE(lpPalette);
    RELEASE(lpClipper);
    RELEASE(d3dappi.lpBackBuffer);
    RELEASE(d3dappi.lpFrontBuffer);
    return FALSE;
}

/***************************************************************************/
/*              Setting the display mode and cooperative level             */
/***************************************************************************/
/*
 * D3DAppISetCoopLevel
 * Set the cooperative level to exclusive mode for fullscreen and normal for
 * a window.  Set the bIgnoreWM_SIZE flag because SetCooperativeLevel
 * generates a WM_SIZE message you do not have to resize the buffers on.
 */
BOOL
D3DAppISetCoopLevel(HWND hwnd, BOOL bFullscreen)
{
    if (bFullscreen) {
	bIgnoreWM_SIZE = TRUE;
	LastError = d3dappi.lpDD->lpVtbl->SetCooperativeLevel(d3dappi.lpDD,
				   hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
	bIgnoreWM_SIZE = FALSE;
	if(LastError != DD_OK ) {
	    D3DAppISetErrorString("SetCooperativeLevel to fullscreen failed.\n%s",
				  D3DAppErrorToString(LastError));
	    return FALSE;
	}
	d3dappi.bFullscreen = TRUE;
    } else {
	bIgnoreWM_SIZE = TRUE;
        LastError = d3dappi.lpDD->lpVtbl->SetCooperativeLevel(d3dappi.lpDD,
							 hwnd, DDSCL_NORMAL);
	bIgnoreWM_SIZE = FALSE;
	if(LastError != DD_OK ) {
            D3DAppISetErrorString("SetCooperativeLevel to normal failed.\n%s",
				  D3DAppErrorToString(LastError));
            return FALSE;
        }
	d3dappi.bFullscreen = FALSE;
    }
    return TRUE;
}

/*
 * D3DAppISetDisplayMode
 * Set the display mode to the given dimensions and bits per pixel.  The
 * bIgnoreWM_SIZE message is set because the display change generates a
 * WM_SIZE message which we don't want to resize the buffers on.
 */
BOOL
D3DAppISetDisplayMode(int w, int h, int bpp)
{
    d3dappi.ThisMode.w = w; d3dappi.ThisMode.h = h;
    d3dappi.ThisMode.bpp = bpp;
    bIgnoreWM_SIZE = TRUE;
    LastError = d3dappi.lpDD->lpVtbl->SetDisplayMode(d3dappi.lpDD, w, h,
						     bpp);
    bIgnoreWM_SIZE = FALSE;
    if(LastError != DD_OK ) {
        D3DAppISetErrorString("SetDisplayMode to %dx%dx%d failed\n%s",
			      w, h, bpp, D3DAppErrorToString(LastError));
        return FALSE;
    }
    return TRUE;
}

/*
 * D3DAppIRestoreDispMode
 * Restores the display mode to the current windows display mode.  The
 * bIgnoreWM_SIZE message is set because the display change generates a
 * WM_SIZE message which we don't want to resize the buffers on.
 */
BOOL
D3DAppIRestoreDispMode(void)
{
    bIgnoreWM_SIZE = TRUE;
    LastError = d3dappi.lpDD->lpVtbl->RestoreDisplayMode(d3dappi.lpDD);
    if (LastError != DD_OK) {
	D3DAppISetErrorString("RestoreDisplayMode failed.\n%s",
			      D3DAppErrorToString(LastError));
	return FALSE;
    }
    bIgnoreWM_SIZE = FALSE;
    return TRUE;
}

/*
 * D3DAppRememberWindowsMode
 * Record the current display mode in d3dappi.WindowsDisplay
 */
BOOL
D3DAppIRememberWindowsMode(void)
{
    DDSURFACEDESC ddsd;

    memset(&ddsd, 0, sizeof(DDSURFACEDESC));
    ddsd.dwSize = sizeof(DDSURFACEDESC);
    LastError = d3dappi.lpDD->lpVtbl->GetDisplayMode(d3dappi.lpDD, &ddsd);
    if (LastError != DD_OK) {
	D3DAppISetErrorString("Getting the current display mode failed.\n%s",
			      D3DAppErrorToString(LastError));
	return FALSE;
    }
    d3dappi.WindowsDisplay.w = ddsd.dwWidth;
    d3dappi.WindowsDisplay.h = ddsd.dwHeight;
    d3dappi.WindowsDisplay.bpp = ddsd.ddpfPixelFormat.dwRGBBitCount;
    return TRUE;
}

/***************************************************************************/
/*                          Misc DD Utilities                              */
/***************************************************************************/

/*
 * D3DAppIClearBuffers
 * Clear the front and back buffers to black
 */
BOOL
D3DAppIClearBuffers(void)
{
    DDSURFACEDESC ddsd;
    RECT dst;
    DDBLTFX ddbltfx;
    /*
     * Find the width and height of the front buffer by getting its
     * DDSURFACEDESC
     */
    if (d3dappi.lpFrontBuffer) {
	LastError = D3DAppIGetSurfDesc(&ddsd, d3dappi.lpFrontBuffer);
	if (LastError != DD_OK) {
	    D3DAppISetErrorString("Failure getting the surface description of the front buffer before clearing.\n%s",
				  D3DAppErrorToString(LastError));
	    return FALSE;
	}
	/*
	 * Clear the front buffer to black
	 */
	memset(&ddbltfx, 0, sizeof(ddbltfx));
	ddbltfx.dwSize = sizeof(DDBLTFX);
	SetRect(&dst, 0, 0, ddsd.dwWidth, ddsd.dwHeight);
	LastError = d3dappi.lpFrontBuffer->lpVtbl->Blt(d3dappi.lpFrontBuffer,
						    &dst, NULL, NULL, 
						    DDBLT_COLORFILL | DDBLT_WAIT,
						    &ddbltfx);
	if (LastError != DD_OK) {
	    D3DAppISetErrorString("Clearing the front buffer failed.\n%s",
				  D3DAppErrorToString(LastError));
	    return FALSE;
	}
    }
    if (d3dappi.lpBackBuffer) {
	/*
	 * Find the width and height of the back buffer by getting its
	 * DDSURFACEDESC
	 */
	LastError = D3DAppIGetSurfDesc(&ddsd, d3dappi.lpBackBuffer);
	if (LastError != DD_OK) {
	    D3DAppISetErrorString("Failure while getting the surface description of the back buffer before clearing.\n%s",
				  D3DAppErrorToString(LastError));
	    return FALSE;
	}
	/*
	 * Clear the back buffer to black
	 */
	memset(&ddbltfx, 0, sizeof(ddbltfx));
	ddbltfx.dwSize = sizeof(DDBLTFX);
	SetRect(&dst, 0, 0, ddsd.dwWidth, ddsd.dwHeight);
	LastError = d3dappi.lpBackBuffer->lpVtbl->Blt(d3dappi.lpBackBuffer, &dst,
						     NULL, NULL,
						     DDBLT_COLORFILL | DDBLT_WAIT,
						     &ddbltfx);
	if (LastError != DD_OK) {
	    D3DAppISetErrorString("Clearing the front buffer failed.\n%s",
				  D3DAppErrorToString(LastError));
	    return FALSE;
	}
    }
    return TRUE;
}

/*
 * D3DAppIBPPToDDBD
 * Convert an integer bit per pixel number to a DirectDraw bit depth flag
 */
DWORD
D3DAppIBPPToDDBD(int bpp)
{
    switch(bpp) {
	case 1:
	    return DDBD_1;
	case 2:
	    return DDBD_2;
	case 4:
	    return DDBD_4;
	case 8:
	    return DDBD_8;
	case 16:
	    return DDBD_16;
	case 24:
	    return DDBD_24;
	case 32:
	    return DDBD_32;
	default:
	    return (DWORD)D3DAPP_BOGUS;
    }
}

/*
 * D3DAppTotalVideoMemory
 * Returns the amount of total video memory supported (not free)
 */
DWORD
D3DAppTotalVideoMemory(void)
{
    DDCAPS DriverCaps, HELCaps;
    memset (&DriverCaps, 0, sizeof(DDCAPS));
    DriverCaps.dwSize = sizeof(DDCAPS);
    memset (&HELCaps, 0, sizeof(DDCAPS));
    HELCaps.dwSize = sizeof(DDCAPS);
    LastError = d3dappi.lpDD->lpVtbl->GetCaps(d3dappi.lpDD, &DriverCaps,
					      &HELCaps);
    if (LastError != DD_OK) {
	D3DAppISetErrorString("Getting DD capabilities failed while checking total video memory.\n%s",
			      D3DAppErrorToString(LastError));
	return 0L;
    }
    if (DriverCaps.dwVidMemTotal)
	return DriverCaps.dwVidMemTotal;
    else
	return HELCaps.dwVidMemTotal;
}

⌨️ 快捷键说明

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