-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.cpp
More file actions
342 lines (300 loc) · 8.51 KB
/
animation.cpp
File metadata and controls
342 lines (300 loc) · 8.51 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <assert.h>
#include <SDL/SDL.h>
#include "sprite.h"
namespace dragonfighting {
/*
* Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!
*/
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
/*
* Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!
*/
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
AnimationSequence::AnimationSequence(const char *name) :
name(name),
animRateFrame(0),
curIndex(0),
ended(false),
playStyle(ONCE),
indexArray(NULL),
size(0)
{
}
AnimationSequence::~AnimationSequence()
{
if (indexArray != NULL) {
free(indexArray);
}
}
bool AnimationSequence::nameCompare(const char *name)
{
return this->name.compare(name) == 0;
}
void AnimationSequence::setFrameRate(Uint32 frame)
{
this->animRateFrame = frame;
}
Uint32 AnimationSequence::getFrameRate()
{
return this->animRateFrame;
}
void AnimationSequence::setPlayStyle(enum AnimationStyle style)
{
this->playStyle = style;
}
AnimationSequence::AnimationStyle AnimationSequence::getPlayStyle()
{
return this->playStyle;
}
void AnimationSequence::setIndexArray(int indexarray[], int length)
{
indexArray = (int *)malloc(sizeof(int) * length);
assert(indexArray != NULL);
memcpy(this->indexArray, indexarray, sizeof(int) * length);
size = length;
}
int AnimationSequence::getCurrentFrameIndex()
{
return indexArray[curIndex];
}
int AnimationSequence::getNextFrameIndex()
{
curIndex ++;
if (curIndex >= size) {
switch (playStyle) {
case ONCE:
curIndex = size - 1;
ended = true;
break;
case LOOP:
curIndex = 0;
break;
default:
break;
}
}
return indexArray[curIndex];
}
int AnimationSequence::getPrevFrameIndex()
{
curIndex --;
if (curIndex < 0) {
switch (playStyle) {
case ONCE:
curIndex = 0;
ended = true;
break;
case LOOP:
curIndex = size - 1;
break;
default:
break;
}
}
return indexArray[curIndex];
}
bool AnimationSequence::isEnd()
{
return ended;
}
void AnimationSequence::reset()
{
curIndex = 0;
ended = false;
}
Animation::Animation() :
fullImage(NULL),
flipedFullImage(NULL),
sequences(),
currentFrame(0),
currentSequence(-1),
defaultSequence(-1),
oldFrameStamp(0),
flipHorizontal(false)
{
}
Animation::~Animation()
{
for (vector<AnimationSequence*>::iterator i = sequences.begin(); i != sequences.end(); ++i ){
delete *i;
}
// free fliped image
if (flipedFullImage != NULL) {
SDL_FreeSurface(flipedFullImage);
}
}
void Animation::setFullImage(SDL_Surface *img)
{
this->fullImage = img;
if (flipedFullImage != NULL) {
SDL_FreeSurface(flipedFullImage);
}
this->flipedFullImage = SDL_CreateRGBSurface(img->flags, img->w, img->h, img->format->BitsPerPixel,
img->format->Rmask, img->format->Gmask, img->format->Bmask, img->format->Amask);
SDL_LockSurface(img);
SDL_LockSurface(flipedFullImage);
for(int y = 0; y < img->h; y++) {
for(int x = 0; x < img->w; x++) {
putpixel(flipedFullImage, img->w - 1 - x, y, getpixel(img, x, y));
}
}
SDL_UnlockSurface(img);
SDL_UnlockSurface(flipedFullImage);
flipedFullImage->flags = img->flags;
flipedFullImage->format->colorkey = img->format->colorkey;
}
SDL_Surface *Animation::getFullImage()
{
return fullImage;
}
void Animation::addFrame(SDL_Rect rect, SDL_Rect anchorpoint)
{
frameRects.push_back(rect);
frameAnchorPoints.push_back(anchorpoint);
}
void Animation::addSequence(const char *name, Uint32 framerate, AnimationSequence::AnimationStyle style, int indexarray[], int length)
{
AnimationSequence *seq = new AnimationSequence(name);
seq->setFrameRate(framerate);
seq->setPlayStyle(style);
for (int i=0; i<length; i++) {
assert(indexarray[i] < (int)frameRects.size());
}
seq->setIndexArray(indexarray, length);
this->sequences.push_back(seq);
}
void Animation::setFlipHorizontal(bool b)
{
flipHorizontal = b;
}
void Animation::setDefaultSequence(const char *name)
{
int index = 0;
for (vector<AnimationSequence*>::iterator i = sequences.begin(); i != sequences.end(); ++i ){
if ( (*i)->nameCompare(name) ) {
defaultSequence = index;
break;
}
index ++;
}
}
SDL_Rect Animation::getCurAnchor()
{
return frameAnchorPoints[currentFrame];
}
void Animation::playSequence(const char *name)
{
int index = 0;
for (vector<AnimationSequence*>::iterator i = sequences.begin(); i != sequences.end(); ++i ){
if ( (*i)->nameCompare(name) ) {
currentSequence = index;
sequences[currentSequence]->reset();
currentFrame = sequences[currentSequence]->getCurrentFrameIndex();
oldFrameStamp = 0;
break;
}
index ++;
}
backorder = false;
}
void Animation::playSequenceBackorder(const char *name)
{
int index = 0;
for (vector<AnimationSequence*>::iterator i = sequences.begin(); i != sequences.end(); ++i ){
if ( (*i)->nameCompare(name) ) {
currentSequence = index;
sequences[currentSequence]->reset();
currentFrame = sequences[currentSequence]->getPrevFrameIndex();
break;
}
index ++;
}
backorder = true;
}
void Animation::update(Uint32 frameStamp)
{
assert(currentSequence >= 0);
if (oldFrameStamp == 0) {
// is the first frame
oldFrameStamp = frameStamp;
} else if (oldFrameStamp + sequences[currentSequence]->getFrameRate() <= frameStamp) {
oldFrameStamp = frameStamp;
if (backorder) {
currentFrame = sequences[currentSequence]->getPrevFrameIndex();
} else {
currentFrame = sequences[currentSequence]->getNextFrameIndex();
}
if (sequences[currentSequence]->isEnd()) {
currentSequence = defaultSequence;
sequences[currentSequence]->reset();
currentFrame = sequences[currentSequence]->getCurrentFrameIndex();
}
}
}
void Animation::draw(SDL_Surface *dst)
{
SDL_Rect screenposition = getPositionScreenCoor();
if (flipHorizontal) {
SDL_Rect srcrect = {(Sint16)(flipedFullImage->w - frameRects[currentFrame].x - frameRects[currentFrame].w),
frameRects[currentFrame].y,
frameRects[currentFrame].w,
frameRects[currentFrame].h};
SDL_Rect dstrect = {(Sint16)(screenposition.x - frameRects[currentFrame].w + frameAnchorPoints[currentFrame].x),
(Sint16)(screenposition.y - frameAnchorPoints[currentFrame].y), 0, 0};
SDL_BlitSurface(flipedFullImage, &srcrect, dst, &dstrect);
} else {
SDL_Rect dstrect = {(Sint16)(screenposition.x - frameAnchorPoints[currentFrame].x),
(Sint16)(screenposition.y - frameAnchorPoints[currentFrame].y), 0, 0};
SDL_BlitSurface(fullImage, &frameRects[currentFrame], dst, &dstrect);
}
}
}