www.pudn.com > notWow.rar > M2DX.cpp


//-------------------------------------------------------------------------------------- 
// File: EmptyProject.cpp 
// 
// Empty starting point for new Direct3D applications 
// 
// Copyright (c) Microsoft Corporation. All rights reserved. 
//-------------------------------------------------------------------------------------- 
#include "dxstdafx.h" 
#include "resource.h" 
#include "Model.h" 
#include "Input.h" 
#include "DXUT.h" 
#include "ModelMgr.h" 
#include "HeroCtrl.h" 
#include "SceneMgr.h" 
#include "Config.h" 
 
ID3DXFont*              g_pFont = NULL;         // Font for drawing text 
ID3DXSprite*            g_pSprite = NULL;       // Sprite for batching draw text calls 
 
 
int						globalTime = 0;			// Used in .m2 model 
NPCID					g_KateID; 
Random					g_Random; 
ResLoader				g_ResLdr; 
TexturesManager         g_TexMgr; 
ModelsManager			g_MdlMgr; 
ScenesManager			g_SceneMgr; 
HeroController			g_HeroCtrl; 
Config					g_Config; 
 
Camera					g_Camera; 
 
Input					g_Input; 
Keyboard				g_Keyboard; 
Mouse					g_Mouse; 
 
 
//-------------------------------------------------------------------------------------- 
// Rejects any devices that aren't acceptable by returning false 
//-------------------------------------------------------------------------------------- 
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,  
                                  D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext ) 
{ 
    // Typically want to skip backbuffer formats that don't support alpha blending 
    IDirect3D9* pD3D = DXUTGetD3DObject();  
    if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, 
                    AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,  
                    D3DRTYPE_TEXTURE, BackBufferFormat ) ) ) 
        return false; 
 
    return true; 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Before a device is created, modify the device settings as needed 
//-------------------------------------------------------------------------------------- 
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext ) 
{    // Turn vsync off 
    pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 
//    g_SettingsDlg.GetDialogControl()->GetComboBox( DXUTSETTINGSDLG_PRESENT_INTERVAL )->SetEnabled( false ); 
 
    // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW  
    // then switch to SWVP. 
    if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 || 
         pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) ) 
    { 
        pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING; 
    } 
 
    // Debugging vertex shaders requires either REF or software vertex processing  
    // and debugging pixel shaders requires REF.   
#ifdef DEBUG_VS 
    if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF ) 
    { 
        pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING; 
        pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;                             
        pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; 
    } 
#endif 
#ifdef DEBUG_PS 
    pDeviceSettings->DeviceType = D3DDEVTYPE_REF; 
#endif 
    // For the first device created if its a REF device, optionally display a warning dialog box 
    static bool s_bFirstTime = true; 
    if( s_bFirstTime ) 
    { 
        s_bFirstTime = false; 
        if( pDeviceSettings->DeviceType == D3DDEVTYPE_REF ) 
            DXUTDisplaySwitchingToREFWarning(); 
    } 
 
    return true; 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Create any D3DPOOL_MANAGED resources here  
