Animation.cpp 11 KB

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