Animation.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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) * _duration, ((float) end / frameCount) * _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. pClip = animationProperties->getNextNamespace();
  241. }
  242. }
  243. void Animation::addClip(AnimationClip* clip)
  244. {
  245. if (_clips == NULL)
  246. _clips = new std::vector<AnimationClip*>;
  247. GP_ASSERT(clip);
  248. _clips->push_back(clip);
  249. }
  250. AnimationClip* Animation::findClip(const char* id) const
  251. {
  252. if (_clips)
  253. {
  254. AnimationClip* clip = NULL;
  255. size_t clipCount = _clips->size();
  256. for (size_t i = 0; i < clipCount; i++)
  257. {
  258. clip = _clips->at(i);
  259. GP_ASSERT(clip);
  260. if (clip->_id.compare(id) == 0)
  261. {
  262. return clip;
  263. }
  264. }
  265. }
  266. return NULL;
  267. }
  268. Animation::Channel* Animation::createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned int* keyTimes, float* keyValues, unsigned int type)
  269. {
  270. GP_ASSERT(target);
  271. GP_ASSERT(keyTimes);
  272. GP_ASSERT(keyValues);
  273. unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  274. GP_ASSERT(propertyComponentCount > 0);
  275. Curve* curve = Curve::create(keyCount, propertyComponentCount);
  276. GP_ASSERT(curve);
  277. if (target->_targetType == AnimationTarget::TRANSFORM)
  278. setTransformRotationOffset(curve, propertyId);
  279. unsigned int lowest = keyTimes[0];
  280. unsigned long duration = keyTimes[keyCount-1] - lowest;
  281. float* normalizedKeyTimes = new float[keyCount];
  282. normalizedKeyTimes[0] = 0.0f;
  283. curve->setPoint(0, normalizedKeyTimes[0], keyValues, (Curve::InterpolationType) type);
  284. unsigned int pointOffset = propertyComponentCount;
  285. unsigned int i = 1;
  286. for (; i < keyCount - 1; i++)
  287. {
  288. normalizedKeyTimes[i] = (float) (keyTimes[i] - lowest) / (float) duration;
  289. curve->setPoint(i, normalizedKeyTimes[i], (keyValues + pointOffset), (Curve::InterpolationType) type);
  290. pointOffset += propertyComponentCount;
  291. }
  292. i = keyCount - 1;
  293. normalizedKeyTimes[i] = 1.0f;
  294. curve->setPoint(i, normalizedKeyTimes[i], keyValues + pointOffset, (Curve::InterpolationType) type);
  295. SAFE_DELETE_ARRAY(normalizedKeyTimes);
  296. Channel* channel = new Channel(this, target, propertyId, curve, duration);
  297. curve->release();
  298. addChannel(channel);
  299. return channel;
  300. }
  301. Animation::Channel* Animation::createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned int* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type)
  302. {
  303. GP_ASSERT(target);
  304. GP_ASSERT(keyTimes);
  305. GP_ASSERT(keyValues);
  306. unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  307. GP_ASSERT(propertyComponentCount > 0);
  308. Curve* curve = Curve::create(keyCount, propertyComponentCount);
  309. GP_ASSERT(curve);
  310. if (target->_targetType == AnimationTarget::TRANSFORM)
  311. setTransformRotationOffset(curve, propertyId);
  312. unsigned long lowest = keyTimes[0];
  313. unsigned long duration = keyTimes[keyCount-1] - lowest;
  314. float* normalizedKeyTimes = new float[keyCount];
  315. normalizedKeyTimes[0] = 0.0f;
  316. curve->setPoint(0, normalizedKeyTimes[0], keyValues, (Curve::InterpolationType) type, keyInValue, keyOutValue);
  317. unsigned int pointOffset = propertyComponentCount;
  318. unsigned int i = 1;
  319. for (; i < keyCount - 1; i++)
  320. {
  321. normalizedKeyTimes[i] = (float) (keyTimes[i] - lowest) / (float) duration;
  322. curve->setPoint(i, normalizedKeyTimes[i], (keyValues + pointOffset), (Curve::InterpolationType) type, (keyInValue + pointOffset), (keyOutValue + pointOffset));
  323. pointOffset += propertyComponentCount;
  324. }
  325. i = keyCount - 1;
  326. normalizedKeyTimes[i] = 1.0f;
  327. curve->setPoint(i, normalizedKeyTimes[i], keyValues + pointOffset, (Curve::InterpolationType) type, keyInValue + pointOffset, keyOutValue + pointOffset);
  328. SAFE_DELETE_ARRAY(normalizedKeyTimes);
  329. Channel* channel = new Channel(this, target, propertyId, curve, duration);
  330. curve->release();
  331. addChannel(channel);
  332. return channel;
  333. }
  334. void Animation::addChannel(Channel* channel)
  335. {
  336. GP_ASSERT(channel);
  337. _channels.push_back(channel);
  338. if (channel->_duration > _duration)
  339. _duration = channel->_duration;
  340. }
  341. void Animation::removeChannel(Channel* channel)
  342. {
  343. std::vector<Animation::Channel*>::iterator itr = _channels.begin();
  344. while (itr != _channels.end())
  345. {
  346. Animation::Channel* chan = *itr;
  347. if (channel == chan)
  348. {
  349. _channels.erase(itr);
  350. return;
  351. }
  352. else
  353. {
  354. itr++;
  355. }
  356. }
  357. }
  358. void Animation::setTransformRotationOffset(Curve* curve, unsigned int propertyId)
  359. {
  360. GP_ASSERT(curve);
  361. switch (propertyId)
  362. {
  363. case Transform::ANIMATE_ROTATE:
  364. case Transform::ANIMATE_ROTATE_TRANSLATE:
  365. curve->setQuaternionOffset(ANIMATION_ROTATE_OFFSET);
  366. return;
  367. case Transform::ANIMATE_SCALE_ROTATE_TRANSLATE:
  368. curve->setQuaternionOffset(ANIMATION_SRT_OFFSET);
  369. return;
  370. }
  371. return;
  372. }
  373. Animation* Animation::clone(Channel* channel, AnimationTarget* target)
  374. {
  375. GP_ASSERT(channel);
  376. Animation* animation = new Animation(getId());
  377. Animation::Channel* channelCopy = new Animation::Channel(*channel, animation, target);
  378. animation->addChannel(channelCopy);
  379. // Release the animation because a newly created animation has a ref count of 1 and the channels hold the ref to animation.
  380. animation->release();
  381. GP_ASSERT(animation->getRefCount() == 1);
  382. // Clone the clips
  383. if (_clips)
  384. {
  385. for (std::vector<AnimationClip*>::iterator it = _clips->begin(); it != _clips->end(); ++it)
  386. {
  387. AnimationClip* newClip = (*it)->clone(animation);
  388. animation->addClip(newClip);
  389. }
  390. }
  391. return animation;
  392. }
  393. }