-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSuperAnimCore.cpp
More file actions
807 lines (691 loc) · 24.6 KB
/
SuperAnimCore.cpp
File metadata and controls
807 lines (691 loc) · 24.6 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
//
// SuperAnimCore.cpp
//
// Created by Raymond Lu(Raymondlu1105@qq.com)
//
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <assert.h>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include "SuperAnimCommon.h"
#define SAM_VERSION 1
#define TWIPS_PER_PIXEL (20.0f)
#define LONG_TO_FLOAT (65536.0f)
#define FRAMEFLAGS_REMOVES 0x01
#define FRAMEFLAGS_ADDS 0x02
#define FRAMEFLAGS_MOVES 0x04
#define FRAMEFLAGS_FRAME_NAME 0x08
#define MOVEFLAGS_ROTATE 0x4000
#define MOVEFLAGS_COLOR 0x2000
#define MOVEFLAGS_MATRIX 0x1000
#define MOVEFLAGS_LONGCOORDS 0x0800
#ifndef max
#define max(x,y) (((x) < (y)) ? (y) : (x))
#endif
namespace SuperAnim {
//////////////////////////////////////////////////////////////////////////////////////////////////
SuperAnimMatrix3::SuperAnimMatrix3()
{
}
void SuperAnimMatrix3::LoadIdentity()
{
m01 = m02 = m10 = m12 = m20 = m21 = 0;
m00 = m11 = m22 = 1;
}
SuperAnimMatrix3 SuperAnimMatrix3::operator*(const SuperAnimMatrix3 &theMat) const
{
SuperAnimMatrix3 aResult;
aResult.m00 = m00*theMat.m00 + m01*theMat.m10 + m02*theMat.m20;
aResult.m01 = m00*theMat.m01 + m01*theMat.m11 + m02*theMat.m21;
aResult.m02 = m00*theMat.m02 + m01*theMat.m12 + m02*theMat.m22;
aResult.m10 = m10*theMat.m00 + m11*theMat.m10 + m12*theMat.m20;
aResult.m11 = m10*theMat.m01 + m11*theMat.m11 + m12*theMat.m21;
aResult.m12 = m10*theMat.m02 + m11*theMat.m12 + m12*theMat.m22;
aResult.m20 = m20*theMat.m00 + m21*theMat.m10 + m22*theMat.m20;
aResult.m21 = m20*theMat.m01 + m21*theMat.m11 + m22*theMat.m21;
aResult.m22 = m20*theMat.m02 + m21*theMat.m12 + m22*theMat.m22;
return aResult;
}
//////////////////////////////////////////////////////////////////////////
SuperAnimTransform::SuperAnimTransform()
{
mMatrix.LoadIdentity();
}
void SuperAnimTransform::Scale(float sx, float sy)
{
mMatrix.m00 *= sx;
mMatrix.m01 *= sx;
mMatrix.m02 *= sx;
mMatrix.m10 *= sy;
mMatrix.m11 *= sy;
mMatrix.m12 *= sy;
}
SuperAnimTransform SuperAnimTransform::TransformSrc(const SuperAnimTransform& theSrcTransform)
{
SuperAnimTransform aNewTransform;
aNewTransform.mMatrix.m00 = mMatrix.m00*theSrcTransform.mMatrix.m00 + mMatrix.m01*theSrcTransform.mMatrix.m10;
aNewTransform.mMatrix.m01 = mMatrix.m00*theSrcTransform.mMatrix.m01 + mMatrix.m01*theSrcTransform.mMatrix.m11;
aNewTransform.mMatrix.m10 = mMatrix.m10*theSrcTransform.mMatrix.m00 + mMatrix.m11*theSrcTransform.mMatrix.m10;
aNewTransform.mMatrix.m11 = mMatrix.m10*theSrcTransform.mMatrix.m01 + mMatrix.m11*theSrcTransform.mMatrix.m11;
aNewTransform.mMatrix.m02 = mMatrix.m02 + mMatrix.m00*theSrcTransform.mMatrix.m02 + mMatrix.m01*theSrcTransform.mMatrix.m12;
aNewTransform.mMatrix.m12 = mMatrix.m12 + mMatrix.m10*theSrcTransform.mMatrix.m02 + mMatrix.m11*theSrcTransform.mMatrix.m12;
return aNewTransform;
}
SuperAnimTransform SuperAnimTransform::InterpolateTo(const SuperAnimTransform& theNextTransform, float thePct)
{
SuperAnimTransform aNewTransform;
aNewTransform.mMatrix.m00 = (mMatrix.m00 * (1.0f - thePct)) + (theNextTransform.mMatrix.m00 * thePct);
aNewTransform.mMatrix.m01 = (mMatrix.m01 * (1.0f - thePct)) + (theNextTransform.mMatrix.m01 * thePct);
aNewTransform.mMatrix.m10 = (mMatrix.m10 * (1.0f - thePct)) + (theNextTransform.mMatrix.m10 * thePct);
aNewTransform.mMatrix.m11 = (mMatrix.m11 * (1.0f - thePct)) + (theNextTransform.mMatrix.m11 * thePct);
aNewTransform.mMatrix.m02 = (mMatrix.m02 * (1.0f - thePct)) + (theNextTransform.mMatrix.m02 * thePct);
aNewTransform.mMatrix.m12 = (mMatrix.m12 * (1.0f - thePct)) + (theNextTransform.mMatrix.m12 * thePct);
return aNewTransform;
}
//////////////////////////////////////////////////////////////////////////
Color::Color() :
mRed(0),
mGreen(0),
mBlue(0),
mAlpha(255)
{
}
Color::Color(int theRed, int theGreen, int theBlue, int theAlpha) :
mRed(theRed),
mGreen(theGreen),
mBlue(theBlue),
mAlpha(theAlpha)
{
}
Color Color::InterpolateTo(const Color& theNextColor, float thePct){
return Color((int)(mRed * (1.0f - thePct) + theNextColor.mRed * thePct),
(int)(mGreen * (1.0f - thePct) + theNextColor.mGreen * thePct),
(int)(mBlue * (1.0f - thePct) + theNextColor.mBlue * thePct),
(int)(mAlpha * (1.0f - thePct) + theNextColor.mAlpha * thePct));
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Animation object definition
class SuperAnimObject
{
public:
int mObjectNum;
int mResNum;
SuperAnimTransform mTransform;
Color mColor;
};
typedef std::vector<SuperAnimObject> SuperAnimObjectVector;
typedef std::map<int, SuperAnimObject> IntToSuperAnimObjectMap;
class SuperAnimImage
{
public:
SuperAnimSpriteId mSpriteId;
std::string mImageName;
int mWidth;
int mHeight;
SuperAnimTransform mTransform;
};
typedef std::vector<SuperAnimImage> SuperAnimImageVector;
class SuperAnimFrame
{
public:
SuperAnimObjectVector mObjectVector;
};
typedef std::vector<SuperAnimFrame> SuperAnimFrameVector;
typedef std::map<std::string, int> StringToIntMap;
class SuperAnimLabel{
public:
std::string mLabelName;
int mStartFrameNum;
int mEndFrameNum;
};
typedef std::vector<SuperAnimLabel> SuperAnimLabelArray;
class SuperAnimMainDef
{
public:
SuperAnimFrameVector mFrames;
int mStartFrameNum;
int mEndFrameNum;
int mAnimRate;
SuperAnimLabelArray mLabels;
int mX;
int mY;
int mWidth;
int mHeight;
SuperAnimImageVector mImageVector;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
typedef std::map<std::string, SuperAnimMainDef> SuperAnimMainDefMap;
class SuperAnimDefMgr
{
private:
SuperAnimMainDefMap mMainDefCache;
private:
SuperAnimDefMgr();
~SuperAnimDefMgr();
// std::string theSuperAnimFile include the absolute path
bool LoadSuperAnimMainDef(const std::string &theSuperAnimFile, bool effectFile = false);
public:
static SuperAnimDefMgr *GetInstance();
static void DestroyInstance();
// std::string theSuperAnimFile include the absolute path
SuperAnimMainDef *Load_GetSuperAnimMainDef(const std::string &theSuperAnimFile, bool effectFile = false);
void UnloadSuperAnimMainDef(const std::string &theName);
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// implement extern functions
SuperAnimHandler GetSuperAnimHandler(const std::string &theAbsAnimFile){
SuperAnimHandler aSuperAnimHander;
SuperAnimMainDef *aMainDef = SuperAnimDefMgr::GetInstance()->Load_GetSuperAnimMainDef(theAbsAnimFile);
if (aMainDef) {
aSuperAnimHander.mMainDefKey = theAbsAnimFile; // right now just use the animation file name as the key
aSuperAnimHander.mAnimRate = aMainDef->mAnimRate;
aSuperAnimHander.mWidth = aMainDef->mWidth;
aSuperAnimHander.mHeight = aMainDef->mHeight;
aSuperAnimHander.mCurFrameNum = aMainDef->mStartFrameNum;
aSuperAnimHander.mIsHandlerValid = true;
} else {
aSuperAnimHander.mIsHandlerValid = false;
}
return aSuperAnimHander;
}
static int sAnimObjIndex = 0;
static bool sShouldStartAnimObjDrawItr = false;
void BeginIterateAnimObjDrawInfo(){
sAnimObjIndex = 0;
sShouldStartAnimObjDrawItr = true;
}
bool IterateAnimObjDrawInfo(const SuperAnimHandler &theHandler, SuperAnimObjDrawInfo& theOutputObjDrawInfo){
if (!sShouldStartAnimObjDrawItr) {
assert(false && "Forgot to call BeginIterateAnimObjDrawInfo?");
return false;
}
if (!theHandler.IsValid()) {
assert(false && "The Animation handler is not valid.");
return false;
}
SuperAnimMainDef *aMainDef = SuperAnimDefMgr::GetInstance()->Load_GetSuperAnimMainDef(theHandler.mMainDefKey);
if (aMainDef == NULL) {
assert(false && "I can't find the Animation definition.");
return false;
}
int aCurFrameNum = (int)theHandler.mCurFrameNum;
SuperAnimFrame *aCurFrame = &aMainDef->mFrames[aCurFrameNum];
if (sAnimObjIndex >= aCurFrame->mObjectVector.size()) {
// we have iterated all objects in this frame
sShouldStartAnimObjDrawItr = false;
return false;
}
SuperAnimObject *aCurObject = &aCurFrame->mObjectVector[sAnimObjIndex];
// find the image, fill the sprite id
SuperAnimImage *aSuperAnimImage = &aMainDef->mImageVector[aCurObject->mResNum];
theOutputObjDrawInfo.mSpriteId = aSuperAnimImage->mSpriteId;
// do the interpolateion to next frame for transform & color
if (aCurFrameNum == aMainDef->mEndFrameNum) {
// reach the end frame, don't need to do any interpolation
theOutputObjDrawInfo.mTransform = aCurObject->mTransform;
theOutputObjDrawInfo.mColor = aCurObject->mColor;
} else {
int aNextFrameNum = aCurFrameNum + 1;
bool finishedInterp = false;
SuperAnimFrame *aNextFrame = &aMainDef->mFrames[aNextFrameNum];
for (int i = 0; i < aNextFrame->mObjectVector.size(); ++i) {
SuperAnimObject *anObj = &aNextFrame->mObjectVector[i];
if (anObj->mObjectNum == aCurObject->mObjectNum &&
anObj->mResNum == aCurObject->mResNum) {
float anInterp = theHandler.mCurFrameNum - aCurFrameNum;
theOutputObjDrawInfo.mTransform = aCurObject->mTransform.InterpolateTo(anObj->mTransform, anInterp);
theOutputObjDrawInfo.mColor = aCurObject->mColor.InterpolateTo(anObj->mColor, anInterp);
finishedInterp = true;
break;
}
}
if (!finishedInterp) {
// we miss the object in next frame?
// never mind
theOutputObjDrawInfo.mTransform = aCurObject->mTransform;
theOutputObjDrawInfo.mColor = aCurObject->mColor;
}
}
theOutputObjDrawInfo.mTransform = theOutputObjDrawInfo.mTransform.TransformSrc(aSuperAnimImage->mTransform);
SuperAnimMatrix3 aMatrix;
aMatrix.LoadIdentity();
aMatrix.m02 = aSuperAnimImage->mWidth * 0.5f;
aMatrix.m12 = aSuperAnimImage->mHeight * 0.5f;
theOutputObjDrawInfo.mTransform.mMatrix = theOutputObjDrawInfo.mTransform.mMatrix * aMatrix;
sAnimObjIndex++;
return true;
}
void IncAnimFrameNum(SuperAnimHandler &theMainDefHandler, float theDeltaTime, bool &hitNewLabel){
if (!theMainDefHandler.IsValid()) {
return;
}
int aLastFrameNum = (int)theMainDefHandler.mCurFrameNum;
theMainDefHandler.mCurFrameNum += theDeltaTime * theMainDefHandler.mAnimRate;
int aCurFrame = (int)theMainDefHandler.mCurFrameNum;
if (aCurFrame != aLastFrameNum) // Reach new frame
{
// Check whether reach a new label frame
bool aIsNewLabel = false;
if (aCurFrame >= theMainDefHandler.mLastFrameNumOfCurLabel) {
theMainDefHandler.mCurFrameNum = theMainDefHandler.mLastFrameNumOfCurLabel;
aIsNewLabel = true;
}
hitNewLabel = aIsNewLabel;
}
}
bool HasSection(const SuperAnimHandler &theHandler, std::string theLabelName){
SuperAnimMainDef *aMainDef = SuperAnimDefMgr::GetInstance()->Load_GetSuperAnimMainDef(theHandler.mMainDefKey);
if (aMainDef == NULL) {
return false;
}
for (SuperAnimLabelArray::const_iterator it = aMainDef->mLabels.begin(); it != aMainDef->mLabels.end(); ++it)
{
if (it->mLabelName == theLabelName)
return true;
}
return false;
}
bool PlayBySection(SuperAnimHandler &theHandler, std::string theLabelName){
SuperAnimMainDef *aMainDef = SuperAnimDefMgr::GetInstance()->Load_GetSuperAnimMainDef(theHandler.mMainDefKey);
if (aMainDef == NULL) {
theHandler.mIsHandlerValid = false;
return false;
}
for (SuperAnimLabelArray::const_iterator it = aMainDef->mLabels.begin(); it != aMainDef->mLabels.end(); ++it)
{
if (it->mLabelName == theLabelName)
{
theHandler.mCurFrameNum = it->mStartFrameNum;
theHandler.mCurLabel = theLabelName;
theHandler.mFirstFrameNumOfCurLabel = it->mStartFrameNum;
theHandler.mLastFrameNumOfCurLabel = it->mEndFrameNum;
theHandler.mIsHandlerValid = true;
return true;
}
}
theHandler.mIsHandlerValid = false;
return false;
}
bool LoadAnimFile(const std::string &theAbsAnimFile){
return SuperAnimDefMgr::GetInstance()->Load_GetSuperAnimMainDef(theAbsAnimFile) != NULL;
}
void UnloadAnimFile(const std::string &theAbsAnimFile){
SuperAnimDefMgr::GetInstance()->UnloadSuperAnimMainDef(theAbsAnimFile);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
typedef unsigned char uchar;
typedef std::vector<uchar> ByteVector;
class BufferReader
{
public:
ByteVector mData;
int mDataBitSize;
mutable int mReadBitPos;
mutable int mWriteBitPos;
public:
BufferReader();
virtual ~BufferReader();
void SetData(uchar* thePtr, int theCount);
uchar* GetDataPtr();
int GetDataLen() const;
void Clear();
uchar ReadByte() const;
bool ReadBoolean() const;
short ReadShort() const;
int ReadLong() const;
std::string ReadString() const;
};
BufferReader::BufferReader()
{
mDataBitSize = 0;
mReadBitPos = 0;
mWriteBitPos = 0;
}
BufferReader::~BufferReader()
{
}
void BufferReader::SetData(uchar* thePtr, int theCount)
{
mData.clear();
mData.reserve(theCount);
mData.insert(mData.begin(), thePtr, thePtr + theCount);
mDataBitSize = mData.size() * 8;
}
uchar* BufferReader::GetDataPtr()
{
if (mData.size() == 0)
return NULL;
return &mData[0];
}
int BufferReader::GetDataLen() const
{
return (mDataBitSize + 7) / 8; // Round up
}
void BufferReader::Clear()
{
mReadBitPos = 0;
mWriteBitPos = 0;
mDataBitSize = 0;
mData.clear();
}
uchar BufferReader::ReadByte() const
{
if ((mReadBitPos + 7)/8 >= (int)mData.size())
{
return 0; // Underflow
}
if (mReadBitPos % 8 == 0)
{
uchar b = mData[mReadBitPos/8];
mReadBitPos += 8;
return b;
}
else
{
int anOfs = mReadBitPos % 8;
uchar b = 0;
b = mData[mReadBitPos/8] >> anOfs;
b |= mData[(mReadBitPos/8)+1] << (8 - anOfs);
mReadBitPos += 8;
return b;
}
}
bool BufferReader::ReadBoolean() const
{
return ReadByte() != 0;
}
short BufferReader::ReadShort() const
{
short aShort = ReadByte();
aShort |= ((short) ReadByte() << 8);
return aShort;
}
int BufferReader::ReadLong() const
{
int aLong = ReadByte();
aLong |= ((int) ReadByte()) << 8;
aLong |= ((int) ReadByte()) << 16;
aLong |= ((int) ReadByte()) << 24;
return aLong;
}
std::string BufferReader::ReadString() const
{
std::string aString;
int aLen = ReadShort();
for (int i = 0; i < aLen; i++)
aString += (char) ReadByte();
return aString;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static SuperAnimDefMgr *sInstance = NULL;
SuperAnimDefMgr::SuperAnimDefMgr()
{
}
SuperAnimDefMgr::~SuperAnimDefMgr()
{
mMainDefCache.clear();
}
SuperAnimDefMgr * SuperAnimDefMgr::GetInstance()
{
if (sInstance == NULL)
{
sInstance = new SuperAnimDefMgr();
}
return sInstance;
}
void SuperAnimDefMgr::DestroyInstance()
{
if (sInstance)
{
delete sInstance;
sInstance = NULL;
}
}
typedef std::map<int, SuperAnimObject> IntToSuperAnimObjectMap;
bool SuperAnimLabelLess(const SuperAnimLabel& a, const SuperAnimLabel& b){
// bug fix:
// what was on my mind when I implemented this function???
// a little confused...
// if (a.mStartFrameNum != b.mStartFrameNum) {
// return a.mStartFrameNum < b.mStartFrameNum;
// }
//
// return true;
return a.mStartFrameNum < b.mStartFrameNum;
}
bool SuperAnimDefMgr::LoadSuperAnimMainDef(const std::string &theSuperAnimFile, bool effectFile)
{
std::string aFullPath = theSuperAnimFile;
std::string aCurDir = "";
int aLastSlash = max((int) theSuperAnimFile.rfind('\\'), (int) theSuperAnimFile.rfind('/'));
if (aLastSlash != std::string::npos){
aCurDir = theSuperAnimFile.substr(0, aLastSlash);
}
unsigned long aFileSize = 0;
unsigned char *aFileBuffer = GetFileData(aFullPath.c_str(), "rb", &aFileSize);
if (aFileBuffer == NULL)
{
assert(false && "Cannot allocate memory.");
return false;
}
BufferReader aBuffer;
aBuffer.SetData(aFileBuffer, aFileSize);
// free memory
delete[] aFileBuffer;
if (aBuffer.ReadLong() != 0x2E53414D)
{
assert(false && "Bad file format.");
return false;
}
int aVersion = aBuffer.ReadLong();
if (aVersion != SAM_VERSION)
{
assert(false && "Wrong version.");
return false;
}
SuperAnimMainDef &aMainDef = mMainDefCache[theSuperAnimFile];
aMainDef.mAnimRate = aBuffer.ReadByte();
if (effectFile) {
aMainDef.mX = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
aMainDef.mY = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
aMainDef.mWidth = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
aMainDef.mHeight = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
} else {
aMainDef.mX = aBuffer.ReadLong() / TWIPS_PER_PIXEL;
aMainDef.mY = aBuffer.ReadLong() / TWIPS_PER_PIXEL;
aMainDef.mWidth = aBuffer.ReadLong() / TWIPS_PER_PIXEL;
aMainDef.mHeight = aBuffer.ReadLong() / TWIPS_PER_PIXEL;
}
SuperAnimLabelArray aSuperAnimLabelArray;
int aNumImages = aBuffer.ReadShort();
aMainDef.mImageVector.resize(aNumImages);
for (int anImageNum = 0; anImageNum < aNumImages; ++anImageNum)
{
SuperAnimImage &aSuperAnimImage = aMainDef.mImageVector[anImageNum];
aSuperAnimImage.mImageName = aBuffer.ReadString();
aSuperAnimImage.mWidth = aBuffer.ReadShort();
aSuperAnimImage.mHeight = aBuffer.ReadShort();
aSuperAnimImage.mTransform.mMatrix.m00 = aBuffer.ReadLong() / (LONG_TO_FLOAT * TWIPS_PER_PIXEL);
aSuperAnimImage.mTransform.mMatrix.m01 = -aBuffer.ReadLong() / (LONG_TO_FLOAT * TWIPS_PER_PIXEL);
aSuperAnimImage.mTransform.mMatrix.m10 = -aBuffer.ReadLong() / (LONG_TO_FLOAT * TWIPS_PER_PIXEL);
aSuperAnimImage.mTransform.mMatrix.m11 = aBuffer.ReadLong() / (LONG_TO_FLOAT * TWIPS_PER_PIXEL);
aSuperAnimImage.mTransform.mMatrix.m02 = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
aSuperAnimImage.mTransform.mMatrix.m12 = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
std::string aImagePath;
if (aCurDir.empty()) {
aImagePath = aSuperAnimImage.mImageName;
} else {
aImagePath = aCurDir + '/' + aSuperAnimImage.mImageName;
}
aSuperAnimImage.mSpriteId = LoadSuperAnimSprite(aImagePath);
}
int aNumFrames = aBuffer.ReadShort();
assert(aNumFrames > 0 && "We don't have valid frames.");
aMainDef.mStartFrameNum = 0;
aMainDef.mEndFrameNum = aNumFrames - 1;
aMainDef.mFrames.resize(aNumFrames);
IntToSuperAnimObjectMap aCurObjectMap;
for (int aFrameNum = 0; aFrameNum < aNumFrames; ++aFrameNum)
{
SuperAnimFrame &aFrame = aMainDef.mFrames[aFrameNum];
uchar aFrameFlags = aBuffer.ReadByte();
if (aFrameFlags & FRAMEFLAGS_REMOVES)
{
int aNumRemoves = aBuffer.ReadByte();
for (int aRemoveNum = 0; aRemoveNum < aNumRemoves; ++ aRemoveNum)
{
int anObjectId = aBuffer.ReadShort();
IntToSuperAnimObjectMap::iterator anIt = aCurObjectMap.find(anObjectId);
if (anIt != aCurObjectMap.end())
{
aCurObjectMap.erase(anIt);
}
}
}
if (aFrameFlags & FRAMEFLAGS_ADDS)
{
int aNumAdds = aBuffer.ReadByte();
for(int anAddNum = 0; anAddNum < aNumAdds; ++anAddNum)
{
int anObjNum = (aBuffer.ReadShort() & 0x07FF);
SuperAnimObject& aSuperAnimObject = aCurObjectMap[anObjNum];
aSuperAnimObject.mObjectNum = anObjNum;
aSuperAnimObject.mResNum = aBuffer.ReadByte();
aSuperAnimObject.mColor = Color(255, 255, 255, 255);
}
}
if (aFrameFlags & FRAMEFLAGS_MOVES)
{
int aNumMoves = aBuffer.ReadByte();
for (int aMoveNum = 0; aMoveNum < aNumMoves; ++ aMoveNum)
{
unsigned short aFlagsAndObjectNum = aBuffer.ReadShort();
int anObjectNum = aFlagsAndObjectNum & 0x03FF;
IntToSuperAnimObjectMap::iterator anIt = aCurObjectMap.find(anObjectNum);
if (anIt == aCurObjectMap.end())
continue;
SuperAnimObject &aSuperAnimObject = anIt->second;
aSuperAnimObject.mTransform.mMatrix.LoadIdentity();
if (aFlagsAndObjectNum & MOVEFLAGS_MATRIX)
{
aSuperAnimObject.mTransform.mMatrix.m00 = aBuffer.ReadLong() / LONG_TO_FLOAT;
aSuperAnimObject.mTransform.mMatrix.m01 = -aBuffer.ReadLong() / LONG_TO_FLOAT;
aSuperAnimObject.mTransform.mMatrix.m10 = -aBuffer.ReadLong() / LONG_TO_FLOAT;
aSuperAnimObject.mTransform.mMatrix.m11 = aBuffer.ReadLong() / LONG_TO_FLOAT;
}
else if (aFlagsAndObjectNum & MOVEFLAGS_ROTATE)
{
float aRot = aBuffer.ReadShort() / 1000.0f;
float sinRot = sinf(aRot);
float cosRot = cosf(aRot);
aSuperAnimObject.mTransform.mMatrix.m00 = cosRot;
aSuperAnimObject.mTransform.mMatrix.m01 = sinRot;
aSuperAnimObject.mTransform.mMatrix.m10 = -sinRot;
aSuperAnimObject.mTransform.mMatrix.m11 = cosRot;
}
SuperAnimMatrix3 aMatrix;
aMatrix.LoadIdentity();
if (aFlagsAndObjectNum & MOVEFLAGS_LONGCOORDS)
{
aMatrix.m02 = aBuffer.ReadLong() / TWIPS_PER_PIXEL;
aMatrix.m12 = aBuffer.ReadLong() / TWIPS_PER_PIXEL;
}
else
{
aMatrix.m02 = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
aMatrix.m12 = aBuffer.ReadShort() / TWIPS_PER_PIXEL;
}
aSuperAnimObject.mTransform.mMatrix = aMatrix * aSuperAnimObject.mTransform.mMatrix;
if (aFlagsAndObjectNum & MOVEFLAGS_COLOR)
{
aSuperAnimObject.mColor.mRed = aBuffer.ReadByte();
aSuperAnimObject.mColor.mGreen = aBuffer.ReadByte();
aSuperAnimObject.mColor.mBlue = aBuffer.ReadByte();
aSuperAnimObject.mColor.mAlpha = aBuffer.ReadByte();
}
}
}
if (aFrameFlags & FRAMEFLAGS_FRAME_NAME)
{
std::string aFrameName = aBuffer.ReadString();
SuperAnimLabel aLabel;
aLabel.mLabelName = aFrameName;
aLabel.mStartFrameNum = aFrameNum;
//aMainDef.mLabels.insert(StringToIntMap::value_type(aFrameName, aFrameNum));
aSuperAnimLabelArray.push_back(aLabel);
}
aFrame.mObjectVector.resize(aCurObjectMap.size());
aFrame.mObjectVector.clear();
for (IntToSuperAnimObjectMap::iterator anIt = aCurObjectMap.begin(); anIt != aCurObjectMap.end(); ++anIt)
{
SuperAnimObject &anObject = anIt->second;
aFrame.mObjectVector.push_back(anObject);
}
}
// sort the label array & calculate the end frame for each label
std::sort(aSuperAnimLabelArray.begin(), aSuperAnimLabelArray.end(), SuperAnimLabelLess);
if (aSuperAnimLabelArray.size() > 1) {
for (int i = 0; i < aSuperAnimLabelArray.size() - 1; i++) {
SuperAnimLabel& aCurLabel = aSuperAnimLabelArray[i];
const SuperAnimLabel& aNextLabel = aSuperAnimLabelArray[i + 1];
aCurLabel.mEndFrameNum = aNextLabel.mStartFrameNum - 1;
}
SuperAnimLabel& aLastLabel = aSuperAnimLabelArray[aSuperAnimLabelArray.size() - 1];
aLastLabel.mEndFrameNum = aMainDef.mEndFrameNum;
} else {
// only have one section
SuperAnimLabel& aLabel = aSuperAnimLabelArray[0];
aLabel.mEndFrameNum = aMainDef.mEndFrameNum;
}
aMainDef.mLabels.clear();
for (int i = 0; i < aSuperAnimLabelArray.size(); i++) {
aMainDef.mLabels.push_back(aSuperAnimLabelArray[i]);
}
return true;
}
SuperAnimMainDef *SuperAnimDefMgr::Load_GetSuperAnimMainDef(const std::string &theSuperAnimFile, bool effectFile)
{
SuperAnimMainDefMap::iterator anItr = mMainDefCache.find(theSuperAnimFile);
if (anItr != mMainDefCache.end())
{
return &anItr->second;
}
if (LoadSuperAnimMainDef(theSuperAnimFile, effectFile) == false)
return NULL;
return Load_GetSuperAnimMainDef(theSuperAnimFile, effectFile);
}
void SuperAnimDefMgr::UnloadSuperAnimMainDef(const std::string &theName)
{
SuperAnimMainDefMap::iterator anItr = mMainDefCache.find(theName);
if (anItr == mMainDefCache.end())
return;
// unload the sprites
for (int i = 0; i < (int)anItr->second.mImageVector.size(); ++i)
{
SuperAnimImage anImage = anItr->second.mImageVector[i];
UnloadSuperSprite(anImage.mSpriteId);
}
mMainDefCache.erase(anItr);
}
}// end namespace