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

📄 d3dmain.cpp

📁 国外游戏开发者杂志1997年第九期配套代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:

		if (myglobs.bClearsOn) {
	    if (!D3DAppClearBackBuffer(myglobs.bResized ?
				      D3DAPP_CLEARALL : NULL)) {
		ReportD3DAppError();
		return FALSE;
	    }
	}
	/*
	 * Call the sample's RenderScene to render this frame
	 */
		if (!RenderScene(d3dapp->lpD3DDevice, d3dapp->lpD3DViewport,
			     &extents[0])) {
		Msg("RenderScene failed.\n");
		return FALSE;
	    }
	   count = 1;

#ifdef SYNCH_WITH_SW
		  countHW++;  // just for synchronization debugging
#endif		
 
#ifdef DEBUG_SYNCH_WITH_SW  // debug - synch
      //test to make sure counters are same for HW & SW
if (countSW != countHW)
  {
	  Msg("countSW != countHW !\0",0);
	  __asm int 3;
  }
#endif
	/*
	 * Blt the frame rate and window stat text to the back buffer
	 */
        if (!DisplayFrameRate(&count, &extents[1]))
	    return FALSE;
	/*
	 * Give D3DApp the extents so it can keep track of dirty sections of
	 * the back and front buffers
	 */
	if (!D3DAppRenderExtents(count, extents, myglobs.bResized ?
				 D3DAPP_CLEARALL : NULL)) {
	    ReportD3DAppError();
	    return FALSE;
	}

   if	(compositeMethod  == BLT_COMPOSITE) {
    // Insert composite step here!!! and include synch events
	if (d3dappi.bFullscreen)
    {
	SetRect(&dRect, 200, 180, 200+255,180+255);
    SetRect(&sRect, 0, 0, 255, 255);
	} 
	else
    {
	SetRect(&dRect, 50, 30, 180, 150);
    SetRect(&sRect, 0, 0, 255, 255);
	}
    ddrval = d3dappi.lpBackBuffer->Blt(&dRect, lpDDSurfaceSystem, &sRect,
																DDBLT_KEYSRC | DDBLT_ASYNC, NULL);  
    if (ddrval != D3D_OK)
    {
	  Msg("BLT Failed.\0", ddrval);
      return(FALSE);
     }

  // Resume the SW Thread - that is, the main thread has consumed the
  // output of the SW rendering.  Use of double buffering would eliminate
  // this and then allow SW to go ahead to the next frame early
	SetEvent(compositeHandle);
  }
    /*
	 * Blt or flip the back buffer to the front buffer.  Don't report an
	 * error if this fails.
	 */
	D3DAppShowBackBuffer(myglobs.bResized ? D3DAPP_SHOWALL : NULL);
	/*
	 * Reset the resize flag
	 */
        myglobs.bResized = FALSE;
    }
    return TRUE;
}






/*
 * AppPause
 * Pause and unpause the application
 */
static BOOL
AppPause(BOOL f)
{
    /*
     * Flip to the GDI surface and halt rendering
     */
    if (!D3DAppPause(f))
	return FALSE;
    /*
     * When returning from a pause, reset the frame rate count
     */
    if (!f) {
        ResetFrameRate();
    }
    return TRUE;
}

/*
 * RestoreSurfaces
 * Restores any lost surfaces.  Returns TRUE if all surfaces are not lost and
 * FALSE if one or more surfaces is lost and can not be restored at the
 * moment.
 */
static BOOL
RestoreSurfaces()
{
    HRESULT d3drval;

    /*
     * Have D3DApp check all the surfaces it's in charge of
     */
    if (!D3DAppCheckForLostSurfaces()) {
            return FALSE;
    }
    /*
     * Check frame rate and info surfaces and re-write them if they
     * were lost.
     */
    if (myglobs.lpFrameRateBuffer->IsLost() == DDERR_SURFACELOST) {
        d3drval = myglobs.lpFrameRateBuffer->Restore();
        if (d3drval != DD_OK) {
            return FALSE;
        }
	if (!WriteFrameRateBuffer(0.0f, 0))
	    return FALSE;
    }
    if (myglobs.lpInfoBuffer->IsLost() == DDERR_SURFACELOST) {
        d3drval = myglobs.lpInfoBuffer->Restore();
        if (d3drval != DD_OK) {
            return FALSE;
        }
	if (!WriteInfoBuffer())
	    return FALSE;
    }
    return TRUE;
}


/*************************************************************************
  Windows message handlers
 *************************************************************************/
