#include "StdAfx.h" #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 theWorldModule = 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 *theBitImage = new T2BitImage(theWorldModule, inSpriteDef[i].resID, true); mImages[i] = theBitImage; RECT theFrameRect; theFrameRect.left = 0; theFrameRect.top = 0; theFrameRect.right = inSpriteDef[i].width; theFrameRect.bottom = inSpriteDef[i].height; for (short j = 0; j < inSpriteDef[i].count; j++) { theImageObj->AddObject(name, j + 1, *theBitImage, &theFrameRect, true, false); OffsetRect(&theFrameRect, 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 interval = 60 / inLen; int endTick = GetTickCount() + interval; BOOL playing = mFrame < mNumOfFrame; while (playing) { playing = Step(); mDocument->GetMainView()->tmv_vf120(); int now; for (now = GetTickCount(); now < endTick; now = GetTickCount()) { // nothing there! } endTick = now + interval; } } BOOL T2Animation::Step() { Draw(); mFrame++; return (mFrame < mNumOfFrame); } void T2Animation::Draw() { if (HasNextStep()) { for (int i = 0; i < mNumOfSprite; i++) { const AnimationDef *theAnimationDef = mAnimationDef + (mFrame * mNumOfSprite) + i; CPoint pt = mPoint; pt.Offset(theAnimationDef->position); mDocument->mSprite.MoveSprite(mSpriteIndices[i], pt); mDocument->mSprite.ChangePattern(mSpriteIndices[i], theAnimationDef->pattern); mDocument->mSprite.ShowSprite(mSpriteIndices[i], true); } } }