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

📄 directxtexture.cpp

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

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

#include "Common.h"
#include "DirectXTexture.h"

using namespace std;
using namespace Resources;


// DirectXTexture - Construction and destruction.

DirectXTexture::DirectXTexture(LPCSTR lpszFilename /*=NULL*/)
{
	// 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;
		}
	}
}

DirectXTexture::~DirectXTexture()
{
}


// DirectXTexture - Functions which must be overriden.

DWORD DirectXTexture::GetUniqueID() const
{
	return DIRECTX_TEXTURE;
}


// DirectXTexture - Functions which can be overriden.

void DirectXTexture::OnDestroy()
{
	// Clean up texture object.
	CleanUp();
}

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

	// Clean up texture object.
	CleanUp();

	// Attempt to open texture specified.
	HRESULT hResult = D3DXCreateTextureFromFile(
		ResourceManager::GetActiveDevice(), szFilePath.c_str(), &m_pTexture);

	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 image dimensions.
	D3DSURFACE_DESC desc;
	m_pTexture->GetLevelDesc(0, &desc);

	// Store image dimensions.
	m_nWidth = desc.Width;
	m_nHeight = desc.Height;

	return NOERROR;
}


// DirectXTexture - Properties.


// Function Name:	CleanUp
//
// Author:			Lea Hayes
// Date Created:	12/03/2006
// Date Modified:	12/03/2006
//
// Description:		Prepare object for reuse.
//
void DirectXTexture::CleanUp()
{
	// Free texture resource from memory.
	if(m_pTexture != NULL)
	{
		m_pTexture->Release();
		m_pTexture = NULL;
	}

	// Reset texture details.
	m_nWidth = NULL;
	m_nHeight = NULL;
}

// Function Name:	SelectTexture
//
// Author:			Lea Hayes
// Date Created:	13/03/2006
// Date Modified:	13/03/2006
//
// Description:		Make texture active for the active device.
//					Returns previously active texture.
//
LPDIRECT3DBASETEXTURE9 DirectXTexture::SelectTexture(DWORD dwStage /*=0*/)
{
	// Update texture selection and return previous.
	LPDIRECT3DBASETEXTURE9 pPrev = NULL;
	ResourceManager::GetActiveDevice()->GetTexture(dwStage, &pPrev);
	ResourceManager::GetActiveDevice()->SetTexture(dwStage, m_pTexture);

	return pPrev;
}

⌨️ 快捷键说明

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