/*
 * AppAbout
 * About box message handler
 */
BOOL
FAR PASCAL AppAbout(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
    case WM_COMMAND:
      if (LOWORD(wParam) == IDOK)
        EndDialog(hwnd, TRUE);
      break;

    case WM_INITDIALOG:
      return TRUE;
  }
  return FALSE;
}

/*
 * WindowProc
 * Main window message handler.
 */
long
FAR PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam,
			   LPARAM lParam )
{
    int i;
    BOOL bStop;
    LRESULT lresult;

    /*
     * Give D3DApp an opportunity to process any messages it MUST see in order
     * to perform it's function.
     */
    if (!D3DAppWindowProc(&bStop, &lresult, hWnd, message, wParam, lParam)) {
	ReportD3DAppError();
	CleanUpAndPostQuit();
	return 0;
    }

    /*
     * Prevent resizing through this message
     */
    if (message == WM_GETMINMAXINFO && myglobs.bResizingDisabled && !d3dapp->bFullscreen && !d3dapp->bMinimized) {
	RECT rc;
	GetWindowRect(hWnd, &rc);
	((LPMINMAXINFO)lParam)->ptMaxTrackSize.x = START_WIN_SIZE;
	((LPMINMAXINFO)lParam)->ptMaxTrackSize.y = START_WIN_SIZE;
	((LPMINMAXINFO)lParam)->ptMinTrackSize.x = START_WIN_SIZE;
	((LPMINMAXINFO)lParam)->ptMinTrackSize.y = START_WIN_SIZE;
        return 0;
    }

    /* 
     * If bStop is set by D3DApp, the app should not process the message but
     * return lresult.
     */
    if (bStop)
	return lresult;

    if (!d3dapp || !d3dapp->bRenderingIsOK) {
	return DefWindowProc(hWnd, message, wParam, lParam);
    }

    switch( message ) {
        case WM_MOUSEMOVE:
        case WM_LBUTTONDOWN:
        case WM_LBUTTONUP:
        case WM_RBUTTONDOWN:
        case WM_RBUTTONUP:
        case WM_MBUTTONDOWN:
        case WM_MBUTTONUP:
	    /*
	     * Call the sample's MouseHandler if available
	     */
            if (!MouseHandler)
                break;
            if ((MouseHandler)(message, wParam, lParam))
                return 1;
            break;
        case WM_KEYDOWN:
	    /*
	     * Call the sample's keyboard handler if available
	     */
            if (!KeyboardHandler)
                break;
            if ((KeyboardHandler)(message, wParam, lParam))
                return 1;
            break;
	/*
	 * Pause and unpause the app when entering/leaving the menu
	 */
        case WM_ENTERMENULOOP:
            AppPause(TRUE);
            break;
        case WM_EXITMENULOOP:
            AppPause(FALSE);
            break;
    	case WM_DESTROY:
            myglobs.hWndMain = NULL;
            CleanUpAndPostQuit();
            break;
        case WM_INITMENUPOPUP:
	    /*
             * Check and enable the appropriate menu items
	     */
	    if (d3dapp->ThisDriver.bDoesZBuffer) {
		EnableMenuItem((HMENU)wParam, MENU_ZBUFFER, MF_ENABLED);
		CheckMenuItem((HMENU)wParam, MENU_ZBUFFER, myglobs.rstate.bZBufferOn ? MF_CHECKED : MF_UNCHECKED);
	    } else {
		EnableMenuItem((HMENU)wParam, MENU_ZBUFFER, MF_GRAYED);
		CheckMenuItem((HMENU)wParam, MENU_ZBUFFER, MF_UNCHECKED);
	    }
            CheckMenuItem((HMENU)wParam, MENU_STEP, (myglobs.bSingleStepMode) ? MF_CHECKED : MF_UNCHECKED);
            EnableMenuItem((HMENU)wParam, MENU_GO, (myglobs.bSingleStepMode) ? MF_ENABLED : MF_GRAYED);
            CheckMenuItem((HMENU)wParam, MENU_FLAT, (myglobs.rstate.ShadeMode == D3DSHADE_FLAT) ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_GOURAUD, (myglobs.rstate.ShadeMode == D3DSHADE_GOURAUD) ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_PHONG, (myglobs.rstate.ShadeMode == D3DSHADE_PHONG) ? MF_CHECKED : MF_UNCHECKED);
	    EnableMenuItem((HMENU)wParam, MENU_PHONG, MF_GRAYED);
            CheckMenuItem((HMENU)wParam, MENU_CLEARS, myglobs.bClearsOn ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_POINT, (myglobs.rstate.FillMode == D3DFILL_POINT) ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_WIREFRAME, (myglobs.rstate.FillMode == D3DFILL_WIREFRAME) ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_SOLID, (myglobs.rstate.FillMode == D3DFILL_SOLID) ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_DITHERING, myglobs.rstate.bDithering ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_SPECULAR, myglobs.rstate.bSpecular ? MF_CHECKED : MF_UNCHECKED);
            EnableMenuItem((HMENU)wParam, MENU_SPECULAR, MF_ENABLED);
            CheckMenuItem((HMENU)wParam, MENU_FOG, myglobs.rstate.bFogEnabled ? MF_CHECKED : MF_UNCHECKED);
            CheckMenuItem((HMENU)wParam, MENU_ANTIALIAS, myglobs.rstate.bAntialiasing ? MF_CHECKED : MF_UNCHECKED);
	    if (d3dapp->ThisDriver.bDoesTextures) {
		CheckMenuItem((HMENU)wParam, MENU_TEXTURE_TOGGLE, (!d3dapp->bTexturesDisabled) ? MF_CHECKED : MF_UNCHECKED);
		EnableMenuItem((HMENU)wParam, MENU_TEXTURE_TOGGLE, MF_ENABLED);
		EnableMenuItem((HMENU)wParam, MENU_TEXTURE_SWAP, (d3dapp->bTexturesDisabled) ? MF_GRAYED : MF_ENABLED);
		CheckMenuItem((HMENU)wParam, MENU_PERSPCORRECT, myglobs.rstate.bPerspCorrect ? MF_CHECKED : MF_UNCHECKED);
		EnableMenuItem((HMENU)wParam, MENU_PERSPCORRECT, (d3dapp->bTexturesDisabled) ? MF_GRAYED : MF_ENABLED);
		CheckMenuItem((HMENU)wParam, MENU_POINT_FILTER, (myglobs.rstate.TextureFilter == D3DFILTER_NEAREST) ? MF_CHECKED : MF_UNCHECKED);
		EnableMenuItem((HMENU)wParam, MENU_POINT_FILTER, (d3dapp->bTexturesDisabled) ? MF_GRAYED : MF_ENABLED);
		CheckMenuItem((HMENU)wParam, MENU_LINEAR_FILTER, (myglobs.rstate.TextureFilter == D3DFILTER_LINEAR) ? MF_CHECKED : MF_UNCHECKED);
		EnableMenuItem((HMENU)wParam, MENU_LINEAR_FILTER, (d3dapp->bTexturesDisabled) ? MF_GRAYED : MF_ENABLED);
		for (i = 0; i < d3dapp->NumTextureFormats; i++) {
		    CheckMenuItem((HMENU)wParam, MENU_FIRST_FORMAT + i, (i == d3dapp->CurrTextureFormat) ? MF_CHECKED : MF_UNCHECKED);
		    EnableMenuItem((HMENU)wParam, MENU_FIRST_FORMAT + i, (d3dapp->bTexturesDisabled) ? MF_GRAYED : MF_ENABLED);
		}
	    } else {
		EnableMenuItem((HMENU)wParam, MENU_TEXTURE_SWAP, MF_GRAYED);
		EnableMenuItem((HMENU)wParam, MENU_TEXTURE_TOGGLE, MF_GRAYED);
		EnableMenuItem((HMENU)wParam, MENU_POINT_FILTER, MF_GRAYED);
		EnableMenuItem((HMENU)wParam, MENU_LINEAR_FILTER, MF_GRAYED);
		EnableMenuItem((HMENU)wParam, MENU_PERSPCORRECT, MF_GRAYED);
	    }
	    if (d3dapp->bIsPrimary) {
		CheckMenuItem((HMENU)wParam, MENU_FULLSCREEN, d3dapp->bFullscreen ? MF_CHECKED : MF_UNCHECKED);
		EnableMenuItem((HMENU)wParam, MENU_FULLSCREEN, d3dapp->bFullscreen && !d3dapp->ThisDriver.bCanDoWindow ? MF_GRAYED : MF_ENABLED);
		EnableMenuItem((HMENU)wParam, MENU_NEXT_MODE, (!d3dapp->bFullscreen) ? MF_GRAYED : MF_ENABLED);
		EnableMenuItem((HMENU)wParam, MENU_PREVIOUS_MODE, (!d3dapp->bFullscreen) ? MF_GRAYED : MF_ENABLED);
	    } else {
		EnableMenuItem((HMENU)wParam, MENU_FULLSCREEN, MF_GRAYED);
		EnableMenuItem((HMENU)wParam, MENU_NEXT_MODE, MF_GRAYED);
		EnableMenuItem((HMENU)wParam, MENU_PREVIOUS_MODE, MF_GRAYED);
	    }
	    for (i = 0; i < d3dapp->NumModes; i++) {
		CheckMenuItem((HMENU)wParam, MENU_FIRST_MODE + i, (i == d3dapp->CurrMode) ? MF_CHECKED : MF_UNCHECKED);
		EnableMenuItem((HMENU)wParam, MENU_FIRST_MODE + i, (d3dapp->Mode[i].bThisDriverCanDo) ? MF_ENABLED : MF_GRAYED);
	    }
            for (i = 0; i < d3dapp->NumDrivers; i++) {
                CheckMenuItem((HMENU)wParam, MENU_FIRST_DRIVER + i, (i == d3dapp->CurrDriver) ? MF_CHECKED : MF_UNCHECKED);
            }
            for (i = 0; i < myglobs.NumDDDrivers; i++) {
                CheckMenuItem((HMENU)wParam, MENU_FIRST_DDDRIVER + i, (i == myglobs.CurrDDDriver) ? MF_CHECKED : MF_UNCHECKED);
            }
            break;
	case WM_GETMINMAXINFO:
	    /*
	     * Some samples don't like being resized, such as those which use
	     * screen coordinates (TLVERTEXs).
	     */
	    if (myglobs.bResizingDisabled) {
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.x = START_WIN_SIZE;
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.y = START_WIN_SIZE;
		((LPMINMAXINFO)lParam)->ptMinTrackSize.x = START_WIN_SIZE;
		((LPMINMAXINFO)lParam)->ptMinTrackSize.y = START_WIN_SIZE;
		return 0;
	    }
	    break;
        case WM_COMMAND:
            switch(LOWORD(wParam)) {
                case MENU_ABOUT:
                    AppPause(TRUE);
                    DialogBox(myglobs.hInstApp, "AppAbout", myglobs.hWndMain, (DLGPROC)AppAbout);
                    AppPause(FALSE);
                    break;
                case MENU_EXIT:
	            CleanUpAndPostQuit();
        	    break;
                case MENU_STEP:
		    /*
		     * Begin single step more or draw a frame if in single
		     * step mode
		     */
                    if (!myglobs.bSingleStepMode) {
                        myglobs.bSingleStepMode = TRUE;
                        myglobs.bDrawAFrame = TRUE;
                    } else if (!myglobs.bDrawAFrame) {
                        myglobs.bDrawAFrame = TRUE;
                    }
                    break;
                case MENU_GO:
		    /*
		     * Exit single step mode
		     */
                    if (myglobs.bSingleStepMode) {
                        myglobs.bSingleStepMode = FALSE;
                        ResetFrameRate();
                    }
                    break;
                case MENU_STATS:
		    /*
		     * Toggle output of frame rate and window info
		     */
                    if ((myglobs.bShowFrameRate) && (myglobs.bShowInfo)) {
                        myglobs.bShowFrameRate = FALSE;
                        myglobs.bShowInfo = FALSE;
                        break;
                    }
                    if ((!myglobs.bShowFrameRate) && (!myglobs.bShowInfo)) {
                        myglobs.bShowFrameRate = TRUE;
                        break;
                    }
                    myglobs.bShowInfo = TRUE;
                    break;
        	case MENU_FULLSCREEN:
		    if (d3dapp->bFullscreen) {
			/*
			 * Return to a windowed mode.  Let D3DApp decide which
			 * D3D driver to use in case this one cannot render to
			 * the Windows display depth
			 */
			if (!D3DAppWindow(D3DAPP_YOUDECIDE, D3DAPP_YOUDECIDE)) {
			    ReportD3DAppError();
			    CleanUpAndPostQuit();
			    break;
			}
		    } else {
			/*
			 * Enter the current fullscreen mode.  D3DApp may
			 * resort to another mode if this driver cannot do
			 * the currently selected mode.
			 */
			if (!D3DAppFullscreen(d3dapp->CurrMode)) {
			    ReportD3DAppError();
			    CleanUpAndPostQuit();
			    break;
			}
		    }
            	    break;
		/*
		 * Texture filter method selection

⌨️ 快捷键说明

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