aboutsummaryrefslogtreecommitdiff
path: root/engines/access/animation.cpp
blob: e59874fe7ae8a09f7cf8317267a57d0d0159987a (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
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
/* 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 "common/endian.h"
#include "common/memstream.h"
#include "access/access.h"
#include "access/animation.h"

namespace Access {

AnimationResource::AnimationResource(AccessEngine *vm, Resource *res) {
	int count = res->_stream->readUint16LE();

	Common::Array<int> offsets;
	for (int i = 0; i < count; ++i)
		offsets.push_back(res->_stream->readUint32LE());

	_animations.reserve(count);
	for (int i = 0; i < count; ++i) {
		res->_stream->seek(offsets[i]);
		Animation *anim = new Animation(vm, res->_stream);
		_animations.push_back(anim);
	}
}

AnimationResource::~AnimationResource() {
	for (int i = 0; i < (int)_animations.size(); ++i)
		delete _animations[i];
}

/*------------------------------------------------------------------------*/

Animation::Animation(AccessEngine *vm, Common::SeekableReadStream *stream) :
		Manager(vm) {
	uint32 startOfs = stream->pos();

	_type = stream->readByte();
	_scaling = stream->readSByte();
	stream->readByte(); // unk
	_frameNumber = stream->readByte();
	_initialTicks = stream->readUint16LE();
	stream->readUint16LE(); // unk
	stream->readUint16LE(); // unk
	_loopCount = stream->readSint16LE();
	_countdownTicks = stream->readUint16LE();
	_currentLoopCount = stream->readSint16LE();
	stream->readUint16LE(); // unk

	Common::Array<uint16> frameOffsets;
	uint16 ofs;
	while ((ofs = stream->readUint16LE()) != 0)
		frameOffsets.push_back(ofs);

	for (int i = 0; i < (int)frameOffsets.size(); i++) {
		stream->seek(startOfs + frameOffsets[i]);

		AnimationFrame *frame = new AnimationFrame(stream, startOfs);
		_frames.push_back(frame);
	}
}

Animation::~Animation() {
	for (int i = 0; i < (int)_frames.size(); ++i)
		delete _frames[i];
}

typedef void(Animation::*AnimationMethodPtr)();

void Animation::animate() {
	static const AnimationMethodPtr METHODS[8] =
	{ &Animation::anim0, &Animation::anim1, &Animation::anim2, &Animation::anim3, 
	&Animation::anim4, &Animation::animNone, &Animation::animNone, &Animation::anim7 };

	(this->*METHODS[_type])();
}

void Animation::anim0() {
	if (_currentLoopCount != -1) {
		if (_countdownTicks != 0) {
			setFrame1(calcFrame());
		} else {
			_countdownTicks = _initialTicks;
			++_frameNumber;
			AnimationFrame *frame = calcFrame();

			if (frame == nullptr) {
				_frameNumber = 0;
				_currentLoopCount = -1;
				frame = calcFrame();
			}

			setFrame(frame);
		}
	}
}

void Animation::anim1() {
	if (_currentLoopCount == -1 || _countdownTicks != 0) {
		setFrame1(calcFrame());
	} else {
		_countdownTicks = _initialTicks;
		++_frameNumber;
		AnimationFrame *frame = calcFrame();

		if (frame == nullptr) {
			--_frameNumber;
			_currentLoopCount = -1;
			frame = calcFrame();
		}

		setFrame(frame);
	}
}

void Animation::anim2() {
	if (_countdownTicks != 0) {
		setFrame1(calcFrame());
	} else {
		_countdownTicks = _initialTicks;
		++_frameNumber;
		AnimationFrame *frame = calcFrame();

		if (frame == nullptr) {
			_frameNumber = 0;
			frame = calcFrame();
		}

		setFrame(frame);
	}
}

void Animation::anim3() {
	if (_currentLoopCount != -1) {
		if (_countdownTicks != 0) {
			setFrame1(calcFrame());
		} else {
			_countdownTicks = _initialTicks;
			++_frameNumber;
			AnimationFrame *frame = calcFrame();

			if (frame == nullptr) {
				--_currentLoopCount;
				_frameNumber = 0;
				frame = calcFrame();
			}

			setFrame(frame);
		}
	}
}

void Animation::anim4() {
	if (_currentLoopCount == -1 || _countdownTicks != 0) {
		setFrame1(calcFrame());
	} else {
		_countdownTicks = _initialTicks;
		++_frameNumber;
		AnimationFrame *frame = calcFrame();

		if (frame == nullptr) {
			if (--_currentLoopCount == -1) {
				setFrame1(calcFrame());
				return;
			} else {
				_frameNumber = 0;
				frame = calcFrame();
			}
		}

		setFrame(frame);
	}
}

void Animation::animNone() {
	// No implementation
}

void Animation::anim7() {
	setFrame(calcFrame1());
}

AnimationFrame *Animation::calcFrame() {
	return (_frameNumber < (int)_frames.size()) ? _frames[_frameNumber] : nullptr;
}

AnimationFrame *Animation::calcFrame1() {
	return _frames[0];
}

void Animation::setFrame(AnimationFrame *frame) {
	assert(frame);
	_countdownTicks += frame->_frameDelay;
	setFrame1(frame);
}

void Animation::setFrame1(AnimationFrame *frame) {
	_vm->_animation->_base.x = frame->_baseX;
	_vm->_animation->_base.y = frame->_baseY;

	// Loop to add image draw requests for the parts of the frame
	for (uint i = 0; i < frame->_parts.size(); ++i) {
		AnimationFramePart *part = frame->_parts[i];
		ImageEntry ie;

		// Set the flags
		ie._flags = part->_flags & ~IMGFLAG_UNSCALED;
		if (_vm->_animation->_frameScale == -1)
			ie._flags |= IMGFLAG_UNSCALED;

		// Set the other fields
		ie._spritesPtr = _vm->_objectsTable[part->_spritesIndex];
		ie._frameNumber = part->_frameIndex;
		ie._position = part->_position + _vm->_animation->_base;
		ie._offsetY = part->_offsetY - ie._position.y;

		_vm->_images.addToList(ie);
	}
}

/*------------------------------------------------------------------------*/

AnimationFrame::AnimationFrame(Common::SeekableReadStream *stream, int startOffset) {
	uint16 nextOffset;

	stream->readByte(); // unk
	_baseX = stream->readUint16LE();
	_baseY = stream->readUint16LE();
	_frameDelay = stream->readUint16LE();
	nextOffset = stream->readUint16LE();

	while (nextOffset != 0) {
		stream->seek(startOffset + nextOffset);

		AnimationFramePart *framePart = new AnimationFramePart(stream);
		_parts.push_back(framePart);

		nextOffset = stream->readUint16LE();
	}
}

AnimationFrame::~AnimationFrame() {
	for (int i = 0; i < (int)_parts.size(); ++i)
		delete _parts[i];
}

/*------------------------------------------------------------------------*/

AnimationFramePart::AnimationFramePart(Common::SeekableReadStream *stream) {
	_flags = stream->readByte();
	_spritesIndex = stream->readByte();
	_frameIndex = stream->readByte();
	_position.x = stream->readUint16LE();
	_position.y = stream->readUint16LE();
	_offsetY = stream->readUint16LE();
}

/*------------------------------------------------------------------------*/

AnimationManager::AnimationManager(AccessEngine *vm) : Manager(vm) {
	_animation = nullptr;
	_animStart = nullptr;
	_frameScale = 0;
}

AnimationManager::~AnimationManager() {
	delete _animation;
}

void AnimationManager::freeAnimationData() {
	delete _animation;
	_animation = nullptr;
	_animStart = nullptr;
}

void AnimationManager::clearTimers() {
	_animationTimers.clear();
}

void AnimationManager::loadAnimations(Resource *res) {
	_animationTimers.clear();
	delete _animation;
	_animation = new AnimationResource(_vm, res);
}


Animation *AnimationManager::setAnimation(int animId) {
	Animation *anim = findAnimation(animId);
	if (!anim)
		return nullptr;

	anim->_countdownTicks = anim->_initialTicks;
	anim->_frameNumber = 0;

	anim->_currentLoopCount = (anim->_type != 3 && anim->_type != 4) ? 0 : 
		anim->_loopCount;
	anim->_field10 = 0;
		
	return anim;
}

void AnimationManager::setAnimTimer(Animation *anim) {
	_animationTimers.push_back(anim);
}

Animation *AnimationManager::findAnimation(int animId) {
	_animStart = (_animation == nullptr) ? nullptr : _animation->getAnimation(animId);
	return _animStart;
}

void AnimationManager::animate(int animId) {
	Animation *anim = findAnimation(animId);
	_frameScale = anim->_scaling;
	anim->animate();
}

void AnimationManager::updateTimers() {
	for (uint idx = 0; idx < _animationTimers.size(); ++idx) {
		if (_animationTimers[idx]->_countdownTicks > 0)
			_animationTimers[idx]->_countdownTicks--;
	}
}

} // End of namespace Access