Animation.cpp 9.9 KB

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