aboutsummaryrefslogtreecommitdiff
path: root/engines/draci/animation.cpp
blob: 3429884397e33a7c50bbbc66ba0701c04903804b (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "draci/draci.h"
#include "draci/animation.h"

namespace Draci {

Animation::Animation(DraciEngine *vm, int index) : _vm(vm) {
	_id = kUnused;
	_index = index;
	_z = 0;
	_displacement = kNoDisplacement;
	_playing = false;
	_looping = false;
	_paused = false;
	_tick = _vm->_system->getMillis();
	_currentFrame = 0;
	_callback = &Animation::doNothing;
}

Animation::~Animation() {
	deleteFrames();
}

bool Animation::isLooping() const {
	return _looping;
}

void Animation::setRelative(int relx, int rely) {
	// Delete the previous frame if there is one
	if (_frames.size() > 0)
		markDirtyRect(_vm->_screen->getSurface());

	_displacement.relX = relx;
	_displacement.relY = rely;
}

void Animation::setLooping(bool looping) {
	_looping = looping;
	debugC(7, kDraciAnimationDebugLevel, "Setting looping to %d on animation %d",
		looping, _id);
}

void Animation::markDirtyRect(Surface *surface) const {
	// Fetch the current frame's rectangle
	Drawable *frame = _frames[_currentFrame];
	Common::Rect frameRect = frame->getRect(_displacement);

	// Mark the rectangle dirty on the surface
	surface->markDirtyRect(frameRect);
}

void Animation::nextFrame(bool force) {
	// If there are no frames or if the animation is not playing, return
	if (getFrameCount() == 0 || !_playing)
		return;

	Drawable *frame = _frames[_currentFrame];
	Surface *surface = _vm->_screen->getSurface();

	if (force || (_tick + frame->getDelay() <= _vm->_system->getMillis())) {
		// If we are at the last frame and not looping, stop the animation
		// The animation is also restarted to frame zero
		if ((_currentFrame == getFrameCount() - 1) && !_looping) {
			// When the animation reaches its end, call the preset callback
			(this->*_callback)();
		} else {
			// Mark old frame dirty so it gets deleted
			markDirtyRect(surface);

			_currentFrame = nextFrameNum();
			_tick = _vm->_system->getMillis();

			// Fetch new frame and mark it dirty
			markDirtyRect(surface);
		}
	}

	debugC(6, kDraciAnimationDebugLevel,
	"anim=%d tick=%d delay=%d tick+delay=%d currenttime=%d frame=%d framenum=%d x=%d y=%d z=%d",
	_id, _tick, frame->getDelay(), _tick + frame->getDelay(), _vm->_system->getMillis(),
	_currentFrame, _frames.size(), frame->getX() + getRelativeX(), frame->getY() + getRelativeY(), _z);
}

uint Animation::nextFrameNum() const {
	if (_paused)
		return _currentFrame;

	if ((_currentFrame == getFrameCount() - 1) && _looping)
		return 0;
	else
		return _currentFrame + 1;
}

void Animation::drawFrame(Surface *surface) {
	// If there are no frames or the animation is not playing, return
	if (_frames.size() == 0 || !_playing)
		return;

	const Drawable *frame = _frames[_currentFrame];

	if (_id == kOverlayImage) {
		frame->draw(surface, false);
	} else {
		// Draw frame
		frame->drawReScaled(surface, false, _displacement);
	}
}

void Animation::setID(int id) {
	_id = id;
}

int Animation::getID() const {
	return _id;
}

void Animation::setZ(uint z) {
	_z = z;
}

uint Animation::getZ() const {
	return _z;
}

int Animation::getRelativeX() const {
	return _displacement.relX;
}

int Animation::getRelativeY() const {
	return _displacement.relY;
}

bool Animation::isPlaying() const {
	return _playing;
}

void Animation::setPlaying(bool playing) {
	_tick = _vm->_system->getMillis();
	_playing = playing;
}

bool Animation::isPaused() const {
	return _paused;
}

void Animation::setPaused(bool paused) {
	_paused = paused;
}

void Animation::setScaleFactors(double scaleX, double scaleY) {
	debugC(5, kDraciAnimationDebugLevel,
		"Setting scaling factors on anim %d (scaleX: %.3f scaleY: %.3f)",
		_id, scaleX, scaleY);

	markDirtyRect(_vm->_screen->getSurface());

	_displacement.extraScaleX = scaleX;
	_displacement.extraScaleY = scaleY;
}

double Animation::getScaleX() const {
	return _displacement.extraScaleX;
}

double Animation::getScaleY() const {
	return _displacement.extraScaleY;
}

void Animation::addFrame(Drawable *frame) {
	_frames.push_back(frame);
}

int Animation::getIndex() const {
	return _index;
}

void Animation::setIndex(int index) {
	_index = index;
}

Drawable *Animation::getFrame(int frameNum) {
	// If there are no frames stored, return NULL
	if (_frames.size() == 0) {
		return NULL;
	}

	// If no argument is passed, return the current frame
	if (frameNum == kCurrentFrame) {
		return _frames[_currentFrame];
	} else {
		return _frames[frameNum];
	}
}

uint Animation::getFrameCount() const {
	return _frames.size();
}

uint Animation::currentFrameNum() const {
	return _currentFrame;
}

void Animation::setCurrentFrame(uint frame) {
	// Check whether the value is sane
	if (frame >= _frames.size()) {
		return;
	}

	_currentFrame = frame;
}

void Animation::deleteFrames() {
	// If there are no frames to delete, return
	if (_frames.size() == 0) {
		return;
	}

	markDirtyRect(_vm->_screen->getSurface());

	for (int i = getFrameCount() - 1; i >= 0; --i) {
		delete _frames[i];
		_frames.pop_back();
	}
}

void Animation::stopAnimation() {
	_vm->_anims->stop(_id);
}

void Animation::exitGameLoop() {
	_vm->_game->setExitLoop(true);
}

Animation *AnimationManager::addAnimation(int id, uint z, bool playing) {
	// Increment animation index
	++_lastIndex;

	Animation *anim = new Animation(_vm, _lastIndex);

	anim->setID(id);
	anim->setZ(z);
	anim->setPlaying(playing);

	insertAnimation(anim);

	return anim;
}

Animation *AnimationManager::addItem(int id, bool playing) {
	Animation *anim = new Animation(_vm, kIgnoreIndex);

	anim->setID(id);
	anim->setZ(256);
	anim->setPlaying(playing);

	insertAnimation(anim);

	return anim;
}

Animation *AnimationManager::addText(int id, bool playing) {
	Animation *anim = new Animation(_vm, kIgnoreIndex);

	anim->setID(id);
	anim->setZ(257);
	anim->setPlaying(playing);

	insertAnimation(anim);

	return anim;
}

void AnimationManager::play(int id) {
	Animation *anim = getAnimation(id);

	if (anim) {
		// Mark the first frame dirty so it gets displayed
		anim->markDirtyRect(_vm->_screen->getSurface());

		anim->setPlaying(true);

		debugC(3, kDraciAnimationDebugLevel, "Playing animation %d...", id);
	}
}

void AnimationManager::stop(int id) {
	Animation *anim = getAnimation(id);

	if (anim) {
		// Clean up the last frame that was drawn before stopping
		anim->markDirtyRect(_vm->_screen->getSurface());

		anim->setPlaying(false);

		// Reset the animation to the beginning
		anim->setCurrentFrame(0);

		debugC(3, kDraciAnimationDebugLevel, "Stopping animation %d...", id);
	}
}

void AnimationManager::pauseAnimations() {
	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getID() > 0 || (*it)->getID() == kTitleText) {
			// Clean up the last frame that was drawn before stopping
			(*it)->markDirtyRect(_vm->_screen->getSurface());

			(*it)->setPaused(true);
		}
	}
}

