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

📄 d3dapp.c

📁 国外游戏开发者杂志1997年第九期配套代码
💻 C
📖 第 1 页 / 共 4 页
字号:
    ATTEMPT(D3DAppICreateBuffers(d3dappi.hwnd, w, h, D3DAPP_BOGUS, FALSE,
				 d3dappi.Driver[d3dappi.CurrDriver].bIsHardware));
    /*
     * If the front buffer is palettized, initialize its palette
     */
    ATTEMPT(D3DAppICheckForPalettized());
    /*
     * Create the Z-buffer
     */
    ATTEMPT(D3DAppICreateZBuffer(szBuffers.cx, szBuffers.cy,
				 d3dappi.CurrDriver));
    /*
     * Create the D3D device, load the textures, call the device create
     * callback and set a default render state
     */
    ATTEMPT(D3DAppICreateDevice(d3dappi.CurrDriver));
    ATTEMPT(D3DAppILoadAllTextures());		
    ATTEMPT(D3DAppICallDeviceCreateCallback(szBuffers.cx, szBuffers.cy));
    ATTEMPT(D3DAppISetRenderState());
    /* 
     * Clear dirty rectangle information
     */
    D3DAppIValidateDirtyRects();
    d3dappi.bRenderingIsOK = TRUE;
    return TRUE;

exit_with_error:
    D3DAppICallDeviceDestroyCallback();
    RELEASE(d3dappi.lpD3DDevice);
    RELEASE(d3dappi.lpZBuffer);
    RELEASE(lpPalette);
    RELEASE(lpClipper);
    RELEASE(d3dappi.lpBackBuffer);
    RELEASE(d3dappi.lpFrontBuffer);
    return FALSE;  
}


/*
 * D3DAppChangeDriver 
 */
BOOL
D3DAppChangeDriver(int driver, DWORD flags)
{
    int mode;

    d3dappi.bRenderingIsOK = FALSE;
    /*
     * Verify the compatibility of this mode with the specified driver.
     * The mode may change.
     */
    if (d3dappi.bFullscreen)
	mode = d3dappi.CurrMode;
    else
	mode = D3DAPP_USEWINDOW;
    ATTEMPT(D3DAppIVerifyDriverAndMode(&driver, &mode));
    if (driver == D3DAPP_BOGUS || mode == D3DAPP_BOGUS)
	goto exit_with_error;
    /*
     * Update the current driver and set bThisDriverCanDo flags
     */
    d3dappi.CurrDriver = driver;
    ATTEMPT(D3DAppIFilterDisplayModes(driver));
    /*
     * Either call D3DAppWindow or D3DAppFullscreen depending on mode
     */
    if (mode == D3DAPP_USEWINDOW) {
	if (d3dappi.bFullscreen) {
	    /*
	     * We need to switch to a window.  D3DApp will either use the
	     * size of the last window it saw or use a default size.
	     */
	    ATTEMPT(D3DAppWindow(D3DAPP_YOUDECIDE, D3DAPP_YOUDECIDE));
	} else {
	    /*
	     * We need to recreate the current window.  Don't let D3DApp
	     * decide on the size.
	     */
	    ATTEMPT(D3DAppWindow(d3dappi.szClient.cx, d3dappi.szClient.cy));
	}
	/*
	 * Change the currently selected mode if it's not compatible with
	 * this driver.  Just to make sure that CurrMode is always a mode the
	 * current driver can do.
	 */
	if (!(d3dappi.Driver[driver].Desc.dwDeviceRenderBitDepth &
	      D3DAppIBPPToDDBD(d3dappi.Mode[d3dappi.CurrMode].bpp))){
	    ATTEMPT(D3DAppIPickDisplayMode(&d3dappi.CurrMode,
		        d3dappi.Driver[driver].Desc.dwDeviceRenderBitDepth));
	}
	d3dappi.bRenderingIsOK = TRUE;
	return TRUE;
    } else {
	/*
	 * We need to switch to fullscreen or switch fullscreen modes or stay
	 * in the same fullscreen mode.  In any of these cases, we call the
	 * same function.
	 */
	ATTEMPT(D3DAppFullscreen(mode));
	d3dappi.bRenderingIsOK = TRUE;
	return TRUE;
    }

exit_with_error:
    /*
     * The failed mode setting call would have released everything
     */
    return FALSE;
}


/*
 * D3DAppWindowProc
 */
