AnimationController.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. clip->_isPlaying = false;
  86. clip->onEnd();
  87. SAFE_RELEASE(clip);
  88. clipIter++;
  89. }
  90. _runningClips.clear();
  91. _state = IDLE;
  92. }
  93. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, Properties* animationProperties)
  94. {
  95. assert(target && animationProperties);
  96. assert(std::strcmp(animationProperties->getNamespace(), "animation") == 0);
  97. const char* propertyIdStr = animationProperties->getString("property");
  98. assert(propertyIdStr);
  99. // Get animation target property id
  100. int propertyId = AnimationTarget::getPropertyId(target->_targetType, propertyIdStr);
  101. assert(propertyId != -1);
  102. unsigned int keyCount = animationProperties->getInt("keyCount");
  103. assert(keyCount > 0);
  104. const char* keyTimesStr = animationProperties->getString("keyTimes");
  105. assert(keyTimesStr);
  106. const char* keyValuesStr = animationProperties->getString("keyValues");
  107. assert(keyValuesStr);
  108. const char* curveStr = animationProperties->getString("curve");
  109. assert(curveStr);
  110. char delimeter = ' ';
  111. unsigned int startOffset = 0;
  112. unsigned int endOffset = std::string::npos;
  113. unsigned long* keyTimes = new unsigned long[keyCount];
  114. for (unsigned int i = 0; i < keyCount; i++)
  115. {
  116. endOffset = static_cast<std::string>(keyTimesStr).find_first_of(delimeter, startOffset);
  117. if (endOffset != std::string::npos)
  118. {
  119. keyTimes[i] = std::strtoul(static_cast<std::string>(keyTimesStr).substr(startOffset, endOffset - startOffset).c_str(), NULL, 0);
  120. }
  121. else
  122. {
  123. keyTimes[i] = std::strtoul(static_cast<std::string>(keyTimesStr).substr(startOffset, static_cast<std::string>(keyTimesStr).length()).c_str(), NULL, 0);
  124. }
  125. startOffset = endOffset + 1;
  126. }
  127. startOffset = 0;
  128. endOffset = std::string::npos;
  129. int componentCount = target->getAnimationPropertyComponentCount(propertyId);
  130. assert(componentCount > 0);
  131. unsigned int components = keyCount * componentCount;
  132. float* keyValues = new float[components];
  133. for (unsigned int i = 0; i < components; i++)
  134. {
  135. endOffset = static_cast<std::string>(keyValuesStr).find_first_of(delimeter, startOffset);
  136. if (endOffset != std::string::npos)
  137. {
  138. keyValues[i] = std::atof(static_cast<std::string>(keyValuesStr).substr(startOffset, endOffset - startOffset).c_str());
  139. }
  140. else
  141. {
  142. keyValues[i] = std::atof(static_cast<std::string>(keyValuesStr).substr(startOffset, static_cast<std::string>(keyValuesStr).length()).c_str());
  143. }
  144. startOffset = endOffset + 1;
  145. }
  146. const char* keyInStr = animationProperties->getString("keyIn");
  147. float* keyIn = NULL;
  148. if (keyInStr)
  149. {
  150. keyIn = new float[components];
  151. startOffset = 0;
  152. endOffset = std::string::npos;
  153. for (unsigned int i = 0; i < components; i++)
  154. {
  155. endOffset = static_cast<std::string>(keyInStr).find_first_of(delimeter, startOffset);
  156. if (endOffset != std::string::npos)
  157. {
  158. keyIn[i] = std::atof(static_cast<std::string>(keyInStr).substr(startOffset, endOffset - startOffset).c_str());
  159. }
  160. else
  161. {
  162. keyIn[i] = std::atof(static_cast<std::string>(keyInStr).substr(startOffset, static_cast<std::string>(keyInStr).length()).c_str());
  163. }
  164. startOffset = endOffset + 1;
  165. }
  166. }
  167. const char* keyOutStr = animationProperties->getString("keyOut");
  168. float* keyOut = NULL;
  169. if(keyOutStr)
  170. {
  171. keyOut = new float[components];
  172. startOffset = 0;
  173. endOffset = std::string::npos;
  174. for (unsigned int i = 0; i < components; i++)
  175. {
  176. endOffset = static_cast<std::string>(keyOutStr).find_first_of(delimeter, startOffset);
  177. if (endOffset != std::string::npos)
  178. {
  179. keyOut[i] = std::atof(static_cast<std::string>(keyOutStr).substr(startOffset, endOffset - startOffset).c_str());
  180. }
  181. else
  182. {
  183. keyOut[i] = std::atof(static_cast<std::string>(keyOutStr).substr(startOffset, static_cast<std::string>(keyOutStr).length()).c_str());
  184. }
  185. startOffset = endOffset + 1;
  186. }
  187. }
  188. int curve = Curve::getInterpolationType(curveStr);
  189. Animation* animation = NULL;
  190. if (keyIn && keyOut)
  191. {
  192. animation = createAnimation(id, target, propertyId, keyCount, keyTimes, keyValues, keyIn, keyOut, (Curve::InterpolationType)curve);
  193. }
  194. else
  195. {
  196. animation = createAnimation(id, target, propertyId, keyCount, keyTimes, keyValues, (Curve::InterpolationType) curve);
  197. }
  198. SAFE_DELETE(keyOut);
  199. SAFE_DELETE(keyIn);
  200. SAFE_DELETE(keyValues);
  201. SAFE_DELETE(keyTimes);
  202. Properties* pClip = animationProperties->getNextNamespace();
  203. if (pClip && std::strcmp(pClip->getNamespace(), "clip") == 0)
  204. {
  205. int frameCount = animationProperties->getInt("frameCount");
  206. assert(frameCount > 0);
  207. animation->createClips(animationProperties, (unsigned int) frameCount);
  208. }
  209. return animation;
  210. }
  211. AnimationController::State AnimationController::getState() const
  212. {
  213. return _state;
  214. }
  215. void AnimationController::initialize()
  216. {
  217. _state = IDLE;
  218. }
  219. void AnimationController::finalize()
  220. {
  221. stopAllAnimations();
  222. _state = STOPPED;
  223. }
  224. void AnimationController::resume()
  225. {
  226. if (_runningClips.empty())
  227. _state = IDLE;
  228. else
  229. _state = RUNNING;
  230. }
  231. void AnimationController::pause()
  232. {
  233. _state = PAUSED;
  234. }
  235. void AnimationController::schedule(AnimationClip* clip)
  236. {
  237. if (_runningClips.empty())
  238. {
  239. _state = RUNNING;
  240. }
  241. if (clip->_isPlaying)
  242. {
  243. _runningClips.remove(clip);
  244. clip->_isPlaying = false;
  245. clip->onEnd();
  246. }
  247. else
  248. {
  249. clip->addRef();
  250. }
  251. _runningClips.push_back(clip);
  252. }
  253. void AnimationController::unschedule(AnimationClip* clip)
  254. {
  255. std::list<AnimationClip*>::iterator clipItr = _runningClips.begin();
  256. while (clipItr != _runningClips.end())
  257. {
  258. AnimationClip* rClip = (*clipItr);
  259. if (rClip == clip)
  260. {
  261. _runningClips.erase(clipItr);
  262. SAFE_RELEASE(clip);
  263. break;
  264. }
  265. clipItr++;
  266. }
  267. if (_runningClips.empty())
  268. _state = IDLE;
  269. }
  270. void AnimationController::update(long elapsedTime)
  271. {
  272. if (_state != RUNNING)
  273. return;
  274. // Loop through running clips and call update() on them.
  275. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  276. while (clipIter != _runningClips.end())
  277. {
  278. AnimationClip* clip = (*clipIter);
  279. if (clip->update(elapsedTime, &_activeTargets))
  280. {
  281. SAFE_RELEASE(clip);
  282. clipIter = _runningClips.erase(clipIter);
  283. }
  284. else
  285. {
  286. clipIter++;
  287. }
  288. }
  289. // Loop through active AnimationTarget's and reset their _animationPropertyBitFlag for the next frame.
  290. std::list<AnimationTarget*>::iterator targetItr = _activeTargets.begin();
  291. while (targetItr != _activeTargets.end())
  292. {
  293. AnimationTarget* target = (*targetItr);
  294. target->_animationPropertyBitFlag = 0x00;
  295. targetItr++;
  296. }
  297. _activeTargets.clear();
  298. if (_runningClips.empty())
  299. _state = IDLE;
  300. }
  301. void AnimationController::addAnimation(Animation* animation)
  302. {
  303. _animations.push_back(animation);
  304. }
  305. void AnimationController::destroyAnimation(Animation* animation)
  306. {
  307. std::vector<Animation*>::iterator itr = _animations.begin();
  308. while (itr != _animations.end())
  309. {
  310. if (animation == *itr)
  311. {
  312. Animation* animation = *itr;
  313. _animations.erase(itr);
  314. SAFE_RELEASE(animation);
  315. return;
  316. }
  317. itr++;
  318. }
  319. }
  320. void AnimationController::destroyAllAnimations()
  321. {
  322. std::vector<Animation*>::iterator itr = _animations.begin();
  323. while (itr != _animations.end())
  324. {
  325. Animation* animation = *itr;
  326. SAFE_RELEASE(animation);
  327. itr++;
  328. }
  329. _animations.clear();
  330. }
  331. }