CCAnimation.cpp 5.6 KB

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