00001
#include "StdAfx.h"
00002
#include "MaterialManager.h"
00003
00004 CMaterialManager::CMaterialManager()
00005 {
00006
SetEntityType(Entity_MaterialManager);
00007
CGlobalLogger::GetSingleton().
Write(
"Material Manager - Instantiated\n");
00008 }
00009
00010 CMaterialManager::~CMaterialManager()
00011 {
00012
Destroy();
00013
CGlobalLogger::GetSingleton().
Write(
"Material Manager - Destroyed\n");
00014 }
00015
00016 CMaterial *
CMaterialManager::AddResource(CString strName, UINT uiID, eEntityType ResourceType)
00017 {
00018
switch(ResourceType)
00019 {
00020
case Entity_Default:
00021
case Entity_Material:
00022
case Entity_OpenGLMaterial:
00023
CMaterial *pNewMaterial =
new COpenGLMaterial;
00024
00025 pNewMaterial->
SetID(uiID);
00026 pNewMaterial->
SetName(strName);
00027
00028
push_back(pNewMaterial);
00029
00030
return pNewMaterial;
00031
break;
00032 }
00033
00034
return NULL;
00035 }
00036
00037 int CMaterialManager::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(),
"material") == 0)
00049 {
00050
CXMLResource *AddedResource =
LoadResource(pDataNode, strFilename);
00051
if(AddedResource !=NULL)
00052 {
00053
CGlobalLogger::GetSingleton().
Write(
"Material (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 MaterialLoadedCount = 0;
00061
TiXmlNode *pIterXMLNode = pXMLElement->
FirstChild();
00062
while(pIterXMLNode !=NULL)
00063 {
00064
if(stricmp(pIterXMLNode->
pcValue(),
"material") == 0)
00065 {
00066
CXMLResource *AddedResource =
LoadResource(pIterXMLNode,
"");
00067
if(AddedResource !=NULL)
00068 {
00069
CGlobalLogger::GetSingleton().
Write(
"Material (Name: %s, ID: %d) has been added successfully.\n", AddedResource->
GetName().GetBuffer(), AddedResource->
GetID());
00070 MaterialLoadedCount++;
00071 }
00072 }
00073
00074 pIterXMLNode = pXMLElement->
IterateChildren(pIterXMLNode);
00075 }
00076
00077
return MaterialLoadedCount;
00078 }
00079
00080 int CMaterialManager::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(
"MaterialDatabase");
00093
00094
CMaterial *pMaterial = (
CMaterial *)
begin();
00095
set_ptr(pMaterial);
00096
00097
while(pMaterial !=NULL)
00098 {
00099 TiXmlElement MaterialXMLElement(
"Material");
00100
if(pMaterial->
SaveXML(&MaterialXMLElement,
""))
00101 pSaveXMLElement->
InsertEndChild(MaterialXMLElement);
00102
00103 pMaterial = (
CMaterial *)
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 CMaterial *
CMaterialManager::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(
"MatByID");
00127
if(pcValue !=NULL)
00128 {
00129
CMaterial *ExistingMaterial =
GetResource(-1, atoi(pcValue),
"");
00130
if(ExistingMaterial !=NULL)
00131
return ExistingMaterial;
00132
else
00133
return NULL;
00134 }
00135
00136 pcValue = pXMLElement->
Attribute(
"MatByName");
00137
if(pcValue !=NULL)
00138 {
00139
CMaterial *ExistingMaterial =
GetResource(-1, -1, pcValue);
00140
if(ExistingMaterial !=NULL)
00141
return ExistingMaterial;
00142
else
00143
return NULL;
00144 }
00145 }
00146
00147
if(stricmp(pDataNode->
pcValue(),
"material") == 0)
00148 {
00149
CMaterial *NewMaterial = NULL;
00150
00151
#ifdef OPENGL
00152
NewMaterial =
new COpenGLMaterial;
00153
#endif
00154
00155
if(NewMaterial->
LoadXML(pDataNode, strFilename))
00156 {
00157
if(NewMaterial->
GetID() == 0)
00158 NewMaterial->
SetID(uiID);
00159
00160
if(NewMaterial->
GetName() ==
"")
00161 NewMaterial->
SetName(strName);
00162
00163
if(NewMaterial->
GetName() !=
"")
00164 {
00165
if(
GetResource(-1, -1, NewMaterial->
GetName()) == NULL)
00166 {
00167
push_back(NewMaterial);
00168
return NewMaterial;
00169 }
00170
else
00171 {
00172 SAFEDEL(NewMaterial)
00173
return NULL;
00174 }
00175 }
00176
else
00177 {
00178
push_back(NewMaterial);
00179
return NewMaterial;
00180 }
00181 }
00182
else
00183 SAFEDEL(NewMaterial)
00184 }
00185
00186
return NULL;
00187 }
00188
00189 CMaterial *
CMaterialManager::GetResource(
int uiIndex,
int uiID, CString strName, CString strFilename)
00190 {
00191
if(uiID !=-1 && uiIndex == -1 && strName ==
"" && strFilename ==
"")
00192 {
00193
CXMLResource *ResourcePtr =
begin();
00194
set_ptr(ResourcePtr);
00195
00196
while(ResourcePtr !=NULL)
00197 {
00198
if(ResourcePtr->
GetID() == uiID)
00199
return (
CMaterial *)ResourcePtr;
00200
00201 ResourcePtr =
next();
00202 }
00203 }
00204
else if(strName !=
"" && uiIndex == -1 && uiID == -1 && strFilename ==
"")
00205 {
00206
CXMLResource *ResourcePtr =
begin();
00207
set_ptr(ResourcePtr);
00208
00209
while(ResourcePtr !=NULL)
00210 {
00211
if(stricmp(ResourcePtr->
GetName().GetBuffer(), strName.GetBuffer()) == 0)
00212
return (
CMaterial *)ResourcePtr;
00213
00214 ResourcePtr =
next();
00215 }
00216 }
00217
else if(uiIndex !=-1 && uiID == -1 && strName ==
"" && strFilename ==
"")
00218 {
00219
if(uiIndex >=0 && uiIndex <
size())
00220
return (
CMaterial *)
at(uiIndex);
00221 }
00222
else if(strFilename !=
"" && uiIndex ==-1 && uiID == -1 && strName ==
"")
00223 {
00224
CXMLResource *ResourcePtr =
begin();
00225
set_ptr(ResourcePtr);
00226
00227
while(ResourcePtr !=NULL)
00228 {
00229
if(stricmp(ResourcePtr->
GetFilename().GetBuffer(), strFilename.GetBuffer()) == 0)
00230
return (
CMaterial *)ResourcePtr;
00231
00232 ResourcePtr =
next();
00233 }
00234 }
00235
00236
return NULL;
00237 }
00238
00239 int CMaterialManager::RemoveResource(
CMaterial *pMaterial)
00240 {
00241
00242
return 0;
00243 }
00244
00245 void CMaterialManager::Destroy()
00246 {
00247
CResourceManager::Destroy();
00248 }
00249
00250 bool CMaterialManager::IsOfType(eEntityType eType)
00251 {
00252
if(eType == Entity_MaterialManager)
00253
return true;
00254
00255
return CResourceManager::IsOfType(eType);
00256 }