Animation.cpp 11 KB

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