www.pudn.com > Render.rar > Sample.cpp


#define STRICT 
#include  
#include  
#include  
#include  
#include  
#include "DXUtil.h" 
#include "D3DEnumeration.h" 
#include "D3DSettings.h" 
#include "D3DApp.h" 
#include "D3DFile.h" 
#include "D3DFont.h" 
#include "D3DUtil.h" 
#include "resource.h" 
 
#include "SJJ_Role.h" 
#include "SJJ_Camera.h" 
//----------------------------------------------------------------------------- 
// Name: class CMyD3DApplication 
// Desc: Application class. The base class (CD3DApplication) provides the  
//       generic functionality needed in all Direct3D samples. CMyD3DApplication  
//       adds functionality specific to this sample program. 
//----------------------------------------------------------------------------- 
class CMyD3DApplication : public CD3DApplication 
{ 
 
protected: 
    HRESULT OneTimeSceneInit(); 
    HRESULT InitDeviceObjects(); 
    HRESULT RestoreDeviceObjects(); 
    HRESULT InvalidateDeviceObjects(); 
    HRESULT DeleteDeviceObjects(); 
    HRESULT FinalCleanup(); 
    HRESULT Render(); 
    HRESULT FrameMove(); 
    HRESULT ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,  
		D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat ); 
    LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,LPARAM lParam ); 
public: 
    CMyD3DApplication(); 
 
	SJJ_Role*     TestTiger; 
	SJJ_Camera*   TestCamera; 
	SJJ_Role*     TestPalace; 
 
	BYTE  m_bKey[256];			// keyboard state buffer 
	BYTE  m_mouseright;			//鼠标右键是否按下1按下 0没有按下 
 
	POINT oldpoint; 
	POINT newpoint; 
	void KeyBoardMove(void); 
}; 
 
 
 
 
//----------------------------------------------------------------------------- 
// Name: WinMain() 
// Desc: Entry point to the program. Initializes everything, and goes into a 
//       message-processing loop. Idle time is used to render the scene. 
//----------------------------------------------------------------------------- 
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) 
{ 
    CMyD3DApplication d3dApp; 
 
    InitCommonControls(); 
    if( FAILED( d3dApp.Create( hInst ) ) ) 
        return 0; 
 
    return d3dApp.Run(); 
} 
 
//----------------------------------------------------------------------------- 
// Name: CMyD3DApplication() 
// Desc: Application constructor. Sets attributes for the app. 
//----------------------------------------------------------------------------- 
CMyD3DApplication::CMyD3DApplication() 
{ 
  m_dwCreationWidth		= 800;   // Width used to create window 
  m_dwCreationHeight	= 600; 
  m_strWindowTitle    = _T("Engine TEST"); 
  m_d3dEnumeration.AppUsesDepthBuffer   = TRUE; 
 
  ZeroMemory( m_bKey, 256 ); 
  m_mouseright=0; 
} 
 
