123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "2d/CCAnimation.h"
- #include "renderer/CCTextureCache.h"
- #include "renderer/CCTexture2D.h"
- #include "base/CCDirector.h"
- NS_CC_BEGIN
- AnimationFrame* AnimationFrame::create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
- {
- auto ret = new (std::nothrow) AnimationFrame();
- if (ret && ret->initWithSpriteFrame(spriteFrame, delayUnits, userInfo))
- {
- ret->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(ret);
- }
- return ret;
- }
- AnimationFrame::AnimationFrame()
- : _spriteFrame(nullptr)
- , _delayUnits(0.0f)
- {
- }
- bool AnimationFrame::initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
- {
- setSpriteFrame(spriteFrame);
- setDelayUnits(delayUnits);
- setUserInfo(userInfo);
- return true;
- }
- AnimationFrame::~AnimationFrame()
- {
- CCLOGINFO( "deallocing AnimationFrame: %p", this);
- CC_SAFE_RELEASE(_spriteFrame);
- }
- AnimationFrame* AnimationFrame::clone() const
- {
-
- auto frame = new (std::nothrow) AnimationFrame();
- frame->initWithSpriteFrame(_spriteFrame->clone(),
- _delayUnits,
- _userInfo);
- frame->autorelease();
- return frame;
- }
- Animation* Animation::create()
- {
- Animation *animation = new (std::nothrow) Animation();
- animation->init();
- animation->autorelease();
- return animation;
- }
- Animation* Animation::createWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay, unsigned int loops)
- {
- Animation *animation = new (std::nothrow) Animation();
- animation->initWithSpriteFrames(frames, delay, loops);
- animation->autorelease();
- return animation;
- }
- Animation* Animation::create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops )
- {
- Animation *animation = new (std::nothrow) Animation();
- animation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
- animation->autorelease();
- return animation;
- }
- bool Animation::init()
- {
- _loops = 1;
- _delayPerUnit = 0.0f;
-
- return true;
- }
- bool Animation::initWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay, unsigned int loops)
- {
- _delayPerUnit = delay;
- _loops = loops;
- for (auto& spriteFrame : frames)
- {
- auto animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
- _frames.pushBack(animFrame);
- _totalDelayUnits++;
- }
- return true;
- }
- bool Animation::initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
- {
- _delayPerUnit = delayPerUnit;
- _loops = loops;
- setFrames(arrayOfAnimationFrames);
- for (auto& animFrame : _frames)
- {
- _totalDelayUnits += animFrame->getDelayUnits();
- }
- return true;
- }
- Animation::Animation()
- : _totalDelayUnits(0.0f)
- , _delayPerUnit(0.0f)
- , _duration(0.0f)
- , _restoreOriginalFrame(false)
- , _loops(0)
- {
- }
- Animation::~Animation(void)
- {
- CCLOGINFO("deallocing Animation: %p", this);
- }
- void Animation::addSpriteFrame(SpriteFrame* spriteFrame)
- {
- AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, 1.0f, ValueMap());
- _frames.pushBack(animFrame);
-
- _totalDelayUnits++;
- }
- void Animation::addSpriteFrameWithFile(const std::string& filename)
- {
- Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
- Rect rect = Rect::ZERO;
- rect.size = texture->getContentSize();
- SpriteFrame *frame = SpriteFrame::createWithTexture(texture, rect);
- addSpriteFrame(frame);
- }
- void Animation::addSpriteFrameWithTexture(Texture2D *pobTexture, const Rect& rect)
- {
- SpriteFrame *frame = SpriteFrame::createWithTexture(pobTexture, rect);
- addSpriteFrame(frame);
- }
- float Animation::getDuration(void) const
- {
- return _totalDelayUnits * _delayPerUnit;
- }
- Animation* Animation::clone() const
- {
-
- auto a = new (std::nothrow) Animation();
- a->initWithAnimationFrames(_frames, _delayPerUnit, _loops);
- a->setRestoreOriginalFrame(_restoreOriginalFrame);
- a->autorelease();
- return a;
- }
- NS_CC_END
|