00001
#include "stdafx.h"
00002
#include "TextureFrameAnimation.h"
00003
00004 CTextureFrameAnimation::CTextureFrameAnimation()
00005 {
00006
SetEntityType(Entity_TextureFrameAnimation);
00007
m_pTextures = NULL;
00008 }
00009
00010 CTextureFrameAnimation::~CTextureFrameAnimation()
00011 {
00012
Destroy();
00013
00014
for (UINT i=0; i<
m_uiDataCount; i++)
00015
m_pTextures[i] = NULL;
00016
00017 SAFEDEL_ARRAY(m_pTextures)
00018 }
00019
00020 int CTextureFrameAnimation::Create(UINT uiFrameCount, UINT uiDataCount)
00021 {
00022
if(!
CFrameAnimation<tRect>::Create(uiFrameCount, uiDataCount))
00023
return 0;
00024
00025
m_pTextures =
new CTexture*[uiDataCount];
00026
for (UINT i=0; i<uiDataCount; i++)
00027
m_pTextures[i] = NULL;
00028
00029
for (UINT j=0; j<uiFrameCount; j++)
00030 m_pAnimationData[j] =
new tRect(0, 0, 0, 0);
00031
00032
return 1;
00033 }
00034
00035 int CTextureFrameAnimation::Animate(
double fTimePerFrame)
00036 {
00037
00038
return 1;
00039 }
00040
00041 int CTextureFrameAnimation::Pause(
double fPauseTime)
00042 {
00043
00044
return 1;
00045 }
00046
00047 int CTextureFrameAnimation::Resume(
double fTimeToWait)
00048 {
00049
00050
return 1;
00051 }
00052
00053 int CTextureFrameAnimation::Stop()
00054 {
00055
00056
00057
return 1;
00058 }
00059
00060 void CTextureFrameAnimation::SetFrameRect(UINT uiFrameIndex, tRect& Rect)
00061 {
00062
if(uiFrameIndex >=0 && uiFrameIndex < m_uiFrameCount)
00063 *m_pAnimationData[uiFrameIndex] = Rect;
00064 }
00065
00066 int CTextureFrameAnimation::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00067 {
00068
TiXmlElement* pXMLElement = NULL;
00069
const char *pcValue = NULL;
00070
00071
int iRetValue =
CFrameAnimation<tRect>::LoadXML(pDataNode, strFilename);
00072
if(iRetValue !=1)
00073
return iRetValue;
00074
00075 pXMLElement = m_pXMLElement;
00076
00077 UINT uiFrameCount = 0;
00078
00079 pcValue = pXMLElement->
Attribute(
"Type");
00080
if(pcValue !=NULL)
00081 {
00082
if(stricmp(pcValue,
"Texture Frame") != 0)
00083
return 0;
00084
00085 pcValue = NULL;
00086 }
00087
00088 pcValue = pXMLElement->
Attribute(
"Looping");
00089
if(pcValue !=NULL)
00090 {
00091 m_bLoop = (
int)atoi(pcValue);
00092 pcValue = NULL;
00093 }
00094
00095 pcValue = pXMLElement->
Attribute(
"Loops");
00096
if(pcValue !=NULL)
00097 {
00098 m_uiLoopsCount = atoi(pcValue);
00099 pcValue = NULL;
00100 }
00101
00102
TiXmlNode *pIterXMLNode = pXMLElement->
FirstChild();
00103
while(pIterXMLNode !=NULL)
00104 {
00105
if(stricmp(pIterXMLNode->
pcValue(),
"frame") == 0)
00106 uiFrameCount++;
00107
00108 pIterXMLNode = pXMLElement->
IterateChildren(pIterXMLNode);
00109 }
00110
00111
Create(uiFrameCount, uiFrameCount);
00112
00113 UINT uiIndex = 0;
00114 pIterXMLNode = pXMLElement->
FirstChild();
00115
while(pIterXMLNode !=NULL)
00116 {
00117
if(stricmp(pIterXMLNode->
pcValue(),
"frame") == 0)
00118 {
00119 pcValue = pIterXMLNode->
ToElement()->
Attribute(
"Rect");
00120
if(pcValue !=NULL)
00121 {
00122 sscanf( pcValue,
"%d,%d,%d,%d",
00123 &m_pAnimationData[uiIndex]->left,
00124 &m_pAnimationData[uiIndex]->right,
00125 &m_pAnimationData[uiIndex]->top,
00126 &m_pAnimationData[uiIndex]->bottom);
00127
00128 pcValue = NULL;
00129 }
00130
00131
m_pTextures[uiIndex] =
CTextureManager::GetSingleton().
LoadResource(pIterXMLNode,
"");
00132
00133 uiIndex++;
00134 }
00135
00136 pIterXMLNode = pXMLElement->
IterateChildren(pIterXMLNode);
00137 }
00138
00139
return 1;
00140 }
00141
00142 int CTextureFrameAnimation::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00143 {
00144
TiXmlElement *pSaveElement = NULL;
00145
TiXmlDocument Doc;
00146
00147
if(pDataNode !=NULL)
00148 pSaveElement = pDataNode->
ToElement();
00149
else if(m_pXMLElement !=NULL)
00150 pSaveElement = m_pXMLElement;
00151
else
00152 pSaveElement =
new TiXmlElement(
"Animation");
00153
00154 pSaveElement->
SetAttribute(
"Type",
"Texture Frame");
00155 pSaveElement->
SetAttribute(
"strName",
GetName().GetBuffer());
00156 pSaveElement->
SetAttribute(
"Looping", (
int)m_bLoop);
00157 pSaveElement->
SetAttribute(
"Loops",
GetLoopCount());
00158
00159
for (UINT i=0; i<m_uiFrameCount; i++)
00160 {
00161 TiXmlElement FrameElement(
"Frame");
00162
SaveFrame(&FrameElement, i);
00163
00164 pSaveElement->
InsertEndChild(FrameElement);
00165 }
00166
00167
char pcValue[256] =
"";
00168
00169
if(pDataNode == NULL)
00170 {
00171
if(strFilename ==
"")
00172
return 0;
00173
00174 Doc.
InsertEndChild(*pSaveElement);
00175 SAFEDEL(pSaveElement)
00176
return Doc.
SaveFile(strFilename.GetBuffer());
00177 }
00178
else
00179 {
00180
if(strFilename ==
"")
00181
return 1;
00182
00183 Doc.
InsertEndChild(*pSaveElement);
00184
return Doc.
SaveFile(strFilename.GetBuffer());
00185 }
00186
00187
return 0;
00188 }
00189
00190 CTexture *
CTextureFrameAnimation::GetTexture()
00191 {
00192
return m_pTextures[
GetFrameIndex()];
00193 }
00194
00195 int CTextureFrameAnimation::SaveFrame(
TiXmlNode *pDataNode, UINT uiIndex)
00196 {
00197
if(pDataNode == NULL)
00198
return -1;
00199
00200
TiXmlElement *pSaveElement = pDataNode->
ToElement();
00201
00202
char pcValue[256] =
"";
00203 sprintf((
char *)pcValue,
"%d,%d,%d,%d", m_pAnimationData[uiIndex]->left, m_pAnimationData[uiIndex]->right, m_pAnimationData[uiIndex]->top, m_pAnimationData[uiIndex]->bottom);
00204
00205 pSaveElement->
SetAttribute(
"Rect", pcValue);
00206
00207
if(
m_pTextures[m_uipDataIndex[uiIndex]] !=NULL)
00208 {
00209
if(
m_pTextures[m_uipDataIndex[uiIndex]]->
GetName() !=
"")
00210 pSaveElement->
SetAttribute(
"TexByName",
m_pTextures[m_uipDataIndex[uiIndex]]->
GetName().GetBuffer());
00211 }
00212
return 1;
00213 }
00214
00215 void CTextureFrameAnimation::SetTexture(UINT uiFrame, UINT uiTexIndex,
CTexture *pTexture)
00216 {
00217
if(uiFrame >=0 && uiFrame < m_uiFrameCount)
00218 {
00219
if(uiTexIndex >=0 && uiTexIndex < m_uiDataCount)
00220 {
00221
if(pTexture !=NULL)
00222
m_pTextures[uiTexIndex] = pTexture;
00223
00224 m_uipDataIndex[uiFrame] = uiTexIndex;
00225 }
00226 }
00227 }
00228
00229 void CTextureFrameAnimation::Destroy()
00230 {
00231
if(m_pAnimationData !=NULL)
00232 {
00233
for (UINT i=0; i<m_uiFrameCount; i++)
00234 SAFEDEL(m_pAnimationData[i])
00235 }
00236
00237
CFrameAnimation<tRect>::Destroy();
00238 }
00239
00240 bool CTextureFrameAnimation::IsOfType(eEntityType eType)
00241 {
00242
if(eType == Entity_TextureFrameAnimation)
00243
return true;
00244
00245
return CFrameAnimation<tRect>::IsOfType(eType);
00246 }