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

📄 directxobject.cpp

📁 这个是我第一次完成的一个简单的3D ALIEN SHOOTING GAME的RESOURCE MANAGER部分,大家可以看看,然后提点意见~THX
💻 CPP
字号:
// DirectXObject.cpp
//

// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006

#include "Common.h"
#include "DirectXObject.h"

using namespace std;
using namespace Resources;


// DirectXObject - Construction and destruction.

DirectXObject::DirectXObject(LPCSTR lpszFilename /*=NULL*/)
: m_pMesh(NULL)
, m_pMaterials(NULL)
, m_pTextures(NULL)
, m_dwMatCount(0L)
{
	// If filename and path were specified then attempt to
	// load this texture.
	if(lpszFilename != NULL)
	{
		// If read failed then throw an exception.
		if(FAILED(ReadFromFile(lpszFilename)))
		{
			// Generate exception and then throw it.
			ResException e = ResException::UnableToOpenFile EXTRA_DEBUGINFO;
			e.SetExtraInfo(lpszFilename);
			throw e;
		}
	}
}

DirectXObject::~DirectXObject()
{
}


// DirectXObject - Functions which must be overriden.

DWORD DirectXObject::GetUniqueID() const
{
	return DIRECTX_OBJECTX;
}


// DirectXObject - Functions which can be overriden.


// Function Name:	OnProcess
//
// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006
//
// Description:		Can be overriden for processing.
//
void DirectXObject::OnProcess(float nTime)
{
}

// Function Name:	OnRender
//
// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006
//
// Description:		Simply renders mesh to device. Any world transforms
//					must happen prior to calling this function.
//
void DirectXObject::OnRender(float nTime)
{
	// Retrieve DirectX device.
	LPDIRECT3DDEVICE9 pDevice = ResourceManager::GetActiveDevice();

	// Render each material subset.
	for(DWORD i = 0; i < m_dwMatCount; i++)
	{
		// Set the material and texture for this subset
		pDevice->SetMaterial(&m_pMaterials[i]);
		m_pTextures[i]->SelectTexture(0);

		// Draw the mesh subset
		m_pMesh->DrawSubset(i);
	}
}

// Function Name:	OnDestroy
//
// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006
//
// Description:		Perform automatic cleanup.
//
void DirectXObject::OnDestroy()
{
	// Clean up object object.
	CleanUp();
}

// Function Name:	ReadFromFile
//
// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006
//
// Description:		Attempt to read object resource from an x file.
//					Note: Result is returned.
//
HRESULT DirectXObject::ReadFromFile(LPCSTR lpszFilename)
{
	// Retrieve resolved resource filename and path.
	string szFilePath = ResourceManager::ResolveFilePath(lpszFilename, "Model");
	if(szFilePath.size() > 0) szFilePath += '\\';
	szFilePath += lpszFilename;

	// Clean up texture object.
	CleanUp();

	// Will point at material buffer.
	LPD3DXBUFFER pD3DXMtrlBuffer;

	// Attempt to load mesh specified.
	HRESULT hResult = D3DXLoadMeshFromX(szFilePath.c_str(), D3DXMESH_SYSTEMMEM,
		ResourceManager::GetActiveDevice(), NULL, &pD3DXMtrlBuffer,
		NULL, &m_dwMatCount, &m_pMesh);

	if(FAILED(hResult)) return hResult;

	// Note:	When filenames are specified a matching path will be
	//			be found. However when the path is found the filename
	//			and path paramter within the resource manager will be
	//			stored; we do not want to store the entire filename
	//			and path; just the original.
	//
	//			Doing this will improve upon efficiency.
	//
	SetFilePath(lpszFilename);

	// Retrieve materials.
	D3DXMATERIAL* d3dxMaterials =
		(D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
	m_pMaterials = new D3DMATERIAL9[m_dwMatCount];
	if(m_pMaterials == NULL)
		return E_OUTOFMEMORY;

	m_pTextures = new DirectXTexture*[GetMaterialCount()];
	if(m_pTextures == NULL)
		return E_OUTOFMEMORY;

	try
	{
		for(DWORD i = 0; i < m_dwMatCount; i++ )
		{
			// Copy the material
			m_pMaterials[i] = d3dxMaterials[i].MatD3D;

			// Set the ambient color for the material.
			m_pMaterials[i].Ambient = m_pMaterials[i].Diffuse;

			m_pTextures[i] = ResourceManager::AquireTexture(
				d3dxMaterials[i].pTextureFilename).ToDirectXTexture();
		}
	}
	catch(HRESULT hError)
	{
		// Set error result.
		hResult = hError;
	}

	// Done with the material buffer
	pD3DXMtrlBuffer->Release();

	return hResult;
}


// DirectXObject - Properties.


// Function Name:	CleanUp
//
// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006
//
// Description:		Prepare object for reuse.
//
void DirectXObject::CleanUp()
{
	// Free object resource from memory.
	if(!!m_pMaterials)
	{
		delete[] m_pMaterials;
		m_pMaterials = NULL;
	}

	// Free texture resources from memory.
	if(!!m_pTextures)
	{
		for(DWORD i = 0; i < m_dwMatCount; i++)
		{
			m_pTextures[i]->Release();
		}

		delete[] m_pTextures;
		m_pTextures = NULL;
	}

	// Free mesh from memory.
	if(!!m_pMesh)
	{
		m_pMesh->Release();
		m_pMesh = NULL;
	}
}

⌨️ 快捷键说明

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