AnimationController.cpp 12 KB

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