BOOL
D3DAppWindowProc(BOOL* bStopProcessing, LRESULT* lresult, HWND hwnd,
		 UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    *bStopProcessing = FALSE;
    if (!bD3DAppInitialized)
	return TRUE;
    /*
     * Look for messages which effect rendering.  In some cases, we will not
     * want the app to continue processing the message, so set the flag and
     * provide a return value in lresult.
     */
    switch(message) {
	case WM_SIZE:
	    if (!bIgnoreWM_SIZE && d3dappi.bRenderingIsOK && d3dappi.lpD3DDevice) {
		d3dappi.bRenderingIsOK = FALSE;
		/*
		 * Too long to fit here, see ddcalls.c. Updates the buffers
		 * and re-creates the device.
		 */
		ATTEMPT(D3DAppIHandleWM_SIZE(lresult, d3dappi.hwnd, message,
					     wParam, lParam));
		*bStopProcessing = TRUE;
		d3dappi.bRenderingIsOK = TRUE;
	    }
	    break;
	case WM_MOVE:
	    /*
	     * Update client window position information
	     */
	    d3dappi.pClientOnPrimary.x = d3dappi.pClientOnPrimary.y = 0;
	    ClientToScreen(hwnd, &d3dappi.pClientOnPrimary);
	    break;
	case WM_ACTIVATE:
	    /*
	     * Set the front buffer's palette
	     */
	    if (bPaletteActivate && bPrimaryPalettized &&
		d3dappi.lpFrontBuffer) {
		d3dappi.lpFrontBuffer->lpVtbl->SetPalette(d3dappi.lpFrontBuffer,
							  lpPalette);
	    }
	    break;
        case WM_ACTIVATEAPP:
            d3dappi.bAppActive = (BOOL)wParam;
            break;
        case WM_SETCURSOR:
	    /*
	     * Prevent the cursor from being shown in fullscreen
	     */
            if (d3dappi.bFullscreen && !d3dappi.bPaused) {
                SetCursor(NULL);
                *lresult = 1;
		*bStopProcessing = TRUE;
		return TRUE;
            }
            break;
	case WM_MOVING:
	    /*
	     * Prevent the window from moving in fullscreen
	     */
	    if (d3dappi.bFullscreen) {
		GetWindowRect(hwnd, (LPRECT)lParam);
                *lresult = 1;
		*bStopProcessing = TRUE;
		return TRUE;
	    }
	    break;
	case WM_GETMINMAXINFO:
	    /*
	     * Ensure the window won't resize in fullscreen
	     */
	    if (d3dappi.bFullscreen) {
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.x= d3dappi.ThisMode.w;
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.y= d3dappi.ThisMode.h;
		((LPMINMAXINFO)lParam)->ptMinTrackSize.x= d3dappi.ThisMode.w;
		((LPMINMAXINFO)lParam)->ptMinTrackSize.y= d3dappi.ThisMode.h;
                *lresult = 0;
		*bStopProcessing = TRUE;
		return TRUE;
	    } else {
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.x =
						    d3dappi.WindowsDisplay.w;
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.y =
						    d3dappi.WindowsDisplay.h;
                *lresult = 0;
		*bStopProcessing = TRUE;
		return TRUE;
	    }
	    break;
	case WM_PAINT:
	    /*
	     * Clear the rectangle and blt the backbuffer image
	     */
	    BeginPaint(hwnd, &ps);
	    if (d3dappi.bRenderingIsOK && !d3dappi.bFullscreen) {
		D3DAppShowBackBuffer(D3DAPP_SHOWALL);
	    }
	    D3DAppIValidateDirtyRects();
	    EndPaint(hwnd, &ps);
	    *lresult = 1;
	    *bStopProcessing = TRUE;
	    return TRUE;
	case WM_NCPAINT:
	    /*
	     * When in fullscreen mode, don't draw the window frame.
	     */
	    if (d3dappi.bFullscreen && !d3dappi.bPaused) {
		*lresult = 0;
		*bStopProcessing = TRUE;
		return TRUE;
	    }
	    break;
    }
    return TRUE;

exit_with_error:
    return FALSE;
}

/*
 * D3DAppAddTexture 
 */
