#include "GlobalFunc.h" #include "T2Animation.h" #include "T2BitImage.h" #include "T2ImageObj.h" #include "T2PluginSpecifier.h" #include "T2TowerDoc.h" #include "T2TowerMainView.h" T2Animation::T2Animation(T2TowerDoc* inDoc, short inNumOfFrame, short inNumOfSprite, const SpriteDef* inSpriteDef, const AnimationDef* inAnimationDef, T2Sprite::ELayer inLayer) { mDocument = inDoc; mNumOfFrame = inNumOfFrame; mNumOfSprite = inNumOfSprite; mAnimationDef = inAnimationDef; mFrame = 0; SetPt(&mPoint, 0, 0); int i; short maxCount = 0; for (i = 0; i < mNumOfSprite; i++) { if (maxCount < inSpriteDef[i].count) maxCount = inSpriteDef[i].count; } HINSTANCE theModuleHandle = inDoc->mWorldPluginSpecifier->mInstance; mSpriteIndices = new int[inNumOfSprite]; #line 41 _ASSERT(mSpriteIndices != NULL); mImageObjs = (T2ImageObj **) malloc(sizeof(T2ImageObj *) * inNumOfSprite); #line 43 _ASSERT(mImageObjs != NULL); mImages = (T2BitImage **) malloc(sizeof(T2BitImage *) * inNumOfSprite); #line 45 _ASSERT(mImages != NULL); for (i = 0; i < mNumOfSprite; i++) { CString name; name.Format("__Anime__%d", i); T2ImageObj *theImageObj = new T2ImageObj; mImageObjs[i] = theImageObj; T2BitImage *theImage = new T2BitImage(theModuleHandle, inSpriteDef[i].resID, true); mImages[i] = theImage; RECT rect; rect.left = 0; rect.top = 0; rect.right = inSpriteDef[i].width; rect.bottom = inSpriteDef[i].height; for (short j = 0; j < inSpriteDef[i].count; j++) { theImageObj->AddObject(name, j + 1, *theImage, &rect, true, false); OffsetRect(&rect, 0, inSpriteDef[i].height); } mSpriteIndices[i] = mDocument->mSprite.NewSprite(*theImageObj, name, inLayer); } } T2Animation::~T2Animation() { for (int i = 0; i < mNumOfSprite; i++) mDocument->mSprite.DeleteSprite(mSpriteIndices[i]); delete[] mSpriteIndices; if (mImageObjs) { for (int i = 0; i < mNumOfSprite; i++) { if (mImageObjs[i]) delete mImageObjs[i]; } free(mImageObjs); } if (mImages) { for (int i = 0; i < mNumOfSprite; i++) { if (mImages[i]) delete mImages[i]; } free(mImages); } } BOOL T2Animation::Start(POINT inPt, int inLen) { mPoint = inPt; mInterval = 60 / inLen; mTickCount = GetTickCount(); Draw(); return HasNextStep(); } BOOL T2Animation::Idle() { int newFrame = (GetTickCount() - mTickCount) / mInterval; if (newFrame != mFrame) { mFrame = newFrame; Draw(); } return HasNextStep(); } void T2Animation::Play(POINT inPt, int inLen) { mPoint = inPt; int delay = 60 / inLen; int endTick = GetTickCount() + delay; BOOL playing = mFrame < mNumOfFrame; while (playing) { playing = Step(); mDocument->GetTowerMainView()->tmv_vf120(); int now; for (now = GetTickCount(); now < endTick; now = GetTickCount()) { // nothing there! } endTick = now + delay; } } BOOL T2Animation::Step() { Draw(); mFrame++; return (mFrame < mNumOfFrame); } void T2Animation::Draw() { if (HasNextStep()) { for (int i = 0; i < mNumOfSprite; i++) { const AnimationDef *def = mAnimationDef + (mFrame * mNumOfSprite) + i; CPoint pt = mPoint; pt.Offset(def->position); mDocument->mSprite.MoveSprite(mSpriteIndices[i], pt); mDocument->mSprite.ChangePattern(mSpriteIndices[i], def->pattern); mDocument->mSprite.ShowSprite(mSpriteIndices[i], true); } } }