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