00001
#ifndef FRAMEANIMATION_H
00002
#define FRAMEANIMATION_H
00003
00004
#pragma once
00005
#include "Animation.h"
00006
#include "Timer.h"
00007
00009
00012 template<
class TAnimation>
class CFrameAnimation :
public CAnimation
00013 {
00014
00015
public:
00016
CFrameAnimation()
00017 {
00018
SetEntityType(Entity_FrameAnimation);
00019
00020
m_uipDataIndex = NULL;
00021
m_pAnimationData = NULL;
00022
m_uiFrameCount =
00023
m_uiLoopsCount =
00024
m_uiCurrentLoop =
00025
m_uiCurrentFrame =
00026
m_uiDataCount = 0;
00027
m_bLoop =
false;
00028
m_bAnimationComplete =
false;
00029
m_bPaused =
false;
00030
m_fLastTime = 0;
00031 };
00032
00033
virtual ~
CFrameAnimation()
00034 {
00035
Destroy();
00036 };
00043 virtual int Create(UINT uiFrameCount, UINT uiDataCount)
00044 {
00045
if(!(uiFrameCount > 0 && uiDataCount > 0))
00046
return 0;
00047
00048 m_pAnimationData =
new TAnimation*[uiFrameCount];
00049 m_uipDataIndex =
new UINT[uiDataCount];
00050
00051
for (UINT i=0; i<uiFrameCount; i++)
00052 m_pAnimationData[i] = NULL;
00053
00054
for (UINT j=0; j<uiDataCount; j++)
00055 m_uipDataIndex[j] = 0;
00056
00057 m_uiDataCount = uiDataCount;
00058 m_uiFrameCount = uiFrameCount;
00059
00060
return 1;
00061 };
00062
00063
public:
00069 TAnimation *
GetFrame(UINT uiFrameIndex)
00070 {
00071
if(uiFrameIndex >=0 && uiFrameIndex < m_uiDataCount)
00072
return m_pAnimationData[uiFrameIndex];
00073
00074
return NULL;
00075 };
00080 void SetLooping(
bool bLooping)
00081 {
00082 m_bLoop = bLooping;
00083 };
00088 bool GetLooping()
00089 {
00090
return m_bLoop;
00091 };
00096 void SetLoopCount(UINT uiLoopCount)
00097 {
00098 m_uiLoopsCount = uiLoopCount;
00099 };
00104 UINT
GetFrameCount()
00105 {
00106
return m_uiFrameCount;
00107 };
00112 UINT
GetLoopCount()
00113 {
00114
return m_uiLoopsCount;
00115 };
00120 UINT
GetLoop()
00121 {
00122
return m_uiCurrentLoop;
00123 };
00128 TAnimation *
GetCurrentFrame()
00129 {
00130
return GetFrame(m_uiCurrentFrame);
00131 };
00136 UINT
GetFrameIndex()
00137 {
00138
return m_uiCurrentFrame;
00139 };
00144 virtual void SetFrameTime(
double fTime)
00145 {
00146
if(fTime > 0)
00147 m_fFrameTime = fTime;
00148 };
00154 virtual int Animate(
double fTimePerFrame = -1)
00155 {
00156
if(m_bPaused ==
true)
00157
return 0;
00158
00159
if(fTimePerFrame == -1 &&
CGlobalTimer::GetSingletonPtr() == NULL)
00160
return 0;
00161
else if(fTimePerFrame !=-1)
00162 m_fFrameTime = fTimePerFrame;
00163
00164
double cur_time =
CGlobalTimer::GetSingleton().
GetAbsoluteTime();
00165
if(m_uiCurrentFrame != (m_uiFrameCount - 1))
00166 {
00167
if((cur_time - m_fLastTime) >= m_fFrameTime)
00168 {
00169 m_uiCurrentFrame++;
00170 m_fLastTime = cur_time;
00171 }
00172 }
00173
else
00174 {
00175
if(m_bLoop)
00176 {
00177
if(m_uiCurrentLoop != (m_uiLoopsCount - 1))
00178 {
00179 m_uiCurrentLoop++;
00180 m_bAnimationComplete =
false;
00181 }
00182
else
00183 m_bAnimationComplete =
true;
00184 }
00185
00186 m_bAnimationComplete =
true;
00187 }
00188
00189
return (
int)m_bAnimationComplete;
00190 };
00196 virtual int Pause(
double fPauseTime = INFINITE)
00197 {
00198
00199
return 1;
00200 };
00206 virtual int Resume(
double fTimeToWait = 0)
00207 {
00208
00209
return 1;
00210 };
00215 virtual int Stop()
00216 {
00217
00218
return 1;
00219 };
00220
00221
public:
00227 virtual int LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00228 {
00229
return CAnimation::LoadXML(pDataNode, strFilename);
00230 };
00236 virtual int SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00237 {
00238
return CAnimation::SaveXML(pDataNode, strFilename);
00239 };
00240
00241 virtual void Destroy()
00242 {
00243 SAFEDEL_ARRAY(m_pAnimationData)
00244 SAFEDEL_ARRAY(m_uipDataIndex)
00245
00246
CAnimation::Destroy();
00247 };
00248
00249 virtual bool IsOfType(eEntityType eType)
00250 {
00251
if(eType == Entity_FrameAnimation)
00252
return true;
00253
00254
return CAnimation::IsOfType(eType);
00255 };
00256
00257
protected:
00258
00259 UINT *m_uipDataIndex;
00260 UINT m_uiFrameCount;
00261 UINT m_uiLoopsCount;
00262 UINT m_uiCurrentLoop;
00263 UINT m_uiCurrentFrame;
00264 double m_fLastTime;
00265
double m_fFrameTime;
00266 bool m_bLoop;
00267 TAnimation **m_pAnimationData;
00268 UINT m_uiDataCount;
00269 bool m_bAnimationComplete;
00270 bool m_bPaused;
00272 CTimer m_Timer;
00273 };
00274
00275
#endif