Animation.cpp 12 KB

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