2
0

AnimationClip.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. #include "Base.h"
  2. #include "AnimationClip.h"
  3. #include "Animation.h"
  4. #include "AnimationTarget.h"
  5. #include "Game.h"
  6. #include "Quaternion.h"
  7. namespace gameplay
  8. {
  9. AnimationClip::AnimationClip(const char* id, Animation* animation, unsigned long startTime, unsigned long endTime)
  10. : _id(id), _animation(animation), _startTime(startTime), _endTime(endTime), _duration(_endTime - _startTime),
  11. _stateBits(0x00), _repeatCount(1.0f), _activeDuration(_duration * _repeatCount), _speed(1.0f), _timeStarted(0),
  12. _elapsedTime(0), _crossFadeToClip(NULL), _crossFadeOutElapsed(0), _crossFadeOutDuration(0), _blendWeight(1.0f),
  13. _beginListeners(NULL), _endListeners(NULL), _listeners(NULL), _listenerItr(NULL)
  14. {
  15. GP_ASSERT(_animation);
  16. GP_ASSERT(0 <= startTime && startTime <= _animation->_duration && 0 <= endTime && endTime <= _animation->_duration);
  17. unsigned int channelCount = _animation->_channels.size();
  18. for (unsigned int i = 0; i < channelCount; i++)
  19. {
  20. GP_ASSERT(_animation->_channels[i]);
  21. GP_ASSERT(_animation->_channels[i]->getCurve());
  22. _values.push_back(new AnimationValue(_animation->_channels[i]->getCurve()->getComponentCount()));
  23. }
  24. }
  25. AnimationClip::~AnimationClip()
  26. {
  27. std::vector<AnimationValue*>::iterator valueIter = _values.begin();
  28. while (valueIter != _values.end())
  29. {
  30. SAFE_DELETE(*valueIter);
  31. valueIter++;
  32. }
  33. _values.clear();
  34. SAFE_RELEASE(_crossFadeToClip);
  35. SAFE_DELETE(_beginListeners);
  36. SAFE_DELETE(_endListeners);
  37. if (_listeners)
  38. {
  39. *_listenerItr = _listeners->begin();
  40. while (*_listenerItr != _listeners->end())
  41. {
  42. ListenerEvent* lEvt = **_listenerItr;
  43. SAFE_DELETE(lEvt);
  44. ++*_listenerItr;
  45. }
  46. SAFE_DELETE(_listeners);
  47. }
  48. SAFE_DELETE(_listenerItr);
  49. }
  50. AnimationClip::ListenerEvent::ListenerEvent(Listener* listener, unsigned long eventTime)
  51. {
  52. _listener = listener;
  53. _eventTime = eventTime;
  54. }
  55. AnimationClip::ListenerEvent::~ListenerEvent()
  56. {
  57. }
  58. const char* AnimationClip::getID() const
  59. {
  60. return _id.c_str();
  61. }
  62. Animation* AnimationClip::getAnimation() const
  63. {
  64. return _animation;
  65. }
  66. unsigned long AnimationClip::getStartTime() const
  67. {
  68. return _startTime;
  69. }
  70. unsigned long AnimationClip::getEndTime() const
  71. {
  72. return _endTime;
  73. }
  74. unsigned long AnimationClip::getElaspedTime() const
  75. {
  76. return _elapsedTime;
  77. }
  78. void AnimationClip::setRepeatCount(float repeatCount)
  79. {
  80. GP_ASSERT(repeatCount == REPEAT_INDEFINITE || repeatCount > 0.0f);
  81. _repeatCount = repeatCount;
  82. if (repeatCount == REPEAT_INDEFINITE)
  83. {
  84. _activeDuration = _duration;
  85. }
  86. else
  87. {
  88. _activeDuration = _repeatCount * _duration;
  89. }
  90. }
  91. float AnimationClip::getRepeatCount() const
  92. {
  93. return _repeatCount;
  94. }
  95. void AnimationClip::setActiveDuration(unsigned long duration)
  96. {
  97. if (duration == REPEAT_INDEFINITE)
  98. {
  99. _repeatCount = REPEAT_INDEFINITE;
  100. _activeDuration = _duration;
  101. }
  102. else
  103. {
  104. _activeDuration = _duration;
  105. _repeatCount = (float) _activeDuration / (float) _duration;
  106. }
  107. }
  108. unsigned long AnimationClip::getActiveDuration() const
  109. {
  110. if (_repeatCount == REPEAT_INDEFINITE)
  111. return REPEAT_INDEFINITE;
  112. return _activeDuration;
  113. }
  114. unsigned long AnimationClip::getDuration() const
  115. {
  116. return _duration;
  117. }
  118. void AnimationClip::setSpeed(float speed)
  119. {
  120. _speed = speed;
  121. }
  122. float AnimationClip::getSpeed() const
  123. {
  124. return _speed;
  125. }
  126. void AnimationClip::setBlendWeight(float blendWeight)
  127. {
  128. _blendWeight = blendWeight;
  129. }
  130. float AnimationClip::getBlendWeight() const
  131. {
  132. return _blendWeight;
  133. }
  134. bool AnimationClip::isPlaying() const
  135. {
  136. return isClipStateBitSet(CLIP_IS_PLAYING_BIT);
  137. }
  138. void AnimationClip::play()
  139. {
  140. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  141. {
  142. // If paused, reset the bit and return.
  143. if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
  144. {
  145. resetClipStateBit(CLIP_IS_PAUSED_BIT);
  146. return;
  147. }
  148. // If the clip is set to be removed, reset the flag.
  149. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  150. resetClipStateBit(CLIP_IS_MARKED_FOR_REMOVAL_BIT);
  151. // Set the state bit to restart.
  152. setClipStateBit(CLIP_IS_RESTARTED_BIT);
  153. }
  154. else
  155. {
  156. setClipStateBit(CLIP_IS_PLAYING_BIT);
  157. GP_ASSERT(_animation);
  158. GP_ASSERT(_animation->_controller);
  159. _animation->_controller->schedule(this);
  160. }
  161. _timeStarted = Game::getGameTime();
  162. }
  163. void AnimationClip::stop()
  164. {
  165. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  166. {
  167. // Reset the restarted and paused bits.
  168. resetClipStateBit(CLIP_IS_RESTARTED_BIT);
  169. resetClipStateBit(CLIP_IS_PAUSED_BIT);
  170. // Mark the clip to removed from the AnimationController.
  171. setClipStateBit(CLIP_IS_MARKED_FOR_REMOVAL_BIT);
  172. }
  173. }
  174. void AnimationClip::pause()
  175. {
  176. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT) && !isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  177. {
  178. setClipStateBit(CLIP_IS_PAUSED_BIT);
  179. }
  180. }
  181. void AnimationClip::crossFade(AnimationClip* clip, unsigned long duration)
  182. {
  183. GP_ASSERT(clip);
  184. // Check if the given clip is fading into this clip.
  185. // We should reset the clip from fading out, and this one from fading in
  186. // in order to start the crossfade back the other way.
  187. if (clip->isClipStateBitSet(CLIP_IS_FADING_OUT_BIT) && clip->_crossFadeToClip == this)
  188. {
  189. clip->resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  190. clip->_crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  191. SAFE_RELEASE(clip->_crossFadeToClip);
  192. }
  193. // If I already have a clip I'm fading to and it's not the same as the given clip release it.
  194. // Assign the new clip and increase it's ref count.
  195. if (_crossFadeToClip)
  196. {
  197. SAFE_RELEASE(_crossFadeToClip);
  198. }
  199. // Set and initialize the crossfade clip
  200. _crossFadeToClip = clip;
  201. _crossFadeToClip->addRef();
  202. _crossFadeToClip->setClipStateBit(CLIP_IS_FADING_IN_BIT);
  203. _crossFadeToClip->_blendWeight = 0.0f;
  204. // Set and intiliaze this clip to fade out
  205. setClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  206. setClipStateBit(CLIP_IS_FADING_OUT_BIT);
  207. _crossFadeOutElapsed = 0L;
  208. _crossFadeOutDuration = duration;
  209. // If this clip is currently not playing, we should start playing it.
  210. if (!isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  211. play();
  212. // Start playing the cross fade clip.
  213. _crossFadeToClip->play();
  214. }
  215. void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long eventTime)
  216. {
  217. GP_ASSERT(listener);
  218. GP_ASSERT(eventTime < _activeDuration);
  219. ListenerEvent* listenerEvent = new ListenerEvent(listener, eventTime);
  220. if (!_listeners)
  221. {
  222. _listeners = new std::list<ListenerEvent*>;
  223. _listeners->push_front(listenerEvent);
  224. _listenerItr = new std::list<ListenerEvent*>::iterator;
  225. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  226. *_listenerItr = _listeners->begin();
  227. }
  228. else
  229. {
  230. for (std::list<ListenerEvent*>::iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
  231. {
  232. GP_ASSERT(*itr);
  233. if (eventTime < (*itr)->_eventTime)
  234. {
  235. itr = _listeners->insert(itr, listenerEvent);
  236. // If playing, update the iterator if we need to.
  237. // otherwise, it will just be set the next time the clip gets played.
  238. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  239. {
  240. unsigned long currentTime = _elapsedTime % _duration;
  241. GP_ASSERT(**_listenerItr || *_listenerItr == _listeners->end());
  242. if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_eventTime)) ||
  243. (_speed <= 0 && currentTime > eventTime && (*_listenerItr == _listeners->begin() || eventTime > (**_listenerItr)->_eventTime)))
  244. *_listenerItr = itr;
  245. }
  246. return;
  247. }
  248. }
  249. _listeners->push_back(listenerEvent);
  250. }
  251. }
  252. void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
  253. {
  254. if (!_beginListeners)
  255. _beginListeners = new std::vector<Listener*>;
  256. GP_ASSERT(listener);
  257. _beginListeners->push_back(listener);
  258. }
  259. void AnimationClip::addEndListener(AnimationClip::Listener* listener)
  260. {
  261. if (!_endListeners)
  262. _endListeners = new std::vector<Listener*>;
  263. GP_ASSERT(listener);
  264. _endListeners->push_back(listener);
  265. }
  266. bool AnimationClip::update(unsigned long elapsedTime)
  267. {
  268. if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
  269. {
  270. return false;
  271. }
  272. else if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  273. { // If the marked for removal bit is set, it means stop() was called on the AnimationClip at some point
  274. // after the last update call. Reset the flag, and return true so the AnimationClip is removed from the
  275. // running clips on the AnimationController.
  276. onEnd();
  277. return true;
  278. }
  279. else if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  280. {
  281. onBegin();
  282. }
  283. else
  284. {
  285. _elapsedTime += elapsedTime * _speed;
  286. if (_repeatCount == REPEAT_INDEFINITE && _elapsedTime <= 0)
  287. _elapsedTime = _activeDuration + _elapsedTime;
  288. }
  289. unsigned long currentTime = 0L;
  290. // Check to see if clip is complete.
  291. if (_repeatCount != REPEAT_INDEFINITE && ((_speed >= 0.0f && _elapsedTime >= (long) _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0L)))
  292. {
  293. resetClipStateBit(CLIP_IS_STARTED_BIT);
  294. if (_speed >= 0.0f)
  295. {
  296. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  297. if (_duration == 0)
  298. {
  299. currentTime = 0L;
  300. }
  301. else
  302. {
  303. currentTime = _activeDuration % _duration; // Get's the fractional part of the final repeat.
  304. if (currentTime == 0L)
  305. currentTime = _duration;
  306. }
  307. }
  308. else
  309. {
  310. currentTime = 0L; // If we are negative speed, the end value should be 0.
  311. }
  312. }
  313. else
  314. {
  315. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  316. if (_duration == 0)
  317. currentTime = 0L;
  318. else // Gets portion/fraction of the repeat.
  319. currentTime = _elapsedTime % _duration;
  320. }
  321. // Notify any listeners of Animation events.
  322. if (_listeners)
  323. {
  324. GP_ASSERT(_listenerItr);
  325. if (_speed >= 0.0f)
  326. {
  327. while (*_listenerItr != _listeners->end() && _elapsedTime >= (long) (**_listenerItr)->_eventTime)
  328. {
  329. GP_ASSERT(_listenerItr);
  330. GP_ASSERT(**_listenerItr);
  331. GP_ASSERT((**_listenerItr)->_listener);
  332. (**_listenerItr)->_listener->animationEvent(this, Listener::DEFAULT);
  333. ++*_listenerItr;
  334. }
  335. }
  336. else
  337. {
  338. while (*_listenerItr != _listeners->begin() && _elapsedTime <= (long) (**_listenerItr)->_eventTime)
  339. {
  340. GP_ASSERT(_listenerItr);
  341. GP_ASSERT(**_listenerItr);
  342. GP_ASSERT((**_listenerItr)->_listener);
  343. (**_listenerItr)->_listener->animationEvent(this, Listener::DEFAULT);
  344. --*_listenerItr;
  345. }
  346. }
  347. }
  348. // Add back in start time, and divide by the total animation's duration to get the actual percentage complete
  349. GP_ASSERT(_animation);
  350. GP_ASSERT(_animation->_duration > 0);
  351. float percentComplete = (float)(_startTime + currentTime) / (float) _animation->_duration;
  352. if (isClipStateBitSet(CLIP_IS_FADING_OUT_BIT))
  353. {
  354. GP_ASSERT(_crossFadeToClip);
  355. GP_ASSERT(_crossFadeOutDuration > 0);
  356. if (isClipStateBitSet(CLIP_IS_FADING_OUT_STARTED_BIT)) // Calculate elapsed time since the fade out begin.
  357. {
  358. GP_ASSERT(_crossFadeToClip);
  359. _crossFadeOutElapsed = (Game::getGameTime() - _crossFadeToClip->_timeStarted) * fabs(_speed);
  360. resetClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  361. }
  362. else
  363. {
  364. // continue tracking elapsed time.
  365. _crossFadeOutElapsed += elapsedTime * fabs(_speed);
  366. }
  367. if (_crossFadeOutElapsed < _crossFadeOutDuration)
  368. {
  369. // Calculate this clip's blend weight.
  370. float tempBlendWeight = (float) (_crossFadeOutDuration - _crossFadeOutElapsed) / (float) _crossFadeOutDuration;
  371. // If this clip is fading in, adjust the crossfade clip's weight to be a percentage of your current blend weight
  372. if (isClipStateBitSet(CLIP_IS_FADING_IN_BIT))
  373. {
  374. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight) * _blendWeight;
  375. _blendWeight -= _crossFadeToClip->_blendWeight;
  376. }
  377. else
  378. {
  379. // Just set the blend weight.
  380. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight);
  381. _blendWeight = tempBlendWeight;
  382. }
  383. }
  384. else
  385. { // Fade is done.
  386. _crossFadeToClip->_blendWeight = 1.0f;
  387. _blendWeight = 0.0f;
  388. resetClipStateBit(CLIP_IS_STARTED_BIT);
  389. resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  390. _crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  391. SAFE_RELEASE(_crossFadeToClip);
  392. }
  393. }
  394. // Evaluate this clip.
  395. Animation::Channel* channel = NULL;
  396. AnimationValue* value = NULL;
  397. AnimationTarget* target = NULL;
  398. unsigned int channelCount = _animation->_channels.size();
  399. for (unsigned int i = 0; i < channelCount; i++)
  400. {
  401. channel = _animation->_channels[i];
  402. GP_ASSERT(channel);
  403. target = channel->_target;
  404. GP_ASSERT(target);
  405. value = _values[i];
  406. GP_ASSERT(value);
  407. // Evaluate the point on Curve
  408. GP_ASSERT(channel->getCurve());
  409. channel->getCurve()->evaluate(percentComplete, value->_value);
  410. // Set the animation value on the target property.
  411. target->setAnimationPropertyValue(channel->_propertyId, value, _blendWeight);
  412. }
  413. // When ended. Probably should move to it's own method so we can call it when the clip is ended early.
  414. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT) || !isClipStateBitSet(CLIP_IS_STARTED_BIT))
  415. {
  416. onEnd();
  417. return true;
  418. }
  419. return false;
  420. }
  421. void AnimationClip::onBegin()
  422. {
  423. // Initialize animation to play.
  424. setClipStateBit(CLIP_IS_STARTED_BIT);
  425. if (_speed >= 0)
  426. {
  427. _elapsedTime = (Game::getGameTime() - _timeStarted) * _speed;
  428. if (_listeners)
  429. *_listenerItr = _listeners->begin();
  430. }
  431. else
  432. {
  433. _elapsedTime = _activeDuration + (Game::getGameTime() - _timeStarted) * _speed;
  434. if (_listeners)
  435. *_listenerItr = _listeners->end();
  436. }
  437. // Notify begin listeners if any.
  438. if (_beginListeners)
  439. {
  440. std::vector<Listener*>::iterator listener = _beginListeners->begin();
  441. while (listener != _beginListeners->end())
  442. {
  443. GP_ASSERT(*listener);
  444. (*listener)->animationEvent(this, Listener::BEGIN);
  445. listener++;
  446. }
  447. }
  448. }
  449. void AnimationClip::onEnd()
  450. {
  451. _blendWeight = 1.0f;
  452. resetClipStateBit(CLIP_ALL_BITS);
  453. // Notify end listeners if any.
  454. if (_endListeners)
  455. {
  456. std::vector<Listener*>::iterator listener = _endListeners->begin();
  457. while (listener != _endListeners->end())
  458. {
  459. GP_ASSERT(*listener);
  460. (*listener)->animationEvent(this, Listener::END);
  461. listener++;
  462. }
  463. }
  464. }
  465. bool AnimationClip::isClipStateBitSet(unsigned char bit) const
  466. {
  467. return (_stateBits & bit) == bit;
  468. }
  469. void AnimationClip::setClipStateBit(unsigned char bit)
  470. {
  471. _stateBits |= bit;
  472. }
  473. void AnimationClip::resetClipStateBit(unsigned char bit)
  474. {
  475. _stateBits &= ~bit;
  476. }
  477. }