aboutsummaryrefslogtreecommitdiff
path: root/engines/kyra/animator_tim.cpp
blob: 90f4ad89adf8a31f481879db48190886a4446d63 (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
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "kyra/script_tim.h"
#include "kyra/wsamovie.h"
#include "kyra/screen_lol.h"

#ifdef ENABLE_LOL
#include "kyra/lol.h"
#else
#include "kyra/screen_v2.h"
#endif

#include "common/system.h"

namespace Kyra {

#ifdef ENABLE_LOL
TimAnimator::TimAnimator(LoLEngine *engine, Screen_v2 *screen_v2, OSystem *system, bool useParts) : _vm(engine), _screen(screen_v2), _system(system), _useParts(useParts) {
#else
TimAnimator::TimAnimator(KyraEngine_v1 *engine, Screen_v2 *screen_v2, OSystem *system, bool useParts) : _vm(engine), _screen(screen_v2), _system(system), _useParts(useParts) {
#endif
	_animations = new Animation[TIM::kWSASlots];
	memset(_animations, 0, TIM::kWSASlots * sizeof(Animation));

	if (_useParts) {
		for (int i = 0; i < TIM::kWSASlots; i++) {
			_animations[i].parts = new AnimPart[TIM::kAnimParts];
			memset(_animations[i].parts, 0, TIM::kAnimParts * sizeof(AnimPart));
		}
	}
}

TimAnimator::~TimAnimator() {
	for (int i = 0; i < TIM::kWSASlots; i++) {
		delete _animations[i].wsa;
		if (_useParts)
			delete[] _animations[i].parts;
	}

	delete[] _animations;
}

void TimAnimator::init(int animIndex, Movie *wsa, int x, int y, int wsaCopyParams, int frameDelay) {
	Animation *anim = &_animations[animIndex];
	anim->wsa = wsa;
	anim->x = x;
	anim->y = y;
	anim->wsaCopyParams = wsaCopyParams;
	anim->frameDelay = frameDelay;
	anim->enable = 0;
	anim->lastPart = -1;
}

void TimAnimator::reset(int animIndex, bool clearStruct) {
	Animation *anim = &_animations[animIndex];
	if (!anim)
		return;
	anim->field_D = 0;
	anim->enable = 0;
	delete anim->wsa;
	anim->wsa = 0;

	if (clearStruct) {
		if (_useParts)
			delete[] anim->parts;

		memset(anim, 0, sizeof(Animation));

		if (_useParts) {
			anim->parts = new AnimPart[TIM::kAnimParts];
			memset(anim->parts, 0, TIM::kAnimParts * sizeof(AnimPart));
		}
	}
}

void TimAnimator::displayFrame(int animIndex, int page, int frame, int flags) {
	Animation *anim = &_animations[animIndex];
	if ((anim->wsaCopyParams & 0x4000) != 0)
		page = 2;
	// WORKAROUND for some bugged scripts that will try to display frames of non-existent animations
	if (anim->wsa)
		anim->wsa->displayFrame(frame, page, anim->x, anim->y, (flags == -1) ? (anim->wsaCopyParams & 0xF0FF) : flags, 0, 0);
	if (!page)
		_screen->updateScreen();
}

#ifdef ENABLE_LOL
void TimAnimator::setupPart(int animIndex, int part, int firstFrame, int lastFrame, int cycles, int nextPart, int partDelay, int f, int sfxIndex, int sfxFrame) {
	AnimPart *a = &_animations[animIndex].parts[part];
	a->firstFrame = firstFrame;
	a->lastFrame = lastFrame;
	a->cycles = cycles;
	a->nextPart = nextPart;
	a->partDelay = partDelay;
	a->field_A = f;
	a->sfxIndex = sfxIndex;
	a->sfxFrame = sfxFrame;
}

void TimAnimator::start(int animIndex, int part) {
	if (!_vm || !_system || !_screen)
		return;

	Animation *anim = &_animations[animIndex];
	anim->curPart = part;
	AnimPart *p = &anim->parts[part];
	anim->enable = 1;
	anim->nextFrame = _system->getMillis() + anim->frameDelay * _vm->_tickLength;
	anim->curFrame = p->firstFrame;
	anim->cyclesCompleted = 0;

	// WORKAROUND for some bugged scripts that will try to display frames of non-existent animations
	if (anim->wsa)
		anim->wsa->displayFrame(anim->curFrame - 1, 0, anim->x, anim->y, 0, 0, 0);
}

void TimAnimator::stop(int animIndex) {
	Animation *anim = &_animations[animIndex];
	anim->enable = 0;
	anim->field_D = 0;
	if (animIndex == 5) {
		delete anim->wsa;
		anim->wsa = 0;
	}
}

void TimAnimator::update(int animIndex) {
	if (!_vm || !_system || !_screen)
		return;

	Animation *anim = &_animations[animIndex];
	if (!anim->enable || anim->nextFrame >= _system->getMillis())
		return;

	AnimPart *p = &anim->parts[anim->curPart];
	anim->nextFrame = 0;

	int step = 0;
	if (p->lastFrame >= p->firstFrame) {
		step = 1;
		anim->curFrame++;
	} else {
		step = -1;
		anim->curFrame--;
	}

	if (anim->curFrame == (p->lastFrame + step)) {
		anim->cyclesCompleted++;

		if ((anim->cyclesCompleted > p->cycles) || anim->field_D) {
			anim->lastPart = anim->curPart;

			if ((p->nextPart == -1) || (anim->field_D && p->field_A)) {
				anim->enable = 0;
				anim->field_D = 0;
				return;
			}

			anim->nextFrame += (p->partDelay * _vm->_tickLength);
			anim->curPart = p->nextPart;

			p = &anim->parts[anim->curPart];
			anim->curFrame = p->firstFrame;
			anim->cyclesCompleted = 0;

		} else {
			anim->curFrame = p->firstFrame;
		}
	}

	if (p->sfxIndex != -1 && p->sfxFrame == anim->curFrame)
		_vm->snd_playSoundEffect(p->sfxIndex, -1);

	anim->nextFrame += (anim->frameDelay * _vm->_tickLength);

	anim->wsa->displayFrame(anim->curFrame - 1, 0, anim->x, anim->y, 0, 0, 0);
	anim->nextFrame += _system->getMillis();
}

void TimAnimator::playPart(int animIndex, int firstFrame, int lastFrame, int delay) {
	if (!_vm || !_system || !_screen)
		return;

	Animation *anim = &_animations[animIndex];

	int step = (lastFrame >= firstFrame) ? 1 : -1;
	for (int i = firstFrame; i != (lastFrame + step); i += step) {
		uint32 next = _system->getMillis() + delay * _vm->_tickLength;
		if (anim->wsaCopyParams & 0x4000) {
			_screen->copyRegion(112, 0, 112, 0, 176, 120, 6, 2);
			anim->wsa->displayFrame(i - 1, 2, anim->x, anim->y, anim->wsaCopyParams & 0x1000 ? 0x5000 : 0x4000, _vm->_transparencyTable1, _vm->_transparencyTable2);
			_screen->copyRegion(112, 0, 112, 0, 176, 120, 2, 0);
			_screen->updateScreen();
		} else {
			anim->wsa->displayFrame(i - 1, 0, anim->x, anim->y, 0, 0, 0);
			_screen->updateScreen();
		}
		int32 del  = (int32)(next - _system->getMillis());
		if (del > 0)
			_vm->delay(del, true);
	}
}

int TimAnimator::resetLastPart(int animIndex) {
	Animation *anim = &_animations[animIndex];
	int8 res = -1;
	SWAP(res, anim->lastPart);
	return res;
}
#endif

} // End of namespace Kyra