Animation.cpp 9.4 KB

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