📄 ch20p2_explosioncluster.cpp
字号:
if (m_timObjectRebirth.IsRunning()) {
for (int q=0; q < NUMSPRITES; q++) {
m_sprExplo[q].Render(m_Camera.GetViewMatrix());
}
}
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
}
char buf[256];
_snprintf(buf, 256, "Position: (%.2f, %.2f, %.2f)",
(float)m_Camera.GetPosition().x, (float)m_Camera.GetPosition().y,
(float)m_Camera.GetPosition().z);
m_pFontSmall->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), buf );
m_pFontSmall->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,0,0),
m_timObjectRebirth.IsRunning() ? "Kablammo!" : "Press B to blow up this poor object." );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );
m_pFontSmall->InitDeviceObjects( m_pd3dDevice );
if( FAILED( m_pObject->Create(m_pd3dDevice, _T("Ch20p2_Object.x") ) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_InputManager.CreateDevices(m_hWnd, false, true);
// create explosion animation
m_pExploAnim = new CAnimSequence(m_pd3dDevice);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame01.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame02.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame03.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame04.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame05.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame06.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame07.dds", 0.03f);
m_pExploAnim->AddFrame("Ch20p2_Explosion_Frame08.dds", 0.03f);
for (int q=0; q < NUMSPRITES; q++) {
m_sprExplo[q].SetAnim(m_pExploAnim);
m_sprExplo[q].SetSize(15.0f);
}
m_pObject->RestoreDeviceObjects( m_pd3dDevice );
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
m_Camera.SetPosition(D3DXVECTOR3(0.0f, 4.0f, -15.0f));
// create the ground plane
m_Ground.RestoreDeviceObjects(m_pd3dDevice, "Ch20p2_GroundTexture.png", 256.0f, 256.0f, 8);
// Set the world matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 0.1f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
// create a light for our Object
D3DLIGHT8 light;
memset(&light, 0, sizeof(light));
light.Ambient.a = 1.0f; light.Ambient.g = light.Ambient.b = light.Ambient.r = 0.0;
light.Diffuse.a = light.Diffuse.g = light.Diffuse.b = 1.0f; light.Diffuse.r = 0.5;
light.Position = D3DXVECTOR3(0.0,5.0,-5.0);
light.Direction = D3DXVECTOR3(0.0,-1.0,1.0);
light.Type = D3DLIGHT_DIRECTIONAL;
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DXCOLOR(0.0f,0.0f,0.0f,0.0f));
m_pd3dDevice->SetLight(0, &light);
m_pd3dDevice->LightEnable(0, true);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
delete m_pExploAnim;
m_InputManager.DestroyDevices();
m_pObject->InvalidateDeviceObjects();
m_pFont->InvalidateDeviceObjects();
m_pFontSmall->InvalidateDeviceObjects();
m_Ground.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()
{
m_pFont->DeleteDeviceObjects();
m_pFontSmall->DeleteDeviceObjects();
m_pObject->Destroy();
m_Ground.DeleteDeviceObjects();
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()
{
SAFE_DELETE( m_pFont );
SAFE_DELETE( m_pFontSmall );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
return S_OK;
}
void CMyD3DApplication::CreateExplosion(CSprite &spr,
float PosX, float PosY, float PosZ,
float RadiusX, float RadiusY, float RadiusZ,
float Size, float SizeDelta,
float Time, float TimeDelta)
{
spr.Pos() = D3DXVECTOR3(PosX, PosY, PosZ) +
RandomNumber(
D3DXVECTOR3(-RadiusX, -RadiusY, -RadiusZ),
D3DXVECTOR3(RadiusX, RadiusY, RadiusZ));
spr.SetSize(Size + RandomNumber(-SizeDelta, SizeDelta));
spr.Timer().BeginWithDelay(Time+RandomNumber(-TimeDelta, TimeDelta));
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
if (uMsg == WM_KEYUP && (wParam == 'b' || wParam == 'B') &&
!m_timObjectRebirth.IsRunning()) {
// one big explosion in the middle
CreateExplosion(m_sprExplo[0],
0.0f, 4.0f, 0.0f, // position
0.0f, 0.0f, 0.0f, // radius
15.0f, 5.0f, // size
0.0f, 0.0f); // time
// several smaller explosions around it
for (int q=1; q < NUMSPRITES; q++) {
CreateExplosion(m_sprExplo[q],
0.0f, 4.0f, 0.0f, // position
3.0f, 3.0f, 3.0f, // radius
10.0f, 5.0f, // size
0.0f, 0.25f); // time
}
m_timObjectRebirth.Begin();
}
// Pass remaining messages to default handler
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -