AnimationController.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * AnimationController.cpp
  3. */
  4. #include "Base.h"
  5. #include "AnimationController.h"
  6. #include "Game.h"
  7. #include "Curve.h"
  8. namespace gameplay
  9. {
  10. AnimationController::AnimationController()
  11. : _state(IDLE), _animations(NULL)
  12. {
  13. }
  14. AnimationController::~AnimationController()
  15. {
  16. destroyAllAnimations();
  17. }
  18. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, Curve::InterpolationType type)
  19. {
  20. assert(type != Curve::BEZIER && type != Curve::HERMITE);
  21. assert(id && keyCount >= 2 && keyTimes && keyValues);
  22. Animation* animation = getAnimation(id);
  23. if (animation != NULL)
  24. return NULL;
  25. animation = new Animation(id, target, propertyId, keyCount, keyTimes, keyValues, type);
  26. addAnimation(animation);
  27. target->addAnimation(animation);
  28. return animation;
  29. }
  30. 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)
  31. {
  32. assert(id && keyCount >= 2 && keyTimes && keyValues && keyInValue && keyOutValue);
  33. Animation* animation = getAnimation(id);
  34. if (animation != NULL)
  35. return NULL;
  36. animation = new Animation(id, target, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
  37. addAnimation(animation);
  38. target->addAnimation(animation);
  39. return animation;
  40. }
  41. Animation* AnimationController::createAnimationFromTo(const char* id, AnimationTarget* target, int propertyId, float* from, float* to, Curve::InterpolationType type, unsigned long duration)
  42. {
  43. const unsigned int keyCount = 2;
  44. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  45. float* keyValues = new float[2 * propertyComponentCount];
  46. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  47. memcpy(keyValues + propertyComponentCount, to, sizeof(float) * propertyComponentCount);
  48. unsigned long* keyTimes = new unsigned long[2];
  49. keyTimes[0] = 0;
  50. keyTimes[1] = duration;
  51. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  52. SAFE_DELETE_ARRAY(keyTimes);
  53. return animation;
  54. }
  55. Animation* AnimationController::createAnimationFromBy(const char* id, AnimationTarget* target, int propertyId, float* from, float* by, Curve::InterpolationType type, unsigned long duration)
  56. {
  57. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  58. float* keyValues = new float[2 * propertyComponentCount];
  59. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  60. memcpy(keyValues + propertyComponentCount, by, sizeof(float) * propertyComponentCount);
  61. unsigned long* keyTimes = new unsigned long[2];
  62. keyTimes[0] = 0;
  63. keyTimes[1] = duration;
  64. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  65. SAFE_DELETE_ARRAY(keyTimes);
  66. return animation;
  67. }
  68. Animation* AnimationController::getAnimation(const char* id) const
  69. {
  70. unsigned int animationCount = _animations.size();
  71. for (unsigned int i = 0; i < animationCount; i++)
  72. {
  73. if (_animations.at(i)->_id.compare(id) == 0)
  74. {
  75. return _animations.at(i);
  76. }
  77. }
  78. return NULL;
  79. }
  80. void AnimationController::stopAllAnimations()
  81. {
  82. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  83. while (clipIter != _runningClips.end())
  84. {
  85. AnimationClip* clip = *clipIter;
  86. clipIter = _runningClips.erase(clipIter);
  87. clip->_isPlaying = false;
  88. SAFE_RELEASE(clip);
  89. clipIter++;
  90. }
  91. _state = IDLE;
  92. }
  93. AnimationController::State AnimationController::getState() const
  94. {
  95. return _state;
  96. }
  97. void AnimationController::initialize()
  98. {
  99. _state = IDLE;
  100. }
  101. void AnimationController::finalize()
  102. {
  103. _state = PAUSED;
  104. }
  105. void AnimationController::resume()
  106. {
  107. if (_runningClips.empty())
  108. _state = IDLE;
  109. else
  110. _state = RUNNING;
  111. }
  112. void AnimationController::pause()
  113. {
  114. _state = PAUSED;
  115. }
  116. void AnimationController::schedule(AnimationClip* clip)
  117. {
  118. if (_runningClips.empty())
  119. {
  120. _state = RUNNING;
  121. }
  122. if (clip->_isPlaying)
  123. {
  124. _runningClips.remove(clip);
  125. clip->_isPlaying = false;
  126. }
  127. else
  128. {
  129. clip->addRef();
  130. }
  131. _runningClips.push_back(clip);
  132. }
  133. void AnimationController::unschedule(AnimationClip* clip)
  134. {
  135. if (clip->_isPlaying)
  136. {
  137. _runningClips.remove(clip);
  138. SAFE_RELEASE(clip);
  139. }
  140. if (_runningClips.empty())
  141. _state = IDLE;
  142. }
  143. void AnimationController::update(long elapsedTime)
  144. {
  145. if (_state != RUNNING)
  146. return;
  147. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  148. while (clipIter != _runningClips.end())
  149. {
  150. if ((*clipIter)->update(elapsedTime))
  151. {
  152. AnimationClip* clip = *clipIter;
  153. clipIter = _runningClips.erase(clipIter);
  154. SAFE_RELEASE(clip);
  155. }
  156. else
  157. {
  158. clipIter++;
  159. }
  160. }
  161. if (_runningClips.empty())
  162. _state = IDLE;
  163. }
  164. void AnimationController::addAnimation(Animation* animation)
  165. {
  166. _animations.push_back(animation);
  167. }
  168. void AnimationController::destroyAnimation(Animation* animation)
  169. {
  170. std::vector<Animation*>::iterator itr = _animations.begin();
  171. while (itr != _animations.end())
  172. {
  173. if (animation == *itr)
  174. {
  175. _animations.erase(itr);
  176. return;
  177. }
  178. itr++;
  179. }
  180. }
  181. void AnimationController::destroyAllAnimations()
  182. {
  183. std::vector<Animation*>::iterator itr = _animations.begin();
  184. while (itr != _animations.end())
  185. {
  186. Animation* animation = *itr;
  187. SAFE_RELEASE(animation);
  188. itr++;
  189. }
  190. _animations.clear();
  191. }
  192. }