-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTurbineAnimation.cpp
189 lines (145 loc) · 4.07 KB
/
TurbineAnimation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "StdAfx.h"
#include "TurbineAnimation.h"
TurbineAnimationFrame::TurbineAnimationFrame()
{
m_pTranslations = NULL;
m_pEvents = NULL;
}
TurbineAnimationFrame::~TurbineAnimationFrame()
{
SafeDeleteArray(m_pTranslations);
SafeDeleteArray(m_pEvents);
}
BYTE* TurbineAnimationFrame::InitializeTranslations(BYTE *pbFrame, int iPartCount)
{
m_pTranslations = new PartOrientation[iPartCount];
for (int i = 0; i < iPartCount; i++)
pbFrame = m_pTranslations[i].ReadData(pbFrame);
return pbFrame;
}
BYTE* TurbineAnimationFrame::InitializeEvents(BYTE *pbFrame)
{
int iEventCount = *((DWORD *)pbFrame);
pbFrame += 4;
if (iEventCount)
{
m_pEvents = new PartEvent[iEventCount];
for (int i = 0; i < iEventCount; i++)
pbFrame = m_pEvents[i].ReadData(pbFrame);
}
return pbFrame;
}
void TurbineAnimationFrame::ExecuteFrame(CPhysicsObj* pWeenie, AnimationPackage* pAnimation)
{
}
TurbineAnimation::TurbineAnimation(DWORD dwID) : TurbineObject(dwID)
{
m_iType = 0;
m_iPartCount = 0;
m_iFrameCount = 0;
}
TurbineAnimation::~TurbineAnimation()
{
SafeDeleteArray(m_pFrames);
}
BYTE* TurbineAnimation::InitializeFrames(BYTE *pbFrames, unsigned int iType, unsigned int iPartCount, unsigned int iFrameCount)
{
m_iType = iType;
m_iPartCount = iPartCount;
m_iFrameCount = iFrameCount;
m_pFrames = new ANIMATIONFRAME[m_iFrameCount];
if (iType == 1 || iType == 3)
pbFrames += sizeof(PartOrientation) * iPartCount;
for (unsigned int i = 0; i < iFrameCount; i++)
{
pbFrames = m_pFrames[i].InitializeTranslations(pbFrames, iPartCount);
pbFrames = m_pFrames[i].InitializeEvents(pbFrames);
}
return pbFrames;
}
void TurbineAnimation::Initialize(BYTE *pbData, DWORD dwLength)
{
if (!pbData)
return;
DWORD *header = (DWORD *)pbData;
// Animation Header (4 dwords, ie: 16 bytes)
DWORD dwFileID = header[0];
int iType = header[1];
int iPartCount = header[2];
int iFrameCount = header[3];
BYTE* frames = pbData + 16;
InitializeFrames(frames, iType, iPartCount, iFrameCount);
}
unsigned int TurbineAnimation::GetFrameCount()
{
return m_iFrameCount;
}
ANIMATIONFRAME* TurbineAnimation::GetFrame(int iFrame)
{
// Returns a frame by index from the array.
return &m_pFrames[iFrame];
}
long TurbineAnimation::GetFrameByTime(float fTime, float fSpeed)
{
if (!fSpeed)
fSpeed = 30.0f;
return (long)(fTime * fSpeed);
}
bool TurbineAnimation::Execute(CPhysicsObj* pWeenie, AnimationPackage* pAnimation)
{
// The is the workhorse for animations.
if (!pAnimation)
return false;
if (!GetFrameCount())
return false;
// Calculate the number of frames intended to be executed.
DWORD dwFrameCount =
min(GetFrameCount() - 1, pAnimation->m_dwEndFrame) -
min(GetFrameCount() - 1, pAnimation->m_dwStartFrame);
//The offset from the starting frame.
float fTime = (float)(g_pGlobals->Time() - pAnimation->m_fStartTime);
long lFrame = GetFrameByTime(fTime, pAnimation->m_fSpeed);
//The offset extends beyond the desired frames.
if (unsigned(abs(lFrame)) >= dwFrameCount)
return false;
DWORD dwCurrentFrame = pAnimation->GetBaseFrame() + lFrame;
if (dwCurrentFrame != pAnimation->m_dwCurrentFrame)
{
//Time to launch some events!
if (lFrame > 0) {
while (1)
{
//Execute all frames until we are up to date.
if (0x80000000 == pAnimation->m_dwCurrentFrame) //Null frame.
{
pAnimation->m_dwCurrentFrame = pAnimation->GetBaseFrame();
GetFrame(pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
}
else if (dwCurrentFrame > pAnimation->m_dwCurrentFrame)
{
GetFrame(++pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
}
else
break;
}
}
else if (lFrame < 0)
{
while (1)
{
if (0x80000000 == pAnimation->m_dwCurrentFrame) //Null frame.
{
pAnimation->m_dwCurrentFrame = pAnimation->GetBaseFrame();
GetFrame(pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
}
else if (dwCurrentFrame < pAnimation->m_dwCurrentFrame)
{
GetFrame(--pAnimation->m_dwCurrentFrame)->ExecuteFrame(pWeenie, pAnimation);
}
else
break;
}
}
}
return true;
}