void AnimationManager::unpauseAnimations() {
	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->isPaused()) {
			// Clean up the last frame that was drawn before stopping
			(*it)->markDirtyRect(_vm->_screen->getSurface());

			(*it)->setPaused(false);
		}
	}
}

Animation *AnimationManager::getAnimation(int id) {
	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getID() == id) {
			return *it;
		}
	}

	return NULL;
}

void AnimationManager::insertAnimation(Animation *anim) {
	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if (anim->getZ() < (*it)->getZ())
			break;
	}

	_animations.insert(it, anim);
}

void AnimationManager::addOverlay(Drawable *overlay, uint z) {
	// Since this is an overlay, we don't need it to be deleted
	// when the GPL Release command is invoked so we pass the index
	// as kIgnoreIndex
	Animation *anim = new Animation(_vm, kIgnoreIndex);

	anim->setID(kOverlayImage);
	anim->setZ(z);
	anim->setPlaying(true);
	anim->addFrame(overlay);

	insertAnimation(anim);
}

void AnimationManager::drawScene(Surface *surf) {
	// Fill the screen with colour zero since some rooms may rely on the screen being black
	_vm->_screen->getSurface()->fill(0);

	sortAnimations();

	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if (! ((*it)->isPlaying()) ) {
			continue;
		}

		(*it)->nextFrame();
		(*it)->drawFrame(surf);
	}
}

void AnimationManager::sortAnimations() {
	Common::List<Animation *>::iterator cur;
	Common::List<Animation *>::iterator next;

	cur = _animations.begin();

	// If the list is empty, we're done
	if (cur == _animations.end())
		return;

	bool hasChanged;

	do {
		hasChanged = false;
		cur = _animations.begin();
		next = cur;

		while (true) {
			next++;

			// If we are at the last element, we're done
			if (next == _animations.end())
				break;

			// If we find an animation out of order, reinsert it
			if ((*next)->getZ() < (*cur)->getZ()) {

				Animation *anim = *next;
				next = _animations.reverse_erase(next);

				insertAnimation(anim);
				hasChanged = true;
			}

			// Advance to next animation
			cur = next;
		}
	} while (hasChanged);
}

void AnimationManager::deleteAnimation(int id) {
	Common::List<Animation *>::iterator it;

	int index = -1;

	// Iterate for the first time to delete the animation
	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getID() == id) {
			// Remember index of the deleted animation
			index = (*it)->getIndex();

			delete *it;
			_animations.erase(it);

			debugC(3, kDraciAnimationDebugLevel, "Deleting animation %d...", id);

			break;
		}
	}

	// Iterate the second time to decrease indexes greater than the deleted animation index
	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getIndex() > index && (*it)->getIndex() != kIgnoreIndex) {
			(*it)->setIndex((*it)->getIndex() - 1);
		}
	}

	// Decrement index of last animation
	_lastIndex -= 1;
}

void AnimationManager::deleteOverlays() {
	debugC(3, kDraciAnimationDebugLevel, "Deleting overlays...");

	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getID() == kOverlayImage) {
			delete *it;
			it = _animations.reverse_erase(it);
		}
	}
}

void AnimationManager::deleteAll() {
	debugC(3, kDraciAnimationDebugLevel, "Deleting all animations...");

	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		delete *it;
	}

	_animations.clear();

	_lastIndex = -1;
}

int AnimationManager::getLastIndex() const {
	return _lastIndex;
}

void AnimationManager::deleteAfterIndex(int index) {
	Common::List<Animation *>::iterator it;

	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getIndex() > index) {

			debugC(3, kDraciAnimationDebugLevel, "Deleting animation %d...", (*it)->getID());

			delete *it;
			it = _animations.reverse_erase(it);
		}
	}

	_lastIndex = index;
}

int AnimationManager::getTopAnimationID(int x, int y) const {
	Common::List<Animation *>::const_iterator it;

	// The default return value if no animations were found on these coordinates (not even overlays)
	// i.e. the black background shows through so treat it as an overlay
	int retval = kOverlayImage;

	// Get transparent colour for the current screen
	const int transparent = _vm->_screen->getSurface()->getTransparentColour();

	for (it = _animations.reverse_begin(); it != _animations.end(); --it) {

		Animation *anim = *it;

		// If the animation is not playing, ignore it
		if (!anim->isPlaying() || anim->isPaused()) {
			continue;
		}

		const Drawable *frame = anim->getFrame();

		if (frame == NULL) {
			continue;
		}

		if (frame->getRect(anim->getDisplacement()).contains(x, y)) {
			if (frame->getType() == kDrawableText) {

				retval = anim->getID();

			} else if (frame->getType() == kDrawableSprite &&
					   reinterpret_cast<const Sprite *>(frame)->getPixel(x, y, anim->getDisplacement()) != transparent) {

				retval = anim->getID();
			}
		}

		// Found an animation
		if (retval != kOverlayImage)
			break;
	}

	return retval;
}

}