AnimationController.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  44. float* keyValues = new float[2 * propertyComponentCount];
  45. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  46. memcpy(keyValues + propertyComponentCount, to, sizeof(float) * propertyComponentCount);
  47. unsigned long* keyTimes = new unsigned long[2];
  48. keyTimes[0] = 0;
  49. keyTimes[1] = duration;
  50. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  51. SAFE_DELETE_ARRAY(keyTimes);
  52. return animation;
  53. }
  54. Animation* AnimationController::createAnimationFromBy(const char* id, AnimationTarget* target, int propertyId, float* from, float* by, Curve::InterpolationType type, unsigned long duration)
  55. {
  56. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  57. float* keyValues = new float[2 * propertyComponentCount];
  58. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  59. memcpy(keyValues + propertyComponentCount, by, sizeof(float) * propertyComponentCount);
  60. unsigned long* keyTimes = new unsigned long[2];
  61. keyTimes[0] = 0;
  62. keyTimes[1] = duration;
  63. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  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. SAFE_RELEASE(clip);
  87. clipIter++;
  88. }
  89. _runningClips.clear();
  90. _state = IDLE;
  91. }
  92. AnimationController::State AnimationController::getState() const
  93. {
  94. return _state;
  95. }
  96. void AnimationController::initialize()
  97. {
  98. _state = IDLE;
  99. }
  100. void AnimationController::finalize()
  101. {
  102. _state = PAUSED;
  103. }
  104. void AnimationController::resume()
  105. {
  106. if (_runningClips.empty())
  107. _state = IDLE;
  108. else
  109. _state = RUNNING;
  110. }
  111. void AnimationController::pause()
  112. {
  113. _state = PAUSED;
  114. }
  115. void AnimationController::schedule(AnimationClip* clip)
  116. {
  117. if (_runningClips.empty())
  118. {
  119. _state = RUNNING;
  120. }
  121. if (clip->_isPlaying)
  122. {
  123. _runningClips.remove(clip);
  124. clip->_isPlaying = false;
  125. }
  126. else
  127. {
  128. clip->addRef();
  129. }
  130. _runningClips.push_back(clip);
  131. }
  132. void AnimationController::unschedule(AnimationClip* clip)
  133. {
  134. if (clip->_isPlaying)
  135. {
  136. _runningClips.remove(clip);
  137. SAFE_RELEASE(clip);
  138. }
  139. if (_runningClips.empty())
  140. _state = IDLE;
  141. }
  142. void AnimationController::update(long elapsedTime)
  143. {
  144. if (_state != RUNNING)
  145. return;
  146. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  147. while (clipIter != _runningClips.end())
  148. {
  149. if ((*clipIter)->update(elapsedTime))
  150. {
  151. AnimationClip* clip = *clipIter;
  152. clipIter = _runningClips.erase(clipIter);
  153. SAFE_RELEASE(clip);
  154. }
  155. else
  156. {
  157. clipIter++;
  158. }
  159. }
  160. if (_runningClips.empty())
  161. _state = IDLE;
  162. }
  163. void AnimationController::addAnimation(Animation* animation)
  164. {
  165. _animations.push_back(animation);
  166. }
  167. void AnimationController::destroyAnimation(Animation* animation)
  168. {
  169. std::vector<Animation*>::iterator itr = _animations.begin();
  170. while (itr != _animations.end())
  171. {
  172. if (animation == *itr)
  173. {
  174. _animations.erase(itr);
  175. return;
  176. }
  177. itr++;
  178. }
  179. }
  180. void AnimationController::destroyAllAnimations()
  181. {
  182. stopAllAnimations();
  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. }