Animation.cpp 13 KB

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