view LightClone/Source/ResourceManager.cpp @ 27:5656c8e382fc

More Gui work; Remove some old files
author koryspansel
date Fri, 16 Sep 2011 18:21:36 -0700
parents 3a63df04f3c0
children 0b729faa4e73
line wrap: on
line source

/*
 * ResourceManager
 */

#include "ResourceManager.h"

/*
 * CreateTextureHelper
 */
ErrorCode CreateTextureHelper(GraphicsDevice* pDevice, const char* pName, IDirect3DTexture9** pTexture)
{
	const uint32 nSizeX		= D3DX_DEFAULT_NONPOW2;
	const uint32 nSizeY		= D3DX_DEFAULT_NONPOW2;
	const uint32 nFilter	= D3DX_DEFAULT;
	const uint32 nFilterMip	= D3DX_DEFAULT;

	return SUCCEEDED(D3DXCreateTextureFromFileExA(*pDevice, pName, nSizeX, nSizeY, 0, 0, D3DFMT_FROM_FILE, D3DPOOL_MANAGED, nFilter, nFilterMip, 0, NULL, NULL, pTexture)) ? Error_Success : Error_Fail;
}

/*
 * CreateEffectHelper
 */
ErrorCode CreateEffectHelper(GraphicsDevice* pDevice, const char* pName, ID3DXEffect** pEffect)
{
	ID3DXBuffer* pBuffer = NULL;
	return SUCCEEDED(D3DXCreateEffectFromFileA(*pDevice, pName, NULL, NULL, 0, NULL, pEffect, &pBuffer)) ? Error_Success : Error_Fail;
}

/*
 * CreateFontHelper
 */
ErrorCode CreateFontHelper(GraphicsDevice* pDevice, const char* pName, uint32 nSize, uint32 nWeight, ID3DXFont** pFont)
{
	const uint32 nCharset	= DEFAULT_CHARSET;
	const uint32 nPrecision	= OUT_DEFAULT_PRECIS;
	const uint32 nQuality	= DEFAULT_QUALITY;
	const uint32 nPitch		= DEFAULT_PITCH | FF_DONTCARE;

	return SUCCEEDED(D3DXCreateFontA(*pDevice, nSize, 0, nWeight, 1, FALSE, nCharset, nPrecision, nQuality, nPitch, pName, pFont)) ? Error_Success : Error_Fail;
}

/*
 * ResourceManager
 */
ResourceManager::ResourceManager()
{
}

/*
 * Initialize
 */
ErrorCode ResourceManager::Initialize(GraphicsDevice* pDevice)
{
	return pGraphicsDevice = pDevice, Error_Success;
}

/* 
 * Terminate
 */
void ResourceManager::Terminate()
{
	TextureCache::Iterator kTexture		= kTextureCache.Begin();
	TextureCache::Iterator kTextureEnd	= kTextureCache.End();

	for(; kTexture != kTextureEnd; ++kTexture)
	{
		(*kTexture)->Release();
	}

	EffectCache::Iterator kEffect		= kEffectCache.Begin();
	EffectCache::Iterator kEffectEnd	= kEffectCache.End();

	for(; kEffect != kEffectEnd; ++kEffect)
	{
		(*kEffect)->Release();
	}

	FontCache::Iterator kFont			= kFontCache.Begin();
	FontCache::Iterator kFontEnd		= kFontCache.Begin();

	for(; kFont != kFontEnd; ++kFont)
	{
		(*kFont)->Release();
	}

	kTextureCache.Clear();
	kEffectCache.Clear();
	kFontCache.Clear();

	pGraphicsDevice = NULL;
}

/*
 * CreateTextureFromFile
 */
ErrorCode ResourceManager::CreateTextureFromFile(const char* pName, IDirect3DTexture9** pTexture)
{
	ErrorCode eCode = Error_Fail;

	if(pGraphicsDevice)
	{
		IDirect3DTexture9** pInstance = kTextureCache.Find(ResourcePath(pName));
		if(!pInstance)
		{
			pInstance = kTextureCache.Add(ResourcePath(pName));
			if(pInstance)
			{
				eCode = CreateTextureHelper(pGraphicsDevice, pName, pInstance);
			}
		}
		else
		{
			eCode = Error_Success;
		}

		if(eCode == Error_Success)
		{
			if(pTexture)
				*pTexture = *pInstance;
		}
	}

	return eCode;
}

/*
 * CreateEffectFromFile
 */
ErrorCode ResourceManager::CreateEffectFromFile(const char* pName, ID3DXEffect** pEffect)
{
	ErrorCode eCode = Error_Fail;

	if(pGraphicsDevice)
	{
		ID3DXEffect** pInstance = kEffectCache.Find(ResourcePath(pName));
		if(!pInstance)
		{
			pInstance = kEffectCache.Add(ResourcePath(pName));
			if(pInstance)
			{
				eCode = CreateEffectHelper(pGraphicsDevice, pName, pInstance);
			}
		}
		else
		{
			eCode = Error_Success;
		}

		if(eCode == Error_Success)
		{
			if(pEffect)
				*pEffect = *pInstance;
		}
	}

	return eCode;
}

/*
 * CreateFontFromName
 */
ErrorCode ResourceManager::CreateFontFromName(const char* pName, uint32 nSize, uint32 nWeight, ID3DXFont** pFont)
{
	ErrorCode eCode = Error_Fail;

	if(pGraphicsDevice)
	{
		ID3DXFont** pInstance = kFontCache.Find(ResourcePath(pName));
		if(!pInstance)
		{
			pInstance = kFontCache.Add(ResourcePath(pName));
			if(pInstance)
			{
				eCode = CreateFontHelper(pGraphicsDevice, pName, nSize, nWeight, pInstance);
			}
		}
		else
		{
			eCode = Error_Success;
		}

		if(eCode == Error_Success)
		{
			if(pFont)
				*pFont = *pInstance;
		}
	}

	return eCode;
}

/*
 * CreateVertexBuffer
 */
ErrorCode ResourceManager::CreateVertexBuffer(uint32 nSize, uint32 nUsage, uint32 nPool, IDirect3DVertexBuffer9** pBuffer)
{
	ErrorCode eCode = Error_Fail;

	if(pGraphicsDevice)
	{
		HRESULT hResult = ((IDirect3DDevice9*)*pGraphicsDevice)->CreateVertexBuffer(nSize, nUsage, 0, (D3DPOOL)nPool, pBuffer, NULL);
		if(SUCCEEDED(hResult))
		{
			eCode = Error_Success;
		}
	}

	return eCode;
}