//-------------------------------------------------------------------------------------- 
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) 
{     
	HRESULT hr; 
 
	// Initialize the font 
    V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,  
                              OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,  
                              L"Arial", &g_pFont ) ); 
	FLOAT fAspect = ((FLOAT)pBackBufferSurfaceDesc->Width) / pBackBufferSurfaceDesc->Height; 
	g_Camera.Init(g_Config.GetFogDist(),g_Config.GetVisDist(),g_Config.GetMaxDist(),(float)(g_Config.GetFogParam()) / 100,fAspect); 
	g_TexMgr.Init(pd3dDevice); 
	g_MdlMgr.Init(pd3dDevice); 
	g_SceneMgr.Init(pd3dDevice); 
	SceneID SID = g_SceneMgr.AddScene("TownOut.map",0); 
	g_SceneMgr.SetCurScene(SID); 
	g_HeroCtrl.Init(NULL,0); 
 
	 
	Scene *pScene = g_SceneMgr.GetCurScene(); 
	NPCID ID = pScene->GetNPCsMgr()->AddNPC("HumanMale.m2",MODEL_CHARACTER,0); 
	Char* NPC = g_SceneMgr.GetCurScene()->GetNPCsMgr()->GetNPC(ID); 
	NPC->m_TexturesID[0] = g_TexMgr.AddTexture("HumanMale0.dds"); 
	NPC->m_Geosets[0] = 1; 
	NPC->m_Geosets[1] = 1; 
	NPC->m_Geosets[2] = 1; 
	NPC->m_Geosets[3] = 1; 
	NPC->m_Geosets[4] = 1; 
	NPC->m_Geosets[5] = 1; 
	NPC->m_Geosets[6] = 1; 
	NPC->m_Geosets[7] = 1; 
	NPC->m_Geosets[8] = 1; 
	NPC->m_Geosets[9] = 1; 
	NPC->m_Geosets[10] = 1; 
	NPC->m_Geosets[11] = 1; 
	NPC->m_Geosets[12] = 1; 
	NPC->m_Geosets[13] = 1; 
	NPC->m_Geosets[14] = 1; 
	NPC->m_Geosets[15] = 1; 
 
	NPC->m_fRunSpeed = 5.0; 
	NPC->m_fWalkSpeed = 3.0; 
	NPC->m_fPosX = 176.5; 
	NPC->m_fPosZ = 108.5; 
	NPC->m_fPosY = pScene->GetMap()->GetY(NPC->m_fPosX,NPC->m_fPosZ); 
	NPC->m_Dir = 0; 
	NPC->Turn(g_HeroCtrl.GetHero()); 
 
	g_KateID = pScene->GetNPCsMgr()->AddNPC("HumanFemale.m2",MODEL_CHARACTER,0); 
	NPC = g_SceneMgr.GetCurScene()->GetNPCsMgr()->GetNPC(g_KateID); 
	NPC->m_TexturesID[0] = g_TexMgr.AddTexture("Kate.dds"); 
	NPC->m_TexturesID[1] = g_TexMgr.AddTexture("Kate0.dds"); 
	NPC->m_Geosets[0] = 5; 
	NPC->m_Geosets[1] = 1; 
	NPC->m_Geosets[2] = 1; 
	NPC->m_Geosets[3] = 1; 
	NPC->m_Geosets[4] = 1; 
	NPC->m_Geosets[5] = 1; 
	NPC->m_Geosets[6] = 1; 
	NPC->m_Geosets[7] = 1; 
	NPC->m_Geosets[8] = 1; 
	NPC->m_Geosets[9] = 1; 
	NPC->m_Geosets[10] = 1; 
	NPC->m_Geosets[11] = 1; 
	NPC->m_Geosets[12] = 1; 
	NPC->m_Geosets[13] = 2; 
	NPC->m_Geosets[14] = 1; 
	NPC->m_Geosets[15] = 1; 
 
	NPC->m_fRunSpeed = 5.5; 
	NPC->m_fWalkSpeed = 3.0; 
	NPC->m_fPosX = 119.5; 
	NPC->m_fPosZ = 162.5; 
	NPC->m_fPosY = pScene->GetMap()->GetY(NPC->m_fPosX,NPC->m_fPosZ); 
	NPC->m_Dir = 180; 
	//NPC->Turn(g_HeroCtrl.GetHero()); 
 
	return S_OK; 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Create any D3DPOOL_DEFAULT resources here  
//-------------------------------------------------------------------------------------- 
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,  
                                const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) 
{ 
	HRESULT hr; 
	if( g_pFont ) 
        V_RETURN( g_pFont->OnResetDevice() ); 
	V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite ) ); 
    // Set up the textures 
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE ); 
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); 
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE ); 
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); 
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); 
    pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); 
    pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); 
 
	if(g_Config.MipFiler() == 2) 
		pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); 
	else if(g_Config.MipFiler() == 1) 
		pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT ); 
	else 
		pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE ); 
 
	// Set miscellaneous render states 
    pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE ); 
    pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE ); 
    pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE ); 
    pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x000F0F0F ); 
 
	pd3dDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x000000A0); 
	pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); 
	pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); 
	pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); 
 
    // Set the projection matrix 
    FLOAT fAspect = ((FLOAT)pBackBufferSurfaceDesc->Width) / pBackBufferSurfaceDesc->Height; 
    g_Camera.Reset(fAspect); 
	 
 
    return S_OK; 
} 
 
