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

📄 ch21p1_firingrange.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
  
	ProcessInput();
  // these are done here so that you can move the camera around during a freeze
  // frame, ala The Matrix
  D3DXMATRIX matWorld;
  D3DXMatrixIdentity( &matWorld );
  m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

  ProcessInput();
  m_Camera.Update(m_fElapsedTime);

  m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
  m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
  m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
  
  // Clear the backbuffer
  m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                       0x000000, 1.0f, 0L );
  
  // Begin the scene  
  if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  {
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
		m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
		
		m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
		m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

    // draw skybox
		m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
		m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
		m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
		m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.GetViewMatrix());
    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
    m_SkyBox.Render(m_Camera.GetViewMatrix());

    
		// draw ground plane
		m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
		m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.GetViewMatrix());
		m_pd3dDevice->SetTexture( 0, m_GroundPlane.GetTexture() );
    m_pd3dDevice->SetStreamSource( 0, m_GroundPlane.GetVB(), sizeof(VERTEX_XYZ_DIFFUSE_TEX1) );
    m_pd3dDevice->SetIndices( m_GroundPlane.GetIB(), 0L );
    m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 
      m_GroundPlane.GetNumVerts(), 0, m_GroundPlane.GetNumIndices()/3 );

		// draw gun
		m_pCurGun->Render(m_Camera);

		// draw plasma bullets
		m_PlasmaBulletArray.RenderAll(m_Camera);
		
    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 );
    
    // 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 );
	m_SkyBox.SetSize(100.0f);
  
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
	m_iLightPosX = 0;
	m_iLightPosY = 0;

	m_InputManager.CreateDevices(m_hWnd, true, true);

  m_SkyBox.RestoreDeviceObjects(m_pd3dDevice,
    "Ch21p1_DesertSkyboxTop.bmp",
    "Ch21p1_DesertSkyboxBottom.bmp",
    "Ch21p1_DesertSkyboxFront.bmp",
    "Ch21p1_DesertSkyboxBack.bmp",
    "Ch21p1_DesertSkyboxLeft.bmp",
    "Ch21p1_DesertSkyboxRight.bmp"
    );

	m_GroundPlane.RestoreDeviceObjects(m_pd3dDevice, "Ch21p1_GroundTexture.bmp", 512.0f, 512.0f, 16);

	m_PlasmaGun.RestoreDeviceObjects(m_pd3dDevice, "Ch21p1_Gun.x");
	m_PlasmaBulletArray.RestoreDeviceObjects(m_pd3dDevice);
	m_PlasmaGun.SetBulletArray(&m_PlasmaBulletArray);

	m_LaserGun.RestoreDeviceObjects(m_pd3dDevice, "Ch21p1_Gun.x");
	m_MachineGun.RestoreDeviceObjects(m_pd3dDevice, "Ch21p1_Gun.x");

	m_PlasmaGun.Pos() = D3DXVECTOR3(0.5f, -0.6f, 1.0f);
	m_LaserGun.Pos() = D3DXVECTOR3(0.5f, -0.6f, 1.0f);
	m_MachineGun.Pos() = D3DXVECTOR3(0.5f, -0.6f, 1.0f);

	m_pCurGun = &m_MachineGun;

	m_pFont->RestoreDeviceObjects();
  m_pFontSmall->RestoreDeviceObjects();
  m_Camera.SetPosition(D3DXVECTOR3(0.0f, 7.0f, -20.0f));
	
	// 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 );
  
  return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
  m_InputManager.DestroyDevices();
  m_pFont->InvalidateDeviceObjects();
  m_pFontSmall->InvalidateDeviceObjects();
  m_SkyBox.InvalidateDeviceObjects();
	m_GroundPlane.InvalidateDeviceObjects();
	
	m_PlasmaGun.InvalidateDeviceObjects();
	m_PlasmaBulletArray.InvalidateDeviceObjects();

	m_LaserGun.InvalidateDeviceObjects();
	m_MachineGun.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_GroundPlane.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;
}

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                                    LPARAM lParam )
{
  // Pass remaining messages to default handler
  return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

⌨️ 快捷键说明

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