AnimationController.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "Base.h"
  2. #include "AnimationController.h"
  3. #include "Game.h"
  4. #include "Curve.h"
  5. namespace gameplay
  6. {
  7. AnimationController::AnimationController()
  8. : _state(STOPPED), _animations(NULL)
  9. {
  10. }
  11. AnimationController::~AnimationController()
  12. {
  13. std::vector<Animation*>::iterator itr = _animations.begin();
  14. for ( ; itr != _animations.end(); itr++)
  15. {
  16. Animation* temp = *itr;
  17. SAFE_RELEASE(temp);
  18. }
  19. _animations.clear();
  20. }
  21. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, Curve::InterpolationType type)
  22. {
  23. assert(type != Curve::BEZIER && type != Curve::HERMITE);
  24. assert(keyCount >= 2 && keyTimes && keyValues && target);
  25. Animation* animation = new Animation(id, target, propertyId, keyCount, keyTimes, keyValues, type);
  26. addAnimation(animation);
  27. return animation;
  28. }
  29. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, Curve::InterpolationType type)
  30. {
  31. assert(target && keyCount >= 2 && keyTimes && keyValues && keyInValue && keyOutValue);
  32. Animation* animation = new Animation(id, target, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
  33. addAnimation(animation);
  34. return animation;
  35. }
  36. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, const char* animationFile)
  37. {
  38. assert(target && animationFile);
  39. Properties* p = Properties::create(animationFile);
  40. assert(p);
  41. Animation* animation = createAnimation(id, target, p->getNextNamespace());
  42. SAFE_DELETE(p);
  43. return animation;
  44. }
  45. Animation* AnimationController::createAnimationFromTo(const char* id, AnimationTarget* target, int propertyId, float* from, float* to, Curve::InterpolationType type, unsigned long duration)
  46. {
  47. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  48. float* keyValues = new float[2 * propertyComponentCount];
  49. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  50. memcpy(keyValues + propertyComponentCount, to, sizeof(float) * propertyComponentCount);
  51. unsigned long* keyTimes = new unsigned long[2];
  52. keyTimes[0] = 0;
  53. keyTimes[1] = duration;
  54. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  55. SAFE_DELETE_ARRAY(keyValues);
  56. SAFE_DELETE_ARRAY(keyTimes);
  57. return animation;
  58. }
  59. Animation* AnimationController::createAnimationFromBy(const char* id, AnimationTarget* target, int propertyId, float* from, float* by, Curve::InterpolationType type, unsigned long duration)
  60. {
  61. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  62. float* keyValues = new float[2 * propertyComponentCount];
  63. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  64. memcpy(keyValues + propertyComponentCount, by, sizeof(float) * propertyComponentCount);
  65. unsigned long* keyTimes = new unsigned long[2];
  66. keyTimes[0] = 0;
  67. keyTimes[1] = duration;
  68. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  69. SAFE_DELETE_ARRAY(keyValues);
  70. SAFE_DELETE_ARRAY(keyTimes);
  71. return animation;
  72. }
  73. Animation* AnimationController::getAnimation(const char* id) const
  74. {
  75. unsigned int animationCount = _animations.size();
  76. for (unsigned int i = 0; i < animationCount; i++)
  77. {
  78. if (_animations.at(i)->_id.compare(id) == 0)
  79. {
  80. return _animations.at(i);
  81. }
  82. }
  83. return NULL;
  84. }
  85. Animation* AnimationController::getAnimation(AnimationTarget* target) const
  86. {
  87. if (!target)
  88. return NULL;
  89. const unsigned int animationCount = _animations.size();
  90. for (unsigned int i = 0; i < animationCount; ++i)
  91. {
  92. Animation* animation = _animations[i];
  93. if (animation->targets(target))
  94. {
  95. return animation;
  96. }
  97. }
  98. return NULL;
  99. }
  100. void AnimationController::stopAllAnimations()
  101. {
  102. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  103. while (clipIter != _runningClips.end())
  104. {
  105. AnimationClip* clip = *clipIter;
  106. clip->stop();
  107. clipIter++;
  108. }
  109. }
  110. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, Properties* animationProperties)
  111. {
  112. assert(target && animationProperties);
  113. assert(std::strcmp(animationProperties->getNamespace(), "animation") == 0);
  114. const char* propertyIdStr = animationProperties->getString("property");
  115. assert(propertyIdStr);
  116. // Get animation target property id
  117. int propertyId = AnimationTarget::getPropertyId(target->_targetType, propertyIdStr);
  118. assert(propertyId != -1);
  119. unsigned int keyCount = animationProperties->getInt("keyCount");
  120. assert(keyCount > 0);
  121. const char* keyTimesStr = animationProperties->getString("keyTimes");
  122. assert(keyTimesStr);
  123. const char* keyValuesStr = animationProperties->getString("keyValues");
  124. assert(keyValuesStr);
  125. const char* curveStr = animationProperties->getString("curve");
  126. assert(curveStr);
  127. char delimeter = ' ';
  128. unsigned int startOffset = 0;
  129. unsigned int endOffset = (unsigned int)std::string::npos;
  130. unsigned long* keyTimes = new unsigned long[keyCount];
  131. for (unsigned int i = 0; i < keyCount; i++)
  132. {
  133. endOffset = static_cast<std::string>(keyTimesStr).find_first_of(delimeter, startOffset);
  134. if (endOffset != std::string::npos)
  135. {
  136. keyTimes[i] = std::strtoul(static_cast<std::string>(keyTimesStr).substr(startOffset, endOffset - startOffset).c_str(), NULL, 0);
  137. }
  138. else
  139. {
  140. keyTimes[i] = std::strtoul(static_cast<std::string>(keyTimesStr).substr(startOffset, static_cast<std::string>(keyTimesStr).length()).c_str(), NULL, 0);
  141. }
  142. startOffset = endOffset + 1;
  143. }
  144. startOffset = 0;
  145. endOffset = (unsigned int)std::string::npos;
  146. int componentCount = target->getAnimationPropertyComponentCount(propertyId);
  147. assert(componentCount > 0);
  148. unsigned int components = keyCount * componentCount;
  149. float* keyValues = new float[components];
  150. for (unsigned int i = 0; i < components; i++)
  151. {
  152. endOffset = static_cast<std::string>(keyValuesStr).find_first_of(delimeter, startOffset);
  153. if (endOffset != std::string::npos)
  154. {
  155. keyValues[i] = std::atof(static_cast<std::string>(keyValuesStr).substr(startOffset, endOffset - startOffset).c_str());
  156. }
  157. else
  158. {
  159. keyValues[i] = std::atof(static_cast<std::string>(keyValuesStr).substr(startOffset, static_cast<std::string>(keyValuesStr).length()).c_str());
  160. }
  161. startOffset = endOffset + 1;
  162. }
  163. const char* keyInStr = animationProperties->getString("keyIn");
  164. float* keyIn = NULL;
  165. if (keyInStr)
  166. {
  167. keyIn = new float[components];
  168. startOffset = 0;
  169. endOffset = (unsigned int)std::string::npos;
  170. for (unsigned int i = 0; i < components; i++)
  171. {
  172. endOffset = static_cast<std::string>(keyInStr).find_first_of(delimeter, startOffset);
  173. if (endOffset != std::string::npos)
  174. {
  175. keyIn[i] = std::atof(static_cast<std::string>(keyInStr).substr(startOffset, endOffset - startOffset).c_str());
  176. }
  177. else
  178. {
  179. keyIn[i] = std::atof(static_cast<std::string>(keyInStr).substr(startOffset, static_cast<std::string>(keyInStr).length()).c_str());
  180. }
  181. startOffset = endOffset + 1;
  182. }
  183. }
  184. const char* keyOutStr = animationProperties->getString("keyOut");
  185. float* keyOut = NULL;
  186. if (keyOutStr)
  187. {
  188. keyOut = new float[components];
  189. startOffset = 0;
  190. endOffset = (unsigned int)std::string::npos;
  191. for (unsigned int i = 0; i < components; i++)
  192. {
  193. endOffset = static_cast<std::string>(keyOutStr).find_first_of(delimeter, startOffset);
  194. if (endOffset != std::string::npos)
  195. {
  196. keyOut[i] = std::atof(static_cast<std::string>(keyOutStr).substr(startOffset, endOffset - startOffset).c_str());
  197. }
  198. else
  199. {
  200. keyOut[i] = std::atof(static_cast<std::string>(keyOutStr).substr(startOffset, static_cast<std::string>(keyOutStr).length()).c_str());
  201. }
  202. startOffset = endOffset + 1;
  203. }
  204. }
  205. int curve = Curve::getInterpolationType(curveStr);
  206. Animation* animation = NULL;
  207. if (keyIn && keyOut)
  208. {
  209. animation = createAnimation(id, target, propertyId, keyCount, keyTimes, keyValues, keyIn, keyOut, (Curve::InterpolationType)curve);
  210. }
  211. else
  212. {
  213. animation = createAnimation(id, target, propertyId, keyCount, keyTimes, keyValues, (Curve::InterpolationType) curve);
  214. }
  215. SAFE_DELETE(keyOut);
  216. SAFE_DELETE(keyIn);
  217. SAFE_DELETE(keyValues);
  218. SAFE_DELETE(keyTimes);
  219. Properties* pClip = animationProperties->getNextNamespace();
  220. if (pClip && std::strcmp(pClip->getNamespace(), "clip") == 0)
  221. {
  222. int frameCount = animationProperties->getInt("frameCount");
  223. assert(frameCount > 0);
  224. animation->createClips(animationProperties, (unsigned int) frameCount);
  225. }
  226. return animation;
  227. }
  228. AnimationController::State AnimationController::getState() const
  229. {
  230. return _state;
  231. }
  232. void AnimationController::initialize()
  233. {
  234. _state = IDLE;
  235. }
  236. void AnimationController::finalize()
  237. {
  238. std::list<AnimationClip*>::iterator itr = _runningClips.begin();
  239. for ( ; itr != _runningClips.end(); itr++)
  240. {
  241. AnimationClip* clip = *itr;
  242. SAFE_RELEASE(clip);
  243. }
  244. _runningClips.clear();
  245. _state = STOPPED;
  246. }
  247. void AnimationController::resume()
  248. {
  249. if (_runningClips.empty())
  250. _state = IDLE;
  251. else
  252. _state = RUNNING;
  253. }
  254. void AnimationController::pause()
  255. {
  256. _state = PAUSED;
  257. }
  258. void AnimationController::schedule(AnimationClip* clip)
  259. {
  260. if (_runningClips.empty())
  261. {
  262. _state = RUNNING;
  263. }
  264. clip->addRef();
  265. _runningClips.push_back(clip);
  266. }
  267. void AnimationController::unschedule(AnimationClip* clip)
  268. {
  269. std::list<AnimationClip*>::iterator clipItr = _runningClips.begin();
  270. while (clipItr != _runningClips.end())
  271. {
  272. AnimationClip* rClip = (*clipItr);
  273. if (rClip == clip)
  274. {
  275. _runningClips.erase(clipItr);
  276. SAFE_RELEASE(clip);
  277. break;
  278. }
  279. clipItr++;
  280. }
  281. if (_runningClips.empty())
  282. _state = IDLE;
  283. }
  284. void AnimationController::update(long elapsedTime)
  285. {
  286. if (_state != RUNNING)
  287. return;
  288. // Loop through running clips and call update() on them.
  289. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  290. while (clipIter != _runningClips.end())
  291. {
  292. AnimationClip* clip = (*clipIter);
  293. if (clip->isClipStateBitSet(AnimationClip::CLIP_IS_RESTARTED_BIT))
  294. { // If the CLIP_IS_RESTARTED_BIT is set, we should end the clip and
  295. // move it from where it is in the running clips list to the back.
  296. clip->onEnd();
  297. clip->setClipStateBit(AnimationClip::CLIP_IS_PLAYING_BIT);
  298. _runningClips.push_back(clip);
  299. clipIter = _runningClips.erase(clipIter);
  300. }
  301. else if (clip->update(elapsedTime, &_activeTargets))
  302. {
  303. SAFE_RELEASE(clip);
  304. clipIter = _runningClips.erase(clipIter);
  305. }
  306. else
  307. {
  308. clipIter++;
  309. }
  310. }
  311. // Loop through active AnimationTarget's and reset their _animationPropertyBitFlag for the next frame.
  312. std::list<AnimationTarget*>::iterator targetItr = _activeTargets.begin();
  313. while (targetItr != _activeTargets.end())
  314. {
  315. AnimationTarget* target = (*targetItr);
  316. target->_animationPropertyBitFlag = 0x00;
  317. targetItr++;
  318. }
  319. _activeTargets.clear();
  320. if (_runningClips.empty())
  321. _state = IDLE;
  322. }
  323. void AnimationController::addAnimation(Animation* animation)
  324. {
  325. _animations.push_back(animation);
  326. }
  327. void AnimationController::destroyAnimation(Animation* animation)
  328. {
  329. assert(animation);
  330. std::vector<Animation::Channel*>::iterator cItr = animation->_channels.begin();
  331. for (; cItr != animation->_channels.end(); cItr++)
  332. {
  333. Animation::Channel* channel = *cItr;
  334. channel->_target->deleteChannel(channel);
  335. }
  336. std::vector<Animation*>::iterator aItr = _animations.begin();
  337. while (aItr != _animations.end())
  338. {
  339. if (animation == *aItr)
  340. {
  341. Animation* temp = *aItr;
  342. SAFE_RELEASE(temp);
  343. _animations.erase(aItr);
  344. return;
  345. }
  346. aItr++;
  347. }
  348. }
  349. void AnimationController::destroyAllAnimations()
  350. {
  351. std::vector<Animation*>::iterator aItr = _animations.begin();
  352. while (aItr != _animations.end())
  353. {
  354. Animation* animation = *aItr;
  355. std::vector<Animation::Channel*>::iterator cItr = animation->_channels.begin();
  356. for (; cItr != animation->_channels.end(); cItr++)
  357. {
  358. Animation::Channel* channel = *cItr;
  359. channel->_target->deleteChannel(channel);
  360. }
  361. SAFE_RELEASE(animation);
  362. aItr++;
  363. }
  364. _animations.clear();
  365. }
  366. }