//-------------------------------------------------------------------------------------- 
// Handle updates to the scene 
//-------------------------------------------------------------------------------------- 
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) 
{ 
 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Render the scene  
//-------------------------------------------------------------------------------------- 
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) 
{ 
	static int KateState = 0; 
	Char* NPC = g_SceneMgr.GetCurScene()->GetNPCsMgr()->GetNPC(g_KateID); 
	if(g_Keyboard.GetKeyState(DIK_F)) 
	{ 
		g_Keyboard.SetLock(DIK_F);	 
		NPC->Idle(); 
		if(KateState != 2) 
		{ 
			NPC->Run(g_HeroCtrl.GetHero()); 
			KateState = 2; 
		} 
		else 
		{ 
			KateState = 1; 
			NPC->Turn(g_HeroCtrl.GetHero()); 
		} 
	} 
	if(g_Keyboard.GetKeyState(DIK_T)) 
	{ 
		g_Keyboard.SetLock(DIK_T); 
		NPC->Idle(); 
		if(KateState < 1) 
		{ 
			NPC->Turn(g_HeroCtrl.GetHero()); 
			KateState = 1; 
		} 
	} 
 
	g_Mouse.Read(); 
	g_Keyboard.Read(); 
 
	g_HeroCtrl.Update(fElapsedTime); 
	g_Camera.Update(fElapsedTime,g_HeroCtrl.GetHero()->m_fPosX,g_HeroCtrl.GetHero()->m_fPosY, 
						  g_HeroCtrl.GetHero()->m_fPosZ,g_HeroCtrl.GetHero()->m_Dir); 
 
	Scene* pScene =  g_SceneMgr.GetCurScene(); 
	pScene->Update(fElapsedTime); 
 
    HRESULT hr; 
	// Clear the render target and the zbuffer  
    V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 96, 129, 183), 1.0f, 0) ); 
 
    // Render the scene 
    if( SUCCEEDED( pd3dDevice->BeginScene() ) ) 
    { 
		g_HeroCtrl.Render(); 
 
		pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW); 
		pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 
		pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE , FALSE);  
 
		Scene* pScene =  g_SceneMgr.GetCurScene(); 
		pScene->Render(); 
	 
		pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW); 
		pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 
		pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE , FALSE);  
		 
		CDXUTTextHelper txtHelper( g_pFont, g_pSprite, 15 ); 
		// Output statistics 
		txtHelper.Begin(); 
		txtHelper.SetInsertionPos( 2, 0 ); 
		txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) ); 
		txtHelper.DrawTextLine( DXUTGetFrameStats(true) ); 
		txtHelper.DrawTextLine( DXUTGetDeviceStats() ); 
		txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) ); 
		txtHelper.End(); 
	 
        V( pd3dDevice->EndScene() ); 
    } 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Handle messages to the application  
//-------------------------------------------------------------------------------------- 
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,  
                          bool* pbNoFurtherProcessing, void* pUserContext ) 
{ 
    return 0; 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Release resources created in the OnResetDevice callback here  
//-------------------------------------------------------------------------------------- 
void CALLBACK OnLostDevice( void* pUserContext ) 
{ 
	if( g_pFont ) 
		g_pFont->OnLostDevice(); 
	SAFE_RELEASE(g_pSprite); 
} 
 
 
//-------------------------------------------------------------------------------------- 
// Release resources created in the OnCreateDevice callback here 
//-------------------------------------------------------------------------------------- 
void CALLBACK OnDestroyDevice( void* pUserContext ) 
{ 
 
	g_SceneMgr.Free(); 
	g_MdlMgr.Free(); 
	g_TexMgr.Free(); 
	 
	SAFE_RELEASE(g_pFont); 
 
//	_CrtDumpMemoryLeaks(); 
} 
 
//-------------------------------------------------------------------------------------- 
// Initialize everything and go into a render loop 
//-------------------------------------------------------------------------------------- 
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 
 
    // Set the callback functions 
    DXUTSetCallbackDeviceCreated( OnCreateDevice ); 
    DXUTSetCallbackDeviceReset( OnResetDevice ); 
    DXUTSetCallbackDeviceLost( OnLostDevice ); 
    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice ); 
    DXUTSetCallbackMsgProc( MsgProc ); 
    DXUTSetCallbackFrameRender( OnFrameRender ); 
    DXUTSetCallbackFrameMove( OnFrameMove ); 
    
    // TODO: Perform any application-level initialization here 
 
    // Initialize DXUT and create the desired Win32 window and Direct3D device for the application 
    DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes 
    DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen 
    DXUTCreateWindow( L"EmptyProject" ); 
	g_Config.Init("Config.ini"); 
    DXUTCreateDevice( D3DADAPTER_DEFAULT,g_Config.Windowed(), g_Config.GetScreenWidth(),g_Config.GetScreenHeight(), IsDeviceAcceptable, ModifyDeviceSettings ); 
	 
	g_Input.Init(DXUTGetHWND(), DXUTGetHINSTANCE()); 
	g_Keyboard.Init(&g_Input); 
	g_Mouse.Init(&g_Input); 
 
 
    // Start the render loop 
    DXUTMainLoop(); 
 
    // TODO: Perform any application-level cleanup here 
//	g_TexMgr.Free(); 
	g_Mouse.Free(); 
	g_Keyboard.Free(); 
	g_Input.Free(); 
    return DXUTGetExitCode(); 
}