00001
#include "StdAfx.h"
00002
#include "TextureManager.h"
00003
00004 CTextureManager::CTextureManager()
00005 {
00006
SetEntityType(Entity_TextureManager);
00007
CGlobalLogger::GetSingleton().
Write(
"Texture Manager - Instantiated\n");
00008 }
00009
00010 CTextureManager::~CTextureManager()
00011 {
00012
Destroy();
00013
CGlobalLogger::GetSingleton().
Write(
"Texture Manager - Destroyed\n");
00014 }
00015
00016 CTexture *
CTextureManager::AddResource(CString strName, UINT uiID, eEntityType ResourceType)
00017 {
00018
switch(ResourceType)
00019 {
00020
case Entity_Default:
00021
case Entity_Texture:
00022
case Entity_OpenGLTexture:
00023
CTexture *pNewTexture =
new COpenGLTexture;
00024
00025 pNewTexture->
SetID(uiID);
00026 pNewTexture->
SetName(strName);
00027
00028
push_back(pNewTexture);
00029
00030
return pNewTexture;
00031
break;
00032 }
00033
00034
return NULL;
00035 }
00036
00037 int CTextureManager::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00038 {
00039
TiXmlElement* pXMLElement = NULL;
00040
const char *pcValue = NULL;
00041
00042
int iRetValue =
CResourceManager::LoadXML(pDataNode, strFilename);
00043
if(iRetValue !=1)
00044
return iRetValue;
00045
00046 pXMLElement = m_pXMLElement;
00047
00048
if(stricmp(pXMLElement->
pcValue(),
"Texture") == 0)
00049 {
00050
CXMLResource *AddedResource =
LoadResource(pDataNode, strFilename);
00051
if(AddedResource !=NULL)
00052 {
00053
CGlobalLogger::GetSingleton().
Write(
"Texture (Name: %s, ID: %d) has been added successfully.\n", AddedResource->
GetName().GetBuffer(), AddedResource->
GetID());
00054
return 1;
00055 }
00056
else
00057
return 0;
00058 }
00059
00060 UINT TextureLoadedCount = 0;
00061
TiXmlNode *pIterXMLNode = pXMLElement->
FirstChild();
00062
while(pIterXMLNode !=NULL)
00063 {
00064
if(stricmp(pIterXMLNode->
pcValue(),
"Texture") == 0)
00065 {
00066
CXMLResource *AddedResource =
LoadResource(pIterXMLNode,
"");
00067
if(AddedResource !=NULL)
00068 {
00069
CGlobalLogger::GetSingleton().
Write(
"Texture (Name: %s, ID: %d) has been added successfully.\n", AddedResource->
GetName().GetBuffer(), AddedResource->
GetID());
00070 TextureLoadedCount++;
00071 }
00072 }
00073
00074 pIterXMLNode = pXMLElement->
IterateChildren(pIterXMLNode);
00075 }
00076
00077
return TextureLoadedCount;
00078 }
00079
00080 int CTextureManager::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00081 {
00082
00083
if(pDataNode == NULL && strFilename ==
"")
00084
return -1;
00085
00086
char Buf[128] =
"";
00087
TiXmlElement *pSaveXMLElement = NULL;
00088
00089
if(pDataNode != NULL)
00090 pSaveXMLElement = pDataNode->
ToElement();
00091
else
00092 pSaveXMLElement =
new TiXmlElement(
"TextureDatabase");
00093
00094
CTexture *pTexture = (
CTexture *)
begin();
00095
set_ptr(pTexture);
00096
00097
while(pTexture !=NULL)
00098 {
00099 TiXmlElement TextureXMLElement(
"Texture");
00100
if(pTexture->
SaveXML(&TextureXMLElement,
""))
00101 pSaveXMLElement->
InsertEndChild(TextureXMLElement);
00102
00103 pTexture = (
CTexture *)
next();
00104 }
00105
00106
if(pDataNode == NULL && strFilename !=
"")
00107 {
00108
TiXmlDocument SaveDoc;
00109 SaveDoc.
InsertEndChild(*pSaveXMLElement);
00110
int iRetValue = SaveDoc.
SaveFile(strFilename);
00111 SAFEDEL(pSaveXMLElement)
00112
00113
return iRetValue;
00114 }
00115
00116
return 1;
00117 }
00118
00119 CTexture *
CTextureManager::LoadResource(
TiXmlNode *pDataNode, CString strFilename, UINT uiID, CString strName)
00120 {
00121
if(pDataNode != NULL)
00122 {
00123
const char *pcValue = NULL;
00124
TiXmlElement *pXMLElement = pDataNode->
ToElement();
00125
00126 pcValue = pXMLElement->
Attribute(
"TexByID");
00127
if(pcValue !=NULL)
00128 {
00129
CTexture *ExistingTexture =
GetResource(-1, atoi(pcValue),
"");
00130
if(ExistingTexture !=NULL)
00131
return ExistingTexture;
00132
else
00133
return NULL;
00134 }
00135
00136 pcValue = pXMLElement->
Attribute(
"TexByName");
00137
if(pcValue !=NULL)
00138 {
00139
CTexture *ExistingTexture =
GetResource(-1, -1, pcValue);
00140
if(ExistingTexture !=NULL)
00141
return ExistingTexture;
00142
else
00143
return NULL;
00144 }
00145 }
00146
00147
CTexture *NewTexture = NULL;
00148
00149
#ifdef OPENGL
00150
NewTexture =
new COpenGLTexture;
00151
#endif
00152
00153
if(NewTexture->
LoadXML(pDataNode, strFilename))
00154 {
00155
if(NewTexture->
GetID() == 0)
00156 NewTexture->
SetID(uiID);
00157
00158
if(NewTexture->
GetName() ==
"")
00159 NewTexture->
SetName(strName);
00160
00161
if(NewTexture->
GetName() !=
"")
00162 {
00163
if(
GetResource(-1, -1, NewTexture->
GetName()) == NULL)
00164 {
00165
push_back(NewTexture);
00166
return NewTexture;
00167 }
00168
else
00169 {
00170 SAFEDEL(NewTexture)
00171
return NULL;
00172 }
00173 }
00174
else
00175 {
00176
push_back(NewTexture);
00177
return NewTexture;
00178 }
00179 }
00180
else
00181 SAFEDEL(NewTexture)
00182
00183
00184
return NULL;
00185 }
00186
00187 CTexture *
CTextureManager::GetResource(
int uiIndex,
int uiID, CString strName, CString strFilename)
00188 {
00189
if(uiID !=-1 && uiIndex == -1 && strName ==
"" && strFilename ==
"")
00190 {
00191
CXMLResource *ResourcePtr =
begin();
00192
set_ptr(ResourcePtr);
00193
00194
while(ResourcePtr !=NULL)
00195 {
00196
if(ResourcePtr->
GetID() == uiID)
00197
return (
CTexture *)ResourcePtr;
00198
00199 ResourcePtr =
next();
00200 }
00201 }
00202
else if(strName !=
"" && uiIndex == -1 && uiID == -1 && strFilename ==
"")
00203 {
00204
CXMLResource *ResourcePtr =
begin();
00205
set_ptr(ResourcePtr);
00206
00207
while(ResourcePtr !=NULL)
00208 {
00209
if(stricmp(ResourcePtr->
GetName().GetBuffer(), strName.GetBuffer()) == 0)
00210
return (
CTexture *)ResourcePtr;
00211
00212 ResourcePtr =
next();
00213 }
00214 }
00215
else if(uiIndex !=-1 && uiID == -1 && strName ==
"" && strFilename ==
"")
00216 {
00217
if(uiIndex >=0 && uiIndex <
size())
00218
return (
CTexture *)
at(uiIndex);
00219 }
00220
else if(strFilename !=
"" && uiIndex ==-1 && uiID == -1 && strName ==
"")
00221 {
00222
CXMLResource *ResourcePtr =
begin();
00223
set_ptr(ResourcePtr);
00224
00225
while(ResourcePtr !=NULL)
00226 {
00227
if(stricmp(((
CTexture *)ResourcePtr)->GetTextureFilename().GetBuffer(), strFilename.GetBuffer()) == 0)
00228
return (
CTexture *)ResourcePtr;
00229
00230 ResourcePtr =
next();
00231 }
00232 }
00233
00234
return NULL;
00235 }
00236
00237 int CTextureManager::RemoveResource(
CTexture *pTexture)
00238 {
00239
00240
return 0;
00241 }
00242
00243 void CTextureManager::Destroy()
00244 {
00245
CResourceManager::Destroy();
00246 }
00247
00248 bool CTextureManager::IsOfType(eEntityType eType)
00249 {
00250
if(eType == Entity_TextureManager)
00251
return true;
00252
00253
return CResourceManager::IsOfType(eType);
00254 }