Animation.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include "Base.h"
  2. #include "Animation.h"
  3. #include "AnimationController.h"
  4. #include "AnimationClip.h"
  5. #include "AnimationTarget.h"
  6. #include "Game.h"
  7. #include "Transform.h"
  8. #include "Properties.h"
  9. #define ANIMATION_INDEFINITE_STR "INDEFINITE"
  10. #define ANIMATION_DEFAULT_CLIP 0
  11. #define ANIMATION_ROTATE_OFFSET 0
  12. #define ANIMATION_SRT_OFFSET 3
  13. namespace gameplay
  14. {
  15. Animation::Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned int* keyTimes, float* keyValues, unsigned int type)
  16. : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0L), _defaultClip(NULL), _clips(NULL)
  17. {
  18. createChannel(target, propertyId, keyCount, keyTimes, keyValues, type);
  19. // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
  20. release();
  21. GP_ASSERT(getRefCount() == 1);
  22. }
  23. Animation::Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned int* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type)
  24. : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0L), _defaultClip(NULL), _clips(NULL)
  25. {
  26. createChannel(target, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
  27. // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
  28. release();
  29. GP_ASSERT(getRefCount() == 1);
  30. }
  31. Animation::Animation(const char* id)
  32. : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0L), _defaultClip(NULL), _clips(NULL)
  33. {
  34. }
  35. Animation::~Animation()
  36. {
  37. _channels.clear();
  38. if (_defaultClip)
  39. {
  40. if (_defaultClip->isClipStateBitSet(AnimationClip::CLIP_IS_PLAYING_BIT))
  41. {
  42. GP_ASSERT(_controller);
  43. _controller->unschedule(_defaultClip);
  44. }
  45. SAFE_RELEASE(_defaultClip);
  46. }
  47. if (_clips)
  48. {
  49. std::vector<AnimationClip*>::iterator clipIter = _clips->begin();
  50. while (clipIter != _clips->end())
  51. {
  52. AnimationClip* clip = *clipIter;
  53. GP_ASSERT(clip);
  54. if (clip->isClipStateBitSet(AnimationClip::CLIP_IS_PLAYING_BIT))
  55. {
  56. GP_ASSERT(_controller);
  57. _controller->unschedule(clip);
  58. }
  59. SAFE_RELEASE(clip);
  60. clipIter++;
  61. }
  62. _clips->clear();
  63. }
  64. SAFE_DELETE(_clips);
  65. }
  66. Animation::Channel::Channel(Animation* animation, AnimationTarget* target, int propertyId, Curve* curve, unsigned long duration)
  67. : _animation(animation), _target(target), _propertyId(propertyId), _curve(curve), _duration(duration)
  68. {
  69. GP_ASSERT(_animation);
  70. GP_ASSERT(_target);
  71. GP_ASSERT(_curve);
  72. // get property component count, and ensure the property exists on the AnimationTarget by getting the property component count.
  73. GP_ASSERT(_target->getAnimationPropertyComponentCount(propertyId));
  74. _curve->addRef();
  75. _target->addChannel(this);
  76. _animation->addRef();
  77. }
  78. Animation::Channel::Channel(const Channel& copy, Animation* animation, AnimationTarget* target)
  79. : _animation(animation), _target(target), _propertyId(copy._propertyId), _curve(copy._curve), _duration(copy._duration)
  80. {
  81. GP_ASSERT(_curve);
  82. GP_ASSERT(_target);
  83. GP_ASSERT(_animation);
  84. _curve->addRef();
  85. _target->addChannel(this);
  86. _animation->addRef();
  87. }
  88. Animation::Channel::~Channel()
  89. {
  90. SAFE_RELEASE(_curve);
  91. SAFE_RELEASE(_animation);
  92. }
  93. Curve* Animation::Channel::getCurve() const
  94. {
  95. return _curve;
  96. }
  97. const char* Animation::getId() const
  98. {
  99. return _id.c_str();
  100. }
  101. unsigned long Animation::getDuration() const
  102. {
  103. return _duration;
  104. }
  105. void Animation::createClips(const char* url)
  106. {
  107. Properties* properties = Properties::create(url);
  108. GP_ASSERT(properties);
  109. Properties* pAnimation = (strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace();
  110. GP_ASSERT(pAnimation);
  111. int frameCount = pAnimation->getInt("frameCount");
  112. if (frameCount <= 0)
  113. GP_ERROR("The animation's frame count must be greater than 0.");
  114. createClips(pAnimation, (unsigned int)frameCount);
  115. SAFE_DELETE(properties);
  116. }
  117. AnimationClip* Animation::createClip(const char* id, unsigned long start, unsigned long end)
  118. {
  119. AnimationClip* clip = new AnimationClip(id, this, start, end);
  120. addClip(clip);
  121. return clip;
  122. }
  123. AnimationClip* Animation::getClip(const char* id)
  124. {
  125. // If id is NULL return the default clip.
  126. if (id == NULL)
  127. {
  128. if (_defaultClip == NULL)
  129. createDefaultClip();
  130. return _defaultClip;
  131. }
  132. else
  133. {
  134. return findClip(id);
  135. }
  136. }
  137. AnimationClip* Animation::getClip(unsigned int index) const
  138. {
  139. if (_clips)
  140. return _clips->at(index);
  141. return NULL;
  142. }
  143. unsigned int Animation::getClipCount() const
  144. {
  145. return _clips ? (unsigned int)_clips->size() : 0;
  146. }
  147. void Animation::play(const char* clipId)
  148. {
  149. // If id is NULL, play the default clip.
  150. if (clipId == NULL)
  151. {
  152. if (_defaultClip == NULL)
  153. createDefaultClip();
  154. _defaultClip->play();
  155. }
  156. else
  157. {
  158. // Find animation clip and play.
  159. AnimationClip* clip = findClip(clipId);
  160. if (clip != NULL)
  161. clip->play();
  162. }
  163. }
  164. void Animation::stop(const char* clipId)
  165. {
  166. // If id is NULL, play the default clip.
  167. if (clipId == NULL)
  168. {
  169. if (_defaultClip)
  170. _defaultClip->stop();
  171. }
  172. else
  173. {
  174. // Find animation clip and play.
  175. AnimationClip* clip = findClip(clipId);
  176. if (clip != NULL)
  177. clip->stop();
  178. }
  179. }
  180. void Animation::pause(const char * clipId)
  181. {
  182. if (clipId == NULL)
  183. {
  184. if (_defaultClip)
  185. _defaultClip->pause();
  186. }
  187. else
  188. {
  189. AnimationClip* clip = findClip(clipId);
  190. if (clip != NULL)
  191. clip->pause();
  192. }
  193. }
  194. bool Animation::targets(AnimationTarget* target) const
  195. {
  196. for (std::vector<Animation::Channel*>::const_iterator itr = _channels.begin(); itr != _channels.end(); ++itr)
  197. {
  198. GP_ASSERT(*itr);
  199. if ((*itr)->_target == target)
  200. {
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. void Animation::createDefaultClip()
  207. {
  208. _defaultClip = new AnimationClip("default_clip", this, 0.0f, _duration);
  209. }
  210. void Animation::createClips(Properties* animationProperties, unsigned int frameCount)
  211. {
  212. GP_ASSERT(animationProperties);
  213. Properties* pClip = animationProperties->getNextNamespace();
  214. while (pClip != NULL && std::strcmp(pClip->getNamespace(), "clip") == 0)
  215. {
  216. int begin = pClip->getInt("begin");
  217. int end = pClip->getInt("end");
  218. AnimationClip* clip = createClip(pClip->getId(), ((float)begin / (frameCount-1)) * _duration, ((float)end / (frameCount-1)) * _duration);
  219. const char* repeat = pClip->getString("repeatCount");
  220. if (repeat)
  221. {
  222. if (strcmp(repeat, ANIMATION_INDEFINITE_STR) == 0)
  223. {
  224. clip->setRepeatCount(AnimationClip::REPEAT_INDEFINITE);
  225. }
  226. else
  227. {
  228. float value;
  229. sscanf(repeat, "%f", &value);
  230. clip->setRepeatCount(value);
  231. }
  232. }
  233. const char* speed = pClip->getString("speed");
  234. if (speed)
  235. {
  236. float value;
  237. sscanf(speed, "%f", &value);
  238. clip->setSpeed(value);
  239. }
  240. clip->setLoopBlendTime(pClip->getFloat("loopBlendTime")); // returns zero if not specified
  241. pClip = animationProperties->getNextNamespace();
  242. }
  243. }
  244. void Animation::addClip(AnimationClip* clip)
  245. {
  246. if (_clips == NULL)
  247. _clips = new std::vector<AnimationClip*>;
  248. GP_ASSERT(clip);
  249. _clips->push_back(clip);
  250. }
  251. AnimationClip* Animation::findClip(const char* id) const
  252. {
  253. if (_clips)
  254. {
  255. AnimationClip* clip = NULL;
  256. size_t clipCount = _clips->size();
  257. for (size_t i = 0; i < clipCount; i++)
  258. {
  259. clip = _clips->at(i);
  260. GP_ASSERT(clip);
  261. if (clip->_id.compare(id) == 0)
  262. {
  263. return clip;
  264. }
  265. }
  266. }
  267. return NULL;
  268. }
  269. Animation::Channel* Animation::createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned int* keyTimes, float* keyValues, unsigned int type)
  270. {
  271. GP_ASSERT(target);
  272. GP_ASSERT(keyTimes);
  273. GP_ASSERT(keyValues);
  274. unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  275. GP_ASSERT(propertyComponentCount > 0);
  276. Curve* curve = Curve::create(keyCount, propertyComponentCount);
  277. GP_ASSERT(curve);
  278. if (target->_targetType == AnimationTarget::TRANSFORM)
  279. setTransformRotationOffset(curve, propertyId);
  280. unsigned int lowest = keyTimes[0];
  281. unsigned long duration = keyTimes[keyCount-1] - lowest;
  282. float* normalizedKeyTimes = new float[keyCount];
  283. normalizedKeyTimes[0] = 0.0f;
  284. curve->setPoint(0, normalizedKeyTimes[0], keyValues, (Curve::InterpolationType) type);
  285. unsigned int pointOffset = propertyComponentCount;
  286. unsigned int i = 1;
  287. for (; i < keyCount - 1; i++)
  288. {
  289. normalizedKeyTimes[i] = (float) (keyTimes[i] - lowest) / (float) duration;
  290. curve->setPoint(i, normalizedKeyTimes[i], (keyValues + pointOffset), (Curve::InterpolationType) type);
  291. pointOffset += propertyComponentCount;
  292. }
  293. i = keyCount - 1;
  294. normalizedKeyTimes[i] = 1.0f;
  295. curve->setPoint(i, normalizedKeyTimes[i], keyValues + pointOffset, (Curve::InterpolationType) type);
  296. SAFE_DELETE_ARRAY(normalizedKeyTimes);
  297. Channel* channel = new Channel(this, target, propertyId, curve, duration);
  298. curve->release();
  299. addChannel(channel);
  300. return channel;
  301. }
  302. Animation::Channel* Animation::createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned int* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type)
  303. {
  304. GP_ASSERT(target);
  305. GP_ASSERT(keyTimes);
  306. GP_ASSERT(keyValues);
  307. unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  308. GP_ASSERT(propertyComponentCount > 0);
  309. Curve* curve = Curve::create(keyCount, propertyComponentCount);
  310. GP_ASSERT(curve);
  311. if (target->_targetType == AnimationTarget::TRANSFORM)
  312. setTransformRotationOffset(curve, propertyId);
  313. unsigned long lowest = keyTimes[0];
  314. unsigned long duration = keyTimes[keyCount-1] - lowest;
  315. float* normalizedKeyTimes = new float[keyCount];
  316. normalizedKeyTimes[0] = 0.0f;
  317. curve->setPoint(0, normalizedKeyTimes[0], keyValues, (Curve::InterpolationType) type, keyInValue, keyOutValue);
  318. unsigned int pointOffset = propertyComponentCount;
  319. unsigned int i = 1;
  320. for (; i < keyCount - 1; i++)
  321. {
  322. normalizedKeyTimes[i] = (float) (keyTimes[i] - lowest) / (float) duration;
  323. curve->setPoint(i, normalizedKeyTimes[i], (keyValues + pointOffset), (Curve::InterpolationType) type, (keyInValue + pointOffset), (keyOutValue + pointOffset));
  324. pointOffset += propertyComponentCount;
  325. }
  326. i = keyCount - 1;
  327. normalizedKeyTimes[i] = 1.0f;
  328. curve->setPoint(i, normalizedKeyTimes[i], keyValues + pointOffset, (Curve::InterpolationType) type, keyInValue + pointOffset, keyOutValue + pointOffset);
  329. SAFE_DELETE_ARRAY(normalizedKeyTimes);
  330. Channel* channel = new Channel(this, target, propertyId, curve, duration);
  331. curve->release();
  332. addChannel(channel);
  333. return channel;
  334. }
  335. void Animation::addChannel(Channel* channel)
  336. {
  337. GP_ASSERT(channel);
  338. _channels.push_back(channel);
  339. if (channel->_duration > _duration)
  340. _duration = channel->_duration;
  341. }
  342. void Animation::removeChannel(Channel* channel)
  343. {
  344. std::vector<Animation::Channel*>::iterator itr = _channels.begin();
  345. while (itr != _channels.end())
  346. {
  347. Animation::Channel* chan = *itr;
  348. if (channel == chan)
  349. {
  350. _channels.erase(itr);
  351. return;
  352. }
  353. else
  354. {
  355. itr++;
  356. }
  357. }
  358. }
  359. void Animation::setTransformRotationOffset(Curve* curve, unsigned int propertyId)
  360. {
  361. GP_ASSERT(curve);
  362. switch (propertyId)
  363. {
  364. case Transform::ANIMATE_ROTATE:
  365. case Transform::ANIMATE_ROTATE_TRANSLATE:
  366. curve->setQuaternionOffset(ANIMATION_ROTATE_OFFSET);
  367. return;
  368. case Transform::ANIMATE_SCALE_ROTATE_TRANSLATE:
  369. curve->setQuaternionOffset(ANIMATION_SRT_OFFSET);
  370. return;
  371. }
  372. return;
  373. }
  374. Animation* Animation::clone(Channel* channel, AnimationTarget* target)
  375. {
  376. GP_ASSERT(channel);
  377. Animation* animation = new Animation(getId());
  378. Animation::Channel* channelCopy = new Animation::Channel(*channel, animation, target);
  379. animation->addChannel(channelCopy);
  380. // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
  381. animation->release();
  382. GP_ASSERT(animation->getRefCount() == 1);
  383. // Clone the clips
  384. if (_defaultClip)
  385. {
  386. animation->_defaultClip = _defaultClip->clone(animation);
  387. }
  388. if (_clips)
  389. {
  390. for (std::vector<AnimationClip*>::iterator it = _clips->begin(); it != _clips->end(); ++it)
  391. {
  392. AnimationClip* newClip = (*it)->clone(animation);
  393. animation->addClip(newClip);
  394. }
  395. }
  396. return animation;
  397. }
  398. }