CCAnimation.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCAnimation.h"
  24. #include "renderer/CCTextureCache.h"
  25. #include "renderer/CCTexture2D.h"
  26. #include "base/CCDirector.h"
  27. NS_CC_BEGIN
  28. AnimationFrame* AnimationFrame::create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
  29. {
  30. auto ret = new (std::nothrow) AnimationFrame();
  31. if (ret && ret->initWithSpriteFrame(spriteFrame, delayUnits, userInfo))
  32. {
  33. ret->autorelease();
  34. }
  35. else
  36. {
  37. CC_SAFE_DELETE(ret);
  38. }
  39. return ret;
  40. }
  41. AnimationFrame::AnimationFrame()
  42. : _spriteFrame(nullptr)
  43. , _delayUnits(0.0f)
  44. {
  45. }
  46. bool AnimationFrame::initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
  47. {
  48. setSpriteFrame(spriteFrame);
  49. setDelayUnits(delayUnits);
  50. setUserInfo(userInfo);
  51. return true;
  52. }
  53. AnimationFrame::~AnimationFrame()
  54. {
  55. CCLOGINFO( "deallocing AnimationFrame: %p", this);
  56. CC_SAFE_RELEASE(_spriteFrame);
  57. }
  58. AnimationFrame* AnimationFrame::clone() const
  59. {
  60. // no copy constructor
  61. auto frame = new (std::nothrow) AnimationFrame();
  62. frame->initWithSpriteFrame(_spriteFrame->clone(),
  63. _delayUnits,
  64. _userInfo);
  65. frame->autorelease();
  66. return frame;
  67. }
  68. // implementation of Animation
  69. Animation* Animation::create()
  70. {
  71. Animation *animation = new (std::nothrow) Animation();
  72. animation->init();
  73. animation->autorelease();
  74. return animation;
  75. }
  76. Animation* Animation::createWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay/* = 0.0f*/, unsigned int loops/* = 1*/)
  77. {
  78. Animation *animation = new (std::nothrow) Animation();
  79. animation->initWithSpriteFrames(frames, delay, loops);
  80. animation->autorelease();
  81. return animation;
  82. }
  83. Animation* Animation::create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops /* = 1 */)
  84. {
  85. Animation *animation = new (std::nothrow) Animation();
  86. animation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
  87. animation->autorelease();
  88. return animation;
  89. }
  90. bool Animation::init()
  91. {
  92. _loops = 1;
  93. _delayPerUnit = 0.0f;
  94. return true;
  95. }
  96. bool Animation::initWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay/* = 0.0f*/, unsigned int loops/* = 1*/)
  97. {
  98. _delayPerUnit = delay;
  99. _loops = loops;
  100. for (auto& spriteFrame : frames)
  101. {
  102. auto animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
  103. _frames.pushBack(animFrame);
  104. _totalDelayUnits++;
  105. }
  106. return true;
  107. }
  108. bool Animation::initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
  109. {
  110. _delayPerUnit = delayPerUnit;
  111. _loops = loops;
  112. setFrames(arrayOfAnimationFrames);
  113. for (auto& animFrame : _frames)
  114. {
  115. _totalDelayUnits += animFrame->getDelayUnits();
  116. }
  117. return true;
  118. }
  119. Animation::Animation()
  120. : _totalDelayUnits(0.0f)
  121. , _delayPerUnit(0.0f)
  122. , _duration(0.0f)
  123. , _restoreOriginalFrame(false)
  124. , _loops(0)
  125. {
  126. }
  127. Animation::~Animation(void)
  128. {
  129. CCLOGINFO("deallocing Animation: %p", this);
  130. }
  131. void Animation::addSpriteFrame(SpriteFrame* spriteFrame)
  132. {
  133. AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, 1.0f, ValueMap());
  134. _frames.pushBack(animFrame);
  135. // update duration
  136. _totalDelayUnits++;
  137. }
  138. void Animation::addSpriteFrameWithFile(const std::string& filename)
  139. {
  140. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
  141. Rect rect = Rect::ZERO;
  142. rect.size = texture->getContentSize();
  143. SpriteFrame *frame = SpriteFrame::createWithTexture(texture, rect);
  144. addSpriteFrame(frame);
  145. }
  146. void Animation::addSpriteFrameWithTexture(Texture2D *pobTexture, const Rect& rect)
  147. {
  148. SpriteFrame *frame = SpriteFrame::createWithTexture(pobTexture, rect);
  149. addSpriteFrame(frame);
  150. }
  151. float Animation::getDuration(void) const
  152. {
  153. return _totalDelayUnits * _delayPerUnit;
  154. }
  155. Animation* Animation::clone() const
  156. {
  157. // no copy constructor
  158. auto a = new (std::nothrow) Animation();
  159. a->initWithAnimationFrames(_frames, _delayPerUnit, _loops);
  160. a->setRestoreOriginalFrame(_restoreOriginalFrame);
  161. a->autorelease();
  162. return a;
  163. }
  164. NS_CC_END