BOOL
D3DAppAddTexture(const char* imagefile)
{
    if (d3dappi.NumTextures == D3DAPP_MAXTEXTURES - 1) {
	D3DAppISetErrorString("Can only load %i textures.", D3DAPP_MAXTEXTURES);
	return FALSE;
    }
    lstrcpy(d3dappi.ImageFile[d3dappi.NumTextures], imagefile);
    /*
     * If this driver does texture mapping, load the texture.
     * This test also tests that a device has been created.
     */
    if (d3dappi.ThisDriver.bDoesTextures && d3dappi.NumUsableTextures == d3dappi.NumTextures) {
	BOOL bInVideo;
	ATTEMPT(D3DAppILoadTextureSurf(d3dappi.NumTextures, &bInVideo));
	if (!bInVideo && d3dappi.ThisDriver.bIsHardware) {
	    D3DAppIReleaseTexture(d3dappi.NumTextures);
	} else {
	    ATTEMPT(D3DAppIGetTextureHandle(d3dappi.NumTextures));
	    ++d3dappi.NumUsableTextures;
	}
    }
    d3dappi.NumTextures++;
    return TRUE;

exit_with_error:
    d3dappi.ImageFile[d3dappi.NumTextures][0] = 0;
    return FALSE;
}

/*
 * D3DAppChangeTextureFormat
 */
BOOL
D3DAppChangeTextureFormat(int format)
{
    /*
     * Release all the textures, change the format and load them again
     */
    d3dappi.bRenderingIsOK = FALSE;
    D3DAppIReleaseAllTextures();
    d3dappi.CurrTextureFormat = format;
    memcpy(&d3dappi.ThisTextureFormat, &d3dappi.TextureFormat[format],
	   sizeof(D3DAppTextureFormat));
    ATTEMPT(D3DAppILoadAllTextures());
    d3dappi.bRenderingIsOK = TRUE;
    return TRUE;

exit_with_error:
    D3DAppIReleaseAllTextures();
    return FALSE;
}

/*
 * D3DAppDisableTextures
 */
BOOL
D3DAppDisableTextures(BOOL flag)
{
    int i;
    if (flag == d3dappi.bTexturesDisabled)
	return TRUE;
    if (flag) {
	/*
	 * Set all the texture handles to 0
	 */
	d3dappi.bTexturesDisabled = TRUE;
	for (i = 0; i < d3dappi.NumTextures; i++)
	    d3dappi.TextureHandle[i] = 0;
    } else {
	/*
	 * Restore the texture handles from the master array
	 */
	d3dappi.bTexturesDisabled = FALSE;
	memcpy(d3dappi.TextureHandle, MasterTextureHandle,
	       sizeof(D3DTEXTUREHANDLE) * D3DAPP_MAXTEXTURES);
    }
    return TRUE;
}

/*
 * D3DAppSwapTextures
 */
BOOL
D3DAppSwapTextures()
{
    int i;
    char tempfile[30];
    LPDIRECT3DTEXTURE lptempTexture;
    LPDIRECTDRAWSURFACE lptempSurface;
    if (d3dappi.bTexturesDisabled || d3dappi.NumTextures == 0) {
	D3DAppISetErrorString("Cannot swap textures which are disable or not loaded.\n");
	goto exit_with_error;
    }
    if (!d3dappi.ThisDriver.bDoesTextures)
	return TRUE;
    /*
     * Swap texture 1 with 2, then 2 with 3, then 3 with 4, etc.
     * Don't forget the image file names, texture objects and surfaces
     */
    for (i = 0; i < d3dappi.NumUsableTextures - 1; i++) {
	lstrcpy(tempfile, d3dappi.ImageFile[i]);
	lstrcpy(d3dappi.ImageFile[i], d3dappi.ImageFile[i+1]);
	lstrcpy(d3dappi.ImageFile[i+1], tempfile);
	d3dappi.lpD3DDevice->lpVtbl->SwapTextureHandles(d3dappi.lpD3DDevice,
						      d3dappi.lpTexture[i],
						      d3dappi.lpTexture[i+1]);
	lptempTexture = d3dappi.lpTexture[i];
	d3dappi.lpTexture[i] = d3dappi.lpTexture[i+1];
	d3dappi.lpTexture[i+1] = lptempTexture;
	lptempSurface = d3dappi.lpTextureSurf[i];
	d3dappi.lpTextureSurf[i] = d3dappi.lpTextureSurf[i+1];
	d3dappi.lpTextureSurf[i+1] = lptempSurface;
    }
    return TRUE;
exit_with_error:
    return FALSE;
}

⌨️ 快捷键说明

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