📄 resman.cpp
字号:
// ResMan.cpp
//
// Author: Lea Hayes
// Date Created: 10/03/2006
// Date Modified: 24/03/2006
#include "Common.h"
#include "ResMan.h"
#include "DirectXTexture.h"
#include "DirectXObject.h"
#include "DirectXFont.h"
using namespace std;
using namespace Resources;
// The one and only resource manager.
ResourceManager ResourceManager::m_sGlobal;
// ResourceManager - Construction and destruction.
ResourceManager::ResourceManager()
{
}
ResourceManager::~ResourceManager()
{
// Release and destroy all resources.
Destroy();
}
// ResourceManager - Resource handles.
const ResourceManager::ResHandle ResourceManager::NullHandle(NULL);
// Function Name: Destroy
//
// Author: Lea Hayes
// Date Created: 12/03/2006
// Date Modified: 12/03/2006
//
// Description: Release all resources from memory.
//
void ResourceManager::Destroy()
{
m_sGlobal.m_arDirectX.Destroy();
m_sGlobal.m_arCustom.Destroy();
}
// ResourceManager - File and path management.
// Function Name: CheckFilePath
//
// Author: Lea Hayes
// Date Created: 24/03/2006
// Date Modified: 24/03/2006
//
// Description: Verify filename and path specified exist.
//
bool ResourceManager::CheckFilePath(LPCSTR lpszFilePath)
{
// Attempt to open file specified.
FILE *pFile = fopen(lpszFilePath, "r");
// If file was opened succesfully then...
if(pFile != NULL)
{
// Close the file and return with success.
fclose(pFile);
return true;
}
// Otherwise the file was not found so return with failure.
return false;
}
// Function Name: ResolveFilePath
//
// Author: Lea Hayes
// Date Created: 15/03/2006
// Date Modified: 24/03/2006
//
// Description: Find nearest path for filename specified.
//
// When specified, the resource folder specified
// is searched for the required resource. When the
// resource is not found there the root directory
// is searched.
//
LPCSTR ResourceManager::ResolveFilePath(LPCSTR lpszFilename,
LPCSTR lpszResFolder /*=NULL*/)
{
// If resource directory was specified then...
if(lpszResFolder != NULL)
{
// Check resource directory for file.
string szNewDir = lpszResFolder;
szNewDir += '\\';
szNewDir += lpszFilename;
// Verify file exists.
if(CheckFilePath(szNewDir.c_str()))
{
// Return default filename.
return lpszResFolder;
}
}
// Resource was not found within a specialised resource folder;
// search within executable root directory.
return "";
}
// ResourceManager - Resource management.
// Function Name: PreloadTexture
//
// Author: Lea Hayes
// Date Created: 13/03/2006
// Date Modified: 13/03/2006
//
// Description: Preload texture resource.
// Note: Duplicate resources will be ignored.
//
HRESULT ResourceManager::PreloadTexture(LPCSTR lpszFilename)
{
try
{
// When preloading resources they will obviously not be
// in use initially. Ensure auto free mode is disabled.
m_sGlobal.m_arDirectX.EnableAutoFreeUnused(false);
// Attempt to find resource within manager.
ResourceArray::iterator itResult =
m_sGlobal.m_arDirectX.Find(lpszFilename);
// If resource was not found then return a null handle.
if(itResult == ResourceArray::NullIterator)
{
// Attempt to load a new texture resource.
m_sGlobal.m_arDirectX.Append(
OPEN_RESOURCE(DirectXTexture, lpszFilename));
}
return NOERROR;
}
catch(HRESULT hError)
{
return hError;
}
}
// Function Name: PreloadObjectX
//
// Author: Lea Hayes
// Date Created: 13/03/2006
// Date Modified: 13/03/2006
//
// Description: Preload x file object resource.
// Note: Duplicate resources will be ignored.
//
HRESULT ResourceManager::PreloadObjectX(LPCSTR lpszFilename)
{
try
{
// When preloading resources they will obviously not be
// in use initially. Ensure auto free mode is disabled.
m_sGlobal.m_arDirectX.EnableAutoFreeUnused(false);
// Attempt to find resource within manager.
ResourceArray::iterator itResult =
m_sGlobal.m_arDirectX.Find(lpszFilename);
// If resource was not found then return a null handle.
if(itResult == ResourceArray::NullIterator)
{
// Attempt to load a new texture resource.
m_sGlobal.m_arDirectX.Append(
OPEN_RESOURCE(DirectXObject, lpszFilename));
}
return NOERROR;
}
catch(HRESULT hError)
{
return hError;
}
}
// Function Name: PreloadFont
//
// Author: Lea Hayes
// Date Created: 13/03/2006
// Date Modified: 13/03/2006
//
// Description: Preload font resource.
// Note: Duplicate resources will be ignored.
//
HRESULT ResourceManager::PreloadFont(LPCSTR lpszFontName,
INT nHeight,
UINT nWidth,
bool bBold,
bool bItalic,
UINT nMipLevels,
LPCSTR lpszTypeFace)
{
try
{
// When preloading resources they will obviously not be
// in use initially. Ensure auto free mode is disabled.
m_sGlobal.m_arDirectX.EnableAutoFreeUnused(false);
// Attempt to find resource within manager.
ResourceArray::iterator itResult =
m_sGlobal.m_arDirectX.Find(lpszFontName);
// If resource was not found then return a null handle.
if(itResult == ResourceArray::NullIterator)
{
// Attempt to load a new texture resource.
m_sGlobal.m_arDirectX.Append(
CREATE_RESOURCE(DirectXFont(lpszFontName, nHeight, nWidth,
bBold, bItalic, nMipLevels, lpszTypeFace)));
}
return NOERROR;
}
catch(HRESULT hError)
{
return hError;
}
}
// Function Name: AppendCustom
//
// Author: Lea Hayes
// Date Created: 13/03/2006
// Date Modified: 13/03/2006
//
// Description: Append custom resource into manager.
//
void ResourceManager::AppendCustom(LPRESOURCE pCustomRes)
{
// When preloading resources they will obviously not be
// in use initially. Ensure auto free mode is disabled.
m_sGlobal.m_arCustom.EnableAutoFreeUnused(false);
// Simply append the custom resource.
m_sGlobal.m_arCustom.Append(pCustomRes);
}
// Function Name: AquireTexture
//
// Author: Lea Hayes
// Date Created: 12/03/2006
// Date Modified: 12/03/2006
//
// Description: Aquire texture from DirectX container. If texture
// has not already been loaded then create and load the
// texture resource.
//
ResourceManager::ResHandle ResourceManager::AquireTexture(LPCSTR lpszFilename)
{
// Resource handle to return; assume failure.
ResHandle handleResult = NullHandle;
// Attempt to find resource within manager.
ResourceArray::iterator itResult =
m_sGlobal.m_arDirectX.Find(lpszFilename);
// If resource was not found then return a null handle.
if(itResult == ResourceArray::NullIterator)
{
// Attempt to load a new texture resource.
m_sGlobal.m_arDirectX.Append(
OPEN_RESOURCE(DirectXTexture, lpszFilename));
handleResult = *m_sGlobal.m_arDirectX.Last();
}
// Otherwise retrieve a resource handle.
else
{
handleResult = *itResult;
}
// Return the resulting resource handle.
return m_sGlobal.m_arDirectX.Aquire(handleResult);
}
// Function Name: AquireObjectX
//
// Author: Lea Hayes
// Date Created: 13/03/2006
// Date Modified: 13/03/2006
//
// Description: Aquire x file object from DirectX container. If texture
// has not already been loaded then create and load the
// texture resource.
//
ResourceManager::ResHandle ResourceManager::AquireObjectX(LPCSTR lpszFilename)
{
// Resource handle to return; assume failure.
ResHandle handleResult = NullHandle;
// Attempt to find resource within manager.
ResourceArray::iterator itResult =
m_sGlobal.m_arDirectX.Find(lpszFilename);
// If resource was not found then return a null handle.
if(itResult == ResourceArray::NullIterator)
{
// Attempt to load a new texture resource.
m_sGlobal.m_arDirectX.Append(
OPEN_RESOURCE(DirectXObject, lpszFilename));
handleResult = *m_sGlobal.m_arDirectX.Last();
}
// Otherwise retrieve a resource handle.
else
{
handleResult = *itResult;
}
// Return the resulting resource handle.
return m_sGlobal.m_arDirectX.Aquire(handleResult);
}
// Function Name: AquireFont
//
// Author: Lea Hayes
// Date Created: 13/03/2006
// Date Modified: 13/03/2006
//
// Description: Aquire font from DirectX container. If texture has not
// already been loaded then create new instance and load the
// texture resource.
//
ResourceManager::ResHandle ResourceManager::AquireFont(LPCSTR lpszFontName,
INT nHeight,
UINT nWidth,
bool bBold,
bool bItalic,
UINT nMipLevels,
LPCSTR lpszTypeFace)
{
// Resource handle to return; assume failure.
ResHandle handleResult = NullHandle;
// Attempt to find resource within manager.
ResourceArray::iterator itResult =
m_sGlobal.m_arDirectX.Find(lpszFontName);
// If resource was not found then return a null handle.
if(itResult == ResourceArray::NullIterator)
{
// Attempt to load a new texture resource.
m_sGlobal.m_arDirectX.Append(
CREATE_RESOURCE(DirectXFont(lpszFontName, nHeight, nWidth,
bBold, bItalic, nMipLevels, lpszTypeFace)));
handleResult = *m_sGlobal.m_arDirectX.Last();
}
// Otherwise retrieve a resource handle.
else
{
handleResult = *itResult;
}
// Return the resulting resource handle.
return m_sGlobal.m_arDirectX.Aquire(handleResult);
}
// ResourceManager - Properties.
// Function Name: EnableAutoFreeUnused
//
// Author: Lea Hayes
// Date Created: 12/03/2006
// Date Modified: 12/03/2006
//
// Description: Enable or disable auto resource memory cleanup
// when a resource is nolonger in use.
//
// Warning: If resources are frequently put in a
// state of inactivity followed by use
// it may be more efficient to disable
// this feature.
//
void ResourceManager::EnableAutoFreeUnused(bool bEnable /*=true*/)
{
// Set this feature for all subsequent resource arrays.
m_sGlobal.m_arDirectX.EnableAutoFreeUnused(bEnable);
m_sGlobal.m_arCustom.EnableAutoFreeUnused(bEnable);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -