aboutsummaryrefslogtreecommitdiff
path: root/engines/draci/animation.cpp
blob: 74e876401ddcedee2803671b44d35d4224b3a80f (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
/* 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) : _vm(vm) {
	_id = kUnused;
	_z = 0;
	_relX = 0;
	_relY = 0;
	_scaleX = 1.0;
	_scaleY = 1.0;
	_playing = false;
	_looping = false;
	_tick = _vm->_system->getMillis();
	_currentFrame = 0;
}	

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

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

void Animation::setRelative(int relx, int rely) {

	// Delete the previous frame
	markDirtyRect(_vm->_screen->getSurface());

	_relX = relx;
	_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) {
	// Fetch the current frame's rectangle
	Drawable *frame = _frames[_currentFrame];
	Common::Rect frameRect = frame->getRect();			

	// Translate rectangle to compensate for relative coordinates	
	frameRect.translate(_relX, _relY);
	
	// Take animation scaling into account
	frameRect.setWidth(frameRect.width() * _scaleX);
	frameRect.setHeight(frameRect.height() * _scaleY);

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

void Animation::nextFrame(bool force) {

	// If there's only one or no frames, or if the animation is not playing, return
	if (getFramesNum() < 2 || !_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 == getFramesNum() - 1) && !_looping) {
			// When the animation reaches its end, stop it
			_vm->_anims->stop(_id);

			// Reset the frame to 0
			_currentFrame = 0;
		} 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", 
	_id, _tick, frame->getDelay(), _tick + frame->getDelay(), _vm->_system->getMillis(), 
	_currentFrame, _frames.size(), frame->getX(), frame->getY());
}

uint Animation::nextFrameNum() {

	if ((_currentFrame == getFramesNum() - 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;

	Drawable *frame = _frames[_currentFrame];

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

		int x = frame->getX();
		int y = frame->getY();

		// Take account relative coordinates
		int newX = x + _relX;
		int newY = y + _relY;

		// Translate the frame to those relative coordinates
		frame->setX(newX);
		frame->setY(newY);

		// Save scaled width and height
		int scaledWidth = frame->getScaledWidth();
		int scaledHeight = frame->getScaledHeight();

		// Take into account per-animation scaling and adjust the current frames dimensions	
		if (_scaleX != 1.0 || _scaleY != 1.0)
			frame->setScaled(scaledWidth * _scaleX, scaledHeight * _scaleY);

		// Draw frame
		frame->drawScaled(surface, false);

		// Revert back to old coordinates
		frame->setX(x);
		frame->setY(y);

		// Revert back to old dimensions
		frame->setScaled(scaledWidth, scaledHeight);
	}
}

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

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

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

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

int Animation::getRelativeX() {
	return _relX;
}

int Animation::getRelativeY() {
	return _relY;
}

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

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

void Animation::setScaleFactors(double scaleX, double scaleY) {
	markDirtyRect(_vm->_screen->getSurface());	
	
	_scaleX = scaleX;
	_scaleY = scaleY;
}

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

Drawable *Animation::getFrame(int frameNum) {

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

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

void Animation::deleteFrames() {
	
	for (int i = getFramesNum() - 1; i >= 0; --i) {		
		delete _frames[i];
		_frames.pop_back();	
	}
}

Animation *AnimationManager::addAnimation(int id, uint z, bool playing) {
	
	Animation *anim = new Animation(_vm);
	
	anim->setID(id);
	anim->setZ(z);
	anim->setPlaying(playing);

	insertAnimation(anim);

	return anim;
}

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

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

	if (anim) {
		anim->setPlaying(true);

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

void AnimationManager::stop(int id) {
	Animation *anim = getAnimation(id);
	
	// Clean up the last frame that was drawn before stopping
	anim->markDirtyRect(_vm->_screen->getSurface());

	if (anim) {
		anim->setPlaying(false);
	
		debugC(5, kDraciAnimationDebugLevel, "Stopping animation %d...", id);
	}
}

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 *animObj) {
	
	Common::List<Animation *>::iterator it;	

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

	_animations.insert(it, animObj);
}

void AnimationManager::addOverlay(Drawable *overlay, uint z) {
	Animation *anim = new Animation(_vm);
	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;	

	while(1) {
		next = cur;
		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;
			_animations.erase(next);

			insertAnimation(anim);
		}

		// Advance to next animation
		cur = next;
	}
}

void AnimationManager::deleteAnimation(int id) {
	
	Common::List<Animation *>::iterator it;
  
	for (it = _animations.begin(); it != _animations.end(); ++it) {
		if ((*it)->getID() == id) {
			(*it)->deleteFrames();
			_animations.erase(it);
			break;
		}
	}
}

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

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

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

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

	_animations.clear();
}

}