File: animation.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (288 lines) | stat: -rw-r--r-- 7,627 bytes parent folder | download | duplicates (3)
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
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM 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/foreach.h"

#include "engines/grim/animation.h"
#include "engines/grim/resource.h"
#include "engines/grim/model.h"
#include "engines/grim/debug.h"
#include "engines/grim/savegame.h"

namespace Grim {

Animation::Animation(const Common::String &keyframe, AnimManager *manager, int pr1, int pr2) :
		_manager(manager), _priority1(pr1), _priority2(pr2), _paused(true),
		_active(false), _time(-1), _fade(1.f), _fadeMode(None) {
	_keyframe = g_resourceloader->getKeyframe(keyframe);
}

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

void Animation::activate() {
	if (!_active) {
		_active = true;
		_manager->addAnimation(this, _priority1, _priority2);
	}
}

void Animation::deactivate() {
	if (_active) {
		_active = false;
		_manager->removeAnimation(this);
	}
}

void Animation::play(RepeatMode repeatMode) {
	_repeatMode = repeatMode;
	if (_repeatMode != Looping)
		_time = -1;
	_paused = false;
	// Reset the fading, so that a fading out a chore and playing another one with an animation in common
	// results in the animation being actually played. (You can check that with Olivia by the car in set me,
	// when jumping with j+ts; me.olivia_search_idles() in me.lua)
	if (_fadeMode == FadeOut)
		_fadeMode = None;
	activate();
}

void Animation::fade(FadeMode fadeMode, int fadeLength) {
	if (!_active) {
		if (fadeMode == FadeIn) {
			_repeatMode = PauseAtEnd;
			_time = -1;
			_fade = 0.f;
			_paused = false;
		}
	}
	_fadeMode = fadeMode;
	_fadeLength = fadeLength;
}

void Animation::pause(bool p) {
	_paused = p;
}

void Animation::stop() {
	_fadeMode = None;
	_time = -1;
	_fade = 1.f;
	_paused = false;
	deactivate();
}

bool Animation::getIsActive() const {
	return _active;
}

Animation::FadeMode Animation::getFadeMode() const {
	return _fadeMode;

}

int Animation::update(uint time) {
	// For first time through newTime will be 0
	int newTime = 0;
	if (_time >= 0 && !_paused)
		newTime = _time + time;

	int marker = 0;
	if (!_paused) {
		marker = _keyframe->getMarker(_time / 1000.f, newTime / 1000.f);
		_time = newTime;
	}

	int animLength = (int)(_keyframe->getLength() * 1000);

	if (_fadeMode != None) {
		if (_fadeMode == FadeIn) {
			_fade += (float)time / (float)_fadeLength;
			if (_fade >= 1.f) {
				_fade = 1.f;
				_fadeMode = None;
			}
		} else {
			_fade -= (float)time / (float)_fadeLength;
			if (_fade <= 0.f) {
				_fade = 0.f;
				// Don't reset the _fadeMode here. This way if fadeOut() was called
				// on a looping chore its keyframe animations will remain faded out
				// when it calls play() again.
				deactivate();
				return 0;
			}
		}
	} else {
		_fade = 1.f;
	}

	if (_time > animLength) { // What to do at end?
		switch (_repeatMode) {
			case Once:
				if (_fadeMode == None)
					deactivate();
				else
					_time = animLength;
				break;
			case Looping:
				_time = -1;
				break;
			case PauseAtEnd:
				_time = animLength;
				_paused = true;
				break;
			case FadeAtEnd:
				if (_fadeMode != FadeOut) {
					_fadeMode = FadeOut;
					_fadeLength = 250;
				}
				_time = animLength;
				break;
			default:
				Debug::warning(Debug::Keyframes, "Unknown repeat mode %d for keyframe %s", _repeatMode, _keyframe->getFilename().c_str());
		}
	}

	return marker;
}

void Animation::saveState(SaveGame *state) const {
	state->writeBool(_active);
	state->writeLESint32((int)_repeatMode);
	state->writeLESint32(_time);
	state->writeLESint32((int)_fadeMode);
	state->writeFloat(_fade);
	state->writeLESint32(_fadeLength);
	state->writeBool(_paused);
}

void Animation::restoreState(SaveGame *state) {
	bool active = state->readBool();
	_repeatMode = (RepeatMode)state->readLESint32();
	_time = state->readLESint32();
	_fadeMode = (FadeMode)state->readLESint32();
	_fade = state->readFloat();
	_fadeLength = state->readLESint32();
	_paused = state->readBool();

	if (active)
		activate();
}

/**
 * @class AnimManager
 */

AnimManager::AnimManager() {

}

AnimManager::~AnimManager() {
	foreach (const AnimationEntry &entry, _activeAnims) {
		Animation *anim = entry._anim;
		// Don't call deactivate() here so we don't mess with the list we're using.
		anim->_manager = nullptr;
		anim->_active = false;
	}
}

void AnimManager::addAnimation(Animation *anim, int priority1, int priority2) {
	// Keep the list of animations sorted by priorities in descending order. Because
	// the animations have two different priorities, we add the animation to the list
	// with both priorities.
	Common::List<AnimationEntry>::iterator i;
	AnimationEntry entry;
	entry._anim = anim;
	entry._priority = priority1;
	entry._tagged = false;
	for (i = _activeAnims.begin(); i != _activeAnims.end(); ++i) {
		if (i->_priority < entry._priority) {
			_activeAnims.insert(i, entry);
			break;
		}
	}
	if (i == _activeAnims.end())
		_activeAnims.push_back(entry);

	entry._priority = priority2;
	entry._tagged = true;
	for (i = _activeAnims.begin(); i != _activeAnims.end(); ++i) {
		if (i->_priority < entry._priority) {
			_activeAnims.insert(i, entry);
			break;
		}
	}
	if (i == _activeAnims.end())
		_activeAnims.push_back(entry);
}

void AnimManager::removeAnimation(const Animation *anim) {
	Common::List<AnimationEntry>::iterator i;
	for (i = _activeAnims.begin(); i != _activeAnims.end(); ++i) {
		if (i->_anim == anim) {
			i = _activeAnims.erase(i);
			--i;
		}
	}
}

void AnimManager::animate(ModelNode *hier, int numNodes) {
	// Apply animation to each hierarchy node separately.
	for (int i = 0; i < numNodes; i++) {
		float remainingWeight = 1.0f;
		int currPriority = -1;
		float layerWeight = 0.0f;

		// The animations are layered so that animations with a higher priority
		// are played regardless of the blend weights of lower priority animations.
		// The highest priority layer gets as much weight as it wants, while the
		// next layer gets the remaining amount and so on.
		for (Common::List<AnimationEntry>::iterator j = _activeAnims.begin(); j != _activeAnims.end(); ++j) {
			if (currPriority != j->_priority) {
				remainingWeight *= 1.0f - layerWeight;
				layerWeight = 0.0f;
				for (Common::List<AnimationEntry>::iterator k = j; k != _activeAnims.end(); ++k) {
					if (j->_priority != k->_priority)
						break;
					float time = k->_anim->_time / 1000.0f;
					if (k->_anim->_keyframe->isNodeAnimated(hier, i, time, k->_tagged))
						layerWeight += k->_anim->_fade;
				}

				currPriority = j->_priority;
				if (remainingWeight <= 0.0f)
					break;
			}

			float time = j->_anim->_time / 1000.0f;
			float weight = j->_anim->_fade;
			if (layerWeight > 1.0f)
				weight /= layerWeight;
			weight *= remainingWeight;
			j->_anim->_keyframe->animate(hier, i, time, weight, j->_tagged);
		}
	}
}

}