-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.cpp
185 lines (144 loc) · 4.1 KB
/
Animation.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
181
182
183
184
185
////////////////////////////////////////////////////////
// File Name : "CAnimation.cpp"
//
// Author : Matthew Di Matteo (MD)
//
// Purpose : This file provides a way to set and control
// the animations of the game objects
////////////////////////////////////////////////////////
#include "Animation.h"
#include<fstream>
#include <exception>
using namespace std;
#include "CSGD_TextureManager.h"
CAnimation::CAnimation(void)
{
m_nTotalFrames = 0;
m_nCurrFrame = 0;
m_fTimeWaited = 0;
m_bIsPlaying = false;
m_bIsLooping = false;
m_fSpeed = 1;
m_nImageID = -1;
}
CAnimation::~CAnimation(void)
{
}
void CAnimation::Play()
{
Reset();
m_bIsPlaying = true;
}
void CAnimation::Stop()
{
m_bIsPlaying = false;
}
void CAnimation::Resume()
{
m_bIsPlaying = true;
}
void CAnimation::Reset()
{
}
void CAnimation::Load(const char* FileName)
{
int nDuration;
char bIsLooping[128] = "true ";
char filebuff[128];
ifstream ifs;
ifs.exceptions(~ios_base::goodbit);
try
{
ifs.open(FileName, ios_base::in| ios_base::binary);
if (ifs.is_open())
{
unsigned char size;
ifs.read(reinterpret_cast<char *>(&m_nTotalFrames),4);
ifs.read(reinterpret_cast<char *>(&size),1);
ZeroMemory(filebuff,128);
ifs.read(filebuff,size);
m_nAnimName = filebuff; //2
ifs.read(reinterpret_cast<char *>(&size),1);
ZeroMemory(filebuff,128);
ifs.read(filebuff,size);
char temp[128];
sprintf_s(temp, _countof(temp),"resource/images/%s", filebuff);
m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(temp ,D3DCOLOR_XRGB(255,255,255));
ifs.read(reinterpret_cast<char *>(&size),1);
ZeroMemory(filebuff,128);
ifs.read(filebuff,size);
m_nTriggerName = filebuff; // flying
ifs.read(reinterpret_cast<char *>(&size),1);
ZeroMemory(filebuff,128);
ifs.read(filebuff,size);
m_nTriggerType = filebuff; //particle
ifs.read(reinterpret_cast<char *>(&size),1);
ZeroMemory(filebuff,128);
ifs.read(filebuff,size);
if(strcmp(filebuff,bIsLooping))
m_bIsLooping = true;
else
m_bIsLooping = false;
ifs.read(reinterpret_cast<char *>(&nDuration),4); //0.35
m_fDuration = nDuration /1000.0f;
frames = new Frame[m_nTotalFrames];
for(int i = 0; i < m_nTotalFrames; i++)
{
ifs.read(reinterpret_cast<char *>(&(frames[i].nFrameX)),4);
ifs.read(reinterpret_cast<char *>(&frames[i].nFrameY),4);
ifs.read(reinterpret_cast<char *>(&frames[i].nFrameWidth),4);
ifs.read(reinterpret_cast<char *>(&frames[i].nFrameHeight),4);
ifs.read(reinterpret_cast<char *>(&frames[i].nAnchorX),4);
ifs.read(reinterpret_cast<char *>(&frames[i].nAnchorY),4);
}
}
}
catch (ios_base::failure &)
{
if (!ifs.eof())
throw;
}
ifs.close();
}
void CAnimation::Update(float fElapsedtime)
{
if(!m_bIsPlaying)
return;
m_fTimeWaited += fElapsedtime * m_fSpeed;
if(m_fTimeWaited > m_fDuration)
{
m_nCurrFrame++;
//m_fTimeWaited = 0;
m_fTimeWaited -= m_fDuration;
if(m_nCurrFrame >= m_nTotalFrames)
{
if(m_bIsLooping)
m_nCurrFrame = 0;
else
{
Stop();
m_nCurrFrame = m_nTotalFrames-1;
}
}
}
}
void CAnimation::Render(int posx, int posy, float scale, bool bFacingLeft, DWORD dwColor)
{
if(m_nImageID !=-1)
{
RECT frame = {frames[m_nCurrFrame].nFrameX, frames[m_nCurrFrame].nFrameY,
frames[m_nCurrFrame].nFrameX + frames[m_nCurrFrame].nFrameWidth, frames[m_nCurrFrame].nFrameY+frames[m_nCurrFrame].nFrameHeight};
float fScaleX = scale;
if(bFacingLeft) // flip
{
//manipulate sclae and pos
fScaleX *=-1;
posx += int(frames[m_nCurrFrame].nFrameWidth*scale);
}
//draw stuff to screen
/*CSGD_TextureManager::GetInstance()->Draw(m_nImageID,posx,posy,fScaleX,scale,&frame, 0, 0, 0);*/
CSGD_TextureManager::GetInstance()->Draw(m_nImageID,posx-(frames[m_nCurrFrame].nAnchorX-frames[m_nCurrFrame].nFrameX),
posy-(frames[m_nCurrFrame].nAnchorY-frames[m_nCurrFrame].nFrameY),
fScaleX,scale,&frame, 0, 0, 0);
}
}