//----------------------------------------------------------------------------- 
// Name: ConfirmDevice() 
// Desc: Called during device initialization, this code checks the device 
//       for some minimum set of capabilities 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior, 
                                          D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat ) 
{ 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: OneTimeSceneInit() 
// Desc: Called during initial app startup, this function performs all the 
//       permanent initialization. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::OneTimeSceneInit() 
{ 
    return S_OK; 
} 
 
 
//----------------------------------------------------------------------------- 
// Name: InitDeviceObjects() 
// Desc: Initialize scene objects. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::InitDeviceObjects() 
{ 
	//初始化SJJ_Role对象 
	 TestPalace=new SJJ_Role(&m_pd3dDevice);//注意要放在m_pd3dDevice初始化以后 
	 char MeshName[]="Map.x"; 
	 TestPalace->InitMesh(MeshName); 
 
	 TestTiger=new SJJ_Role(&m_pd3dDevice);//注意要放在m_pd3dDevice初始化以后 
	 char MeshName2[]="Tiger.x"; 
	 TestTiger->InitMesh(MeshName2); 
	 TestTiger->SetPos(0,2,0);//设置老虎的位置稍微高一点 
 
	//初始化SJJ_Camera对象 
    TestCamera=new SJJ_Camera; 
 
	//记录屏幕中间点 
	RECT rect=this->m_rcWindowClient; 
	oldpoint.x=(rect.right+rect.left)/2; 
	oldpoint.y=(rect.bottom+rect.top)/2; 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: RestoreDeviceObjects() 
// Desc: Initialize scene objects. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::RestoreDeviceObjects() 
{ 
	m_pd3dDevice->SetRenderState(D3DRS_LIGHTING,false); 
 
	D3DXMATRIXA16 matProj; 
	::D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 10000.0f );  
	m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); 
 
	TestCamera->SetCamera(m_pd3dDevice); 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: InvalidateDeviceObjects() 
// Desc: 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::InvalidateDeviceObjects() 
{ 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: DeleteDeviceObjects() 
// Desc: Called when the app is exiting, or the device is being changed, 
//       this function deletes any device dependent objects. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::DeleteDeviceObjects() 
{ 
	TestTiger->CleanUp(); 
	TestPalace->CleanUp(); 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: FrameMove() 
// Desc: Called once per frame, the call is the entry point for animating 
//       the scene. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::FrameMove() 
{ 
	//处理鼠标控制的方向 
	if(m_mouseright==1)		//鼠标右键按下的情况下 才旋转场景 
	{ 
		::GetCursorPos(&newpoint); 
		float fPitch,fYaw; 
		fPitch=-0.002*(oldpoint.y-newpoint.y); 
		fYaw=-0.002*(oldpoint.x-newpoint.x); 
		::SetCursorPos(oldpoint.x,oldpoint.y); 
		TestCamera->RotateCamera_LeftRight(fYaw); 
		TestCamera->RotateCamera_UpDown(fPitch); 
	} 
 
	KeyBoardMove(); 
	TestCamera->SetCamera(m_pd3dDevice); 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: Render() 
// Desc: Called once per frame, the call is the entry point for 3d 
//       rendering. This function sets up render states, clears the 
//       viewport, and renders the scene. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::Render() 
{ 
	// Clear the backbuffer 
   m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
                         0x000000ff, 1.0f, 0x00000000 ); 
    // Begin the scene 
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) 
    { 
	  TestPalace->Render(); 
	  TestTiger->Render(); 
      // End the scene. 
      m_pd3dDevice->EndScene(); 
    } 
 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: FinalCleanup() 
// Desc: Called before the app exits, this function gives the app the chance 
//       to cleanup after itself. 
//----------------------------------------------------------------------------- 
HRESULT CMyD3DApplication::FinalCleanup() 
{ 
    return S_OK; 
} 
 
//----------------------------------------------------------------------------- 
// Name: MsgProc() 
// Desc: Message proc function to handle key and menu input 
//----------------------------------------------------------------------------- 
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, 
                                    LPARAM lParam ) 
{ 
	// Record key presses 
    if( WM_KEYDOWN == uMsg ) 
    { 
        m_bKey[wParam] = 1; 
    } 
 
    // Perform commands when keys are rleased 
    if( WM_KEYUP == uMsg ) 
    { 
        m_bKey[wParam] = 0; 
	} 
    if( WM_RBUTTONDOWN == uMsg ) 
	{ 
		m_mouseright=1; 
		::GetCursorPos(&oldpoint); 
	} 
 
    if( WM_RBUTTONUP == uMsg ) 
	{ 
		m_mouseright=0; 
	} 
	 // Pass remaining messages to default handler 
    return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam ); 
} 
void CMyD3DApplication::KeyBoardMove(void) 
{ 
	//摄像机 
	if(m_bKey['W']==1) TestCamera->MoveCamera_UpDown(.01); 
	if(m_bKey['S']==1) TestCamera->MoveCamera_UpDown(-.01); 
	if(m_bKey['A']==1) TestCamera->MoveCamera_LeftRight(-.01);//左负 
    if(m_bKey['D']==1) TestCamera->MoveCamera_LeftRight(.01);//右正 
    if(m_bKey['Z']==1) TestCamera->MoveCamera_SX(.01);			//上下 
    if(m_bKey['X']==1) TestCamera->MoveCamera_SX(-.01);			//上下 
    //Role平移 
	if(m_bKey[VK_UP])    TestTiger->MoveRoleUpDown(-.01); 
	if(m_bKey[VK_DOWN])  TestTiger->MoveRoleUpDown(.01); 
	if(m_bKey[VK_LEFT])  TestTiger->MoveRoleLeftRight(.01); 
    if(m_bKey[VK_RIGHT]) TestTiger->MoveRoleLeftRight(-.01); 
 
    //Role旋转 
	if(m_bKey['Q']) TestTiger->RotateRoleY(0.01); 
	if(m_bKey['E']) TestTiger->RotateRoleY(-0.01); 
 
    //Role缩放 
	if(m_bKey['J']) TestTiger->ScalMesh(.5); 
	if(m_bKey['K']) TestTiger->ScalMesh(2); 
}