summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2Animation.cpp
blob: 6605ddd9a95446191476a35a76bb5dd636d9de60 (plain)
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
#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);
        }
    }
}