Animation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <string.h>
  9. #include "Properties.h"
  10. #define ANIMATION_DEFAULT_CLIP_SUFFIX "__default__clip"
  11. #define ANIMATION_INDEFINITE_STR "INDEFINITE"
  12. #define ANIMATION_DEFAULT_CLIP 0
  13. #define ANIMATION_ROTATE_OFFSET 0
  14. #define ANIMATION_SRT_OFFSET 3
  15. namespace gameplay
  16. {
  17. Animation::Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type)
  18. : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0), _defaultClip(NULL), _clips(NULL)
  19. {
  20. createChannel(target, propertyId, keyCount, keyTimes, keyValues, type);
  21. }
  22. Animation::Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type)
  23. : _controller(Game::getInstance()->getAnimationController()), _id(id), _duration(0), _defaultClip(NULL), _clips(NULL)
  24. {
  25. createChannel(target, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
  26. }
  27. Animation::~Animation()
  28. {
  29. if (_clips != NULL)
  30. {
  31. std::vector<AnimationClip*>::iterator clipIter = _clips->begin();
  32. while (clipIter != _clips->end())
  33. {
  34. AnimationClip* clip = *clipIter;
  35. clip->stop();
  36. SAFE_RELEASE(clip);
  37. clipIter++;
  38. }
  39. _clips->clear();
  40. }
  41. SAFE_DELETE(_clips);
  42. SAFE_DELETE(_defaultClip);
  43. /*std::vector<Channel*>::iterator channelIter = _channels.begin();
  44. while (channelIter != _channels.end())
  45. {
  46. Animation::Channel* channel = *channelIter;
  47. channel->_target->removeChannel(channel);
  48. SAFE_RELEASE(channel);
  49. channelIter++;
  50. }*/
  51. _channels.clear();
  52. }
  53. Animation::Channel::Channel(Animation* animation, AnimationTarget* target, int propertyId, Curve* curve, unsigned long duration)
  54. : _animation(animation), _target(target), _propertyId(propertyId), _curve(curve), _duration(duration)
  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. _animation->addRef();
  59. _target->addChannel(this);
  60. }
  61. Animation::Channel::~Channel()
  62. {
  63. _animation->removeChannel(this);
  64. SAFE_RELEASE(_animation);
  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(this, 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(this, 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::removeChannel(Channel* channel)
  260. {
  261. std::vector<Animation::Channel*>::iterator itr = _channels.begin();
  262. while (itr != _channels.end())
  263. {
  264. Animation::Channel* chan = *itr;
  265. if (channel == chan)
  266. {
  267. _channels.erase(itr);
  268. itr = _channels.end();
  269. }
  270. else
  271. {
  272. itr++;
  273. }
  274. }
  275. if (_channels.empty())
  276. _controller->destroyAnimation(this);
  277. }
  278. void Animation::createDefaultClip()
  279. {
  280. std::string clipId = _id + ANIMATION_DEFAULT_CLIP_SUFFIX;
  281. _defaultClip = new AnimationClip(clipId.c_str(), this, 0.0f, _duration);
  282. }
  283. void Animation::addClip(AnimationClip* clip)
  284. {
  285. if (_clips == NULL)
  286. _clips = new std::vector<AnimationClip*>;
  287. _clips->push_back(clip);
  288. }
  289. AnimationClip* Animation::findClip(const char* id) const
  290. {
  291. if (_clips)
  292. {
  293. AnimationClip* clip = NULL;
  294. unsigned int clipCount = _clips->size();
  295. for (unsigned int i = 0; i < clipCount; i++)
  296. {
  297. clip = _clips->at(i);
  298. if (clip->_id.compare(id) == 0)
  299. {
  300. return _clips->at(i);
  301. }
  302. }
  303. }
  304. return NULL;
  305. }
  306. }