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

📄 d3dmain.cpp

📁 国外游戏开发者杂志1997年第九期配套代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		 */
                case MENU_POINT_FILTER:
                    if (myglobs.rstate.TextureFilter == D3DFILTER_NEAREST)
                        break;
                    myglobs.rstate.TextureFilter = D3DFILTER_NEAREST;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_LINEAR_FILTER:
                    if (myglobs.rstate.TextureFilter == D3DFILTER_LINEAR)
                        break;
                    myglobs.rstate.TextureFilter = D3DFILTER_LINEAR;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
		/* 
		 * Shading selection
		 */
                case MENU_FLAT:
                    if (myglobs.rstate.ShadeMode == D3DSHADE_FLAT)
                        break;
                    myglobs.rstate.ShadeMode = D3DSHADE_FLAT;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_GOURAUD:
                    if (myglobs.rstate.ShadeMode == D3DSHADE_GOURAUD)
                        break;
                    myglobs.rstate.ShadeMode = D3DSHADE_GOURAUD;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
                    break;
                case MENU_PHONG:
                    if (myglobs.rstate.ShadeMode == D3DSHADE_PHONG)
                        break;
                    myglobs.rstate.ShadeMode = D3DSHADE_PHONG;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
                    break;
		/*
		 * Fill mode selection
		 */
                case MENU_POINT:
                    if (myglobs.rstate.FillMode == D3DFILL_POINT)
                        break;
                    myglobs.rstate.FillMode = D3DFILL_POINT;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
                    break;
                case MENU_WIREFRAME:
                    if (myglobs.rstate.FillMode == D3DFILL_WIREFRAME)
                        break;
                    myglobs.rstate.FillMode = D3DFILL_WIREFRAME;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
                    break;
                case MENU_SOLID:
                    if (myglobs.rstate.FillMode == D3DFILL_SOLID)
                        break;
                    myglobs.rstate.FillMode = D3DFILL_SOLID;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
                    break;
                case MENU_PERSPCORRECT:
		    /*
		     * Toggle perspective correction
		     */
                    myglobs.rstate.bPerspCorrect =
			!myglobs.rstate.bPerspCorrect;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
                    break;
                case MENU_CLEARS:
		    /*
		     * Toggle the clearing the the back buffer and Z-buffer
		     * and set the resized flag to clear the entire window
		     */
                    myglobs.bClearsOn = !myglobs.bClearsOn;
                    if (myglobs.bClearsOn)
                        myglobs.bResized = TRUE;
                    break;
                case MENU_ZBUFFER:
		    /*
		     * Toggle the use of a Z-buffer
		     */
                    myglobs.rstate.bZBufferOn = !myglobs.rstate.bZBufferOn;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_DITHERING:
		    /*
		     * Toggle dithering
		     */
                    myglobs.rstate.bDithering = !myglobs.rstate.bDithering;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_SPECULAR:
		    /*
		     * Toggle specular highlights
		     */
                    myglobs.rstate.bSpecular = !myglobs.rstate.bSpecular;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_FOG:
		    /*
		     * Toggle fog
		     */
                    myglobs.rstate.bFogEnabled = !myglobs.rstate.bFogEnabled;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_ANTIALIAS:
		    /*
		     * Toggle anti-aliasing
		     */
                    myglobs.rstate.bAntialiasing =
			!myglobs.rstate.bAntialiasing;
                    if (!D3DAppSetRenderState(&myglobs.rstate)) {
			ReportD3DAppError();
			break;
		    }
		    break;
                case MENU_TEXTURE_TOGGLE:
		    /*
		     * Release the sample's execute buffers, toggle the
		     * texture disabled state and recreate them.
		     */
		    ReleaseView(d3dapp->lpD3DViewport);
		    D3DAppDisableTextures(!d3dapp->bTexturesDisabled);
		    {
			if (!InitView(d3dapp->lpDD, d3dapp->lpD3D,
				      d3dapp->lpD3DDevice,
				      d3dapp->lpD3DViewport,
				      d3dapp->NumUsableTextures,
				      d3dapp->TextureHandle)) {
			    Msg("InitView failed.\n");
			    CleanUpAndPostQuit();
			    break;
			}
		    }
		    myglobs.bResized = TRUE;
		    break;
		case MENU_TEXTURE_SWAP:
		    /*
		     * Swap textures using the load command
		     */
		    if (!D3DAppSwapTextures()) {
			ReportD3DAppError();
			break;
		    }
		    /*
		     * Just in case we have a texture background
		     */
		    myglobs.bResized = TRUE;
		    break;
		
		case  MENU_SW_TOGGLE_LIGHT  :
		case  MENU_SW_COS_POWER       :
		case  MENU_SW_LIGHT_MODE       :
		case  MENU_SW_TOGGLE_LIGHT_EFFECT  :
		case  MENU_SW_TOGGLE_RESOLUTION :
		case  MENU_SW_OCTAVE_NUM  :
		case  MENU_SW_MARBLE_METHOD  :
		case  MENU_SW_TOGGLE_TEXTURE  :
		case  MENU_SW_TOGGLE_ZBUF  :
		case  MENU_SW_SCALE_UP        :
		case  MENU_SW_SCALE_DOWN  :
				if (KeyboardHandler && (KeyboardHandler)(message, wParam, lParam))
							return 1;
				break;
		case MENU_SW_TOGGLE_COMPOSITE  :     
				    compositeMethod = (compositeMethod + 1 ) % 2 ;
			         break;
		case MENU_NEXT_MODE:
		    /*
		     * Enter the next usable fullscreen mode
		     */
		    i = d3dapp->CurrMode;
		    do {
			++i;
			if (i >= d3dapp->NumModes)
			    i = 0;
			if (!d3dapp->Mode[i].bThisDriverCanDo)
			    continue;
			else {
			    if (!D3DAppFullscreen(i)) {
				ReportD3DAppError();
				CleanUpAndPostQuit();
			    }
			    break;
			}
		    } while(i != d3dapp->CurrMode);
		    break;
		case MENU_PREVIOUS_MODE:
		    /*
		     * Enter the previous usable fullscreen mode
		     */
		    i = d3dapp->CurrMode;
		    do {
			--i;
			if (i < 0)
			    i = d3dapp->NumModes - 1;
			if (!d3dapp->Mode[i].bThisDriverCanDo)
			    continue;
			else {
			    if (!D3DAppFullscreen(i)) {
				ReportD3DAppError();
				CleanUpAndPostQuit();
			    }
			    break;
			}
		    } while(i != d3dapp->CurrMode);
                    break;
            }
            if (   LOWORD(wParam) >= MENU_FIRST_FORMAT
		&& LOWORD(wParam) < MENU_FIRST_FORMAT +
		    D3DAPP_MAXTEXTUREFORMATS
		&& d3dapp->CurrTextureFormat !=
		    LOWORD(wParam) - MENU_FIRST_FORMAT) {
		/*
		 * Release the sample's execute buffers, change the texture
		 * format and recreate the view.
		 */
		ReleaseView(d3dapp->lpD3DViewport);
		if (!D3DAppChangeTextureFormat(LOWORD(wParam) -
  					       MENU_FIRST_FORMAT)) {
		    ReportD3DAppError();
		    CleanUpAndPostQuit();
		}
		{
		    if (!InitView(d3dapp->lpDD, d3dapp->lpD3D,
				  d3dapp->lpD3DDevice, d3dapp->lpD3DViewport,
				  d3dapp->NumUsableTextures,
				  d3dapp->TextureHandle)) {
			Msg("InitView failed.\n");
			CleanUpAndPostQuit();
			break;
		    }
		}
                ResetFrameRate();
            }
            if (   LOWORD(wParam) >= MENU_FIRST_DRIVER
		&& LOWORD(wParam) < MENU_FIRST_DRIVER + D3DAPP_MAXD3DDRIVERS
		&& d3dapp->CurrDriver != LOWORD(wParam) - MENU_FIRST_DRIVER) {
		/*
		 * Change the D3D driver
		 */
		if (!D3DAppChangeDriver(LOWORD(wParam) - MENU_FIRST_DRIVER,
					NULL)) {
		    ReportD3DAppError();
		    CleanUpAndPostQuit();
		}
            }
            if (   LOWORD(wParam) >= MENU_FIRST_MODE
		&& LOWORD(wParam) < MENU_FIRST_MODE+100) {
		/*
		 * Switch to the selected fullscreen mode
		 */
		if (!D3DAppFullscreen(LOWORD(wParam) - MENU_FIRST_MODE)) {
		    ReportD3DAppError();
		    CleanUpAndPostQuit();
		}
            }
            if (   LOWORD(wParam) >= MENU_FIRST_DDDRIVER
		&& LOWORD(wParam) < MENU_FIRST_DDDRIVER + D3DAPP_MAXDDDRIVERS
		&& myglobs.CurrDDDriver != LOWORD(wParam) - MENU_FIRST_DDDRIVER) {
		/*
		 * Change the DD driver
		 */
		DestroyD3DApp();
		myglobs.CurrDDDriver = LOWORD(wParam) - MENU_FIRST_DDDRIVER;
		SetWindowPos(myglobs.hWndMain, HWND_NOTOPMOST, 0, 0, START_WIN_SIZE, START_WIN_SIZE, SWP_NOMOVE | SWP_SHOWWINDOW);
		if (!CreateD3DApp(myglobs.lpCmdLine)) {
		    ReportD3DAppError();
		    CleanUpAndPostQuit();
		}
            }
	    /*
	     * Whenever we receive a command in single step mode, draw a frame
	     */
	    if (myglobs.bSingleStepMode)
		myglobs.bDrawAFrame = TRUE;
            return 0L;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}


/****************************************************************************/
/*                 Setting up callbacks for examples                        */
/****************************************************************************/
 /*
  * SetMouseCallback
  * Called in an example to set a callback function for all WM messages
  * dealing with the mouse.  The handler should return whether or not
  * it handled the message.
  */
BOOL
SetMouseCallback(BOOL(*Handler)(UINT, WPARAM, LPARAM))
{
    MouseHandler = Handler;
    return TRUE;
}
 
/*
 * SetKeyboardCallback
 * Called in an example to set a callback function for all WM messages
 * dealing with the keyboard. The handler should return whether or not it
 * handled the message.
 */
BOOL
SetKeyboardCallback(BOOL(*Handler)(UINT, WPARAM, LPARAM)) {
    
    KeyboardHandler = Handler;
    return TRUE;
}

/****************************************************************************/
/*          Initialization, error reporting and release functions.          */
/****************************************************************************/
/*
 * InitGlobals
 * Called once at program initialization to initialize global variables.
 */
static void
InitGlobals(void)
{
    d3dapp = NULL;
    memset(&myglobs.rstate, 0, sizeof(myglobs.rstate));
    memset(&myglobs, 0, sizeof(myglobs));
    myglobs.bClearsOn = TRUE;
    myglobs.bShowFrameRate = TRUE;
    myglobs.bShowInfo = TRUE;
    MouseHandler = NULL;
    KeyboardHandler = NULL;
}

/*
 * CleanUpAndPostQuit
 * Release all D3D objects, post a quit message and set the bQuit flag
 */
void
CleanUpAndPostQuit(void)
{
    if (myglobs.bQuit)
	return;
    DestroyD3DApp();
    ReleaseScene();
    myglobs.bQuit = TRUE;
    PostQuitMessage( 0 );
}

/*
 * ReportD3DAppError
 * Reports an error during a d3d app call.
 */
void
ReportD3DAppError(void)
{
    Msg("%s", D3DAppLastErrorString());
}

/* Msg
 * Message output for error notification.
 */
void __cdecl
Msg( LPSTR fmt, ... )
{
    char buff[256];

    wvsprintf(buff, fmt, (char *)(&fmt+1));
    lstrcat(buff, "\r\n");
    AppPause(TRUE);
    if (d3dapp && d3dapp->bFullscreen)
	SetWindowPos(myglobs.hWndMain, HWND_NOTOPMOST, 0, 0, 0, 0,
		     SWP_NOSIZE | SWP_NOMOVE);
    MessageBox( NULL, buff, "D3D Example Message", MB_OK );
    if (d3dapp && d3dapp->bFullscreen)
	SetWindowPos(myglobs.hWndMain, HWND_TOPMOST, 0, 0, 0, 0,
		     SWP_NOSIZE | SWP_NOMOVE);
    AppPause(FALSE);
}

⌨️ 快捷键说明

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