AnimationController.cpp 12 KB

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