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

📄 appframe.cpp

📁 DX例子中等难度的。全是新例子。VC2003
💻 CPP
字号:
//=============================================================================
// Desc: 主程序源文件
//=============================================================================

#include "dxstdafx.h"
#include "resource.h"


//-----------------------------------------------------------------------------
// Desc: 设备能力检查
//-----------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
                                  D3DFORMAT BackBufferFormat, bool bWindowed, 
								  void* pUserContext )
{
    //检查后台缓冲区格式是否支持Alpha混合等操作(post pixel blending operations)
    IDirect3D9* pD3D = DXUTGetD3DObject(); 
    if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
                    AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
                    D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
        return false;

    return true;
}


//-----------------------------------------------------------------------------
// Desc: 修改Direct3D渲染设备设置
//-----------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings,
								   const D3DCAPS9* pCaps, void* pUserContext )
{
    return true;
}


//-----------------------------------------------------------------------------
// Desc: 在此创建管理内存资源对象
//-----------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, 
								const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
								void* pUserContext )
{
    return S_OK;
}


//-----------------------------------------------------------------------------
// Desc: 在此创建默认内存类型资源对象
//-----------------------------------------------------------------------------
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
                                const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
								void* pUserContext )
{
    return S_OK;
}


//-----------------------------------------------------------------------------
// Desc: 更新场景
//-----------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, 
						  float fElapsedTime, void* pUserContext )
{
}


//-----------------------------------------------------------------------------
// Desc: 渲染场景
//-----------------------------------------------------------------------------
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, 
							float fElapsedTime, void* pUserContext )
{
    HRESULT hr;

    //清除后台颜色缓冲区和深度缓冲区
    V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
		                 D3DCOLOR_ARGB(0, 45, 50, 170), 1.0f, 0) );

    //开始渲染场景
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
		//结束场景渲染
        V( pd3dDevice->EndScene() );
    }
}


//-----------------------------------------------------------------------------
// Desc: 消息处理
//-----------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
                          bool* pbNoFurtherProcessing, void* pUserContext )
{
    return 0;
}


//-----------------------------------------------------------------------------
// 释放在OnResetDevice()中创建的对象
//-----------------------------------------------------------------------------
void CALLBACK OnLostDevice( void* pUserContext )
{
}


//-----------------------------------------------------------------------------
// Desc: 释放在OnCreateDevice()中创建的对象
//-----------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{
}


//-----------------------------------------------------------------------------
// Desc: 入口函数
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
    // Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

    //设置回调函数
    DXUTSetCallbackDeviceCreated( OnCreateDevice );
    DXUTSetCallbackDeviceReset( OnResetDevice );
    DXUTSetCallbackDeviceLost( OnLostDevice );
    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
    DXUTSetCallbackMsgProc( MsgProc );
    DXUTSetCallbackFrameRender( OnFrameRender );
    DXUTSetCallbackFrameMove( OnFrameMove );
   
	//在此进行应用程序相关的初始化

   
	//初始化DXUT, 创建窗口, 创建Direct3D设备对象
    DXUTInit( true, true, true );
    DXUTSetCursorSettings( true, true );
    DXUTCreateWindow( L"AppFrame" );
    DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, 
		              IsDeviceAcceptable, ModifyDeviceSettings );

    //进入消息循环和场景渲染
    DXUTMainLoop();

	//在此进行应用程序相关的清除工作

    return DXUTGetExitCode();
}

⌨️ 快捷键说明

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