2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
CopyRight (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCAnimation.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrame.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "base/ccMacros.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
AnimationFrame* AnimationFrame::create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
|
2013-11-28 16:02:03 +08:00
|
|
|
{
|
|
|
|
auto ret = new AnimationFrame();
|
|
|
|
if (ret && ret->initWithSpriteFrame(spriteFrame, delayUnits, userInfo))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AnimationFrame::AnimationFrame()
|
2013-12-18 17:47:20 +08:00
|
|
|
: _spriteFrame(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _delayUnits(0.0f)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
bool AnimationFrame::initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setDelayUnits(delayUnits);
|
|
|
|
setUserInfo(userInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AnimationFrame::~AnimationFrame()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO( "deallocing AnimationFrame: %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_spriteFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AnimationFrame* AnimationFrame::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto frame = new AnimationFrame();
|
2013-06-19 06:06:53 +08:00
|
|
|
frame->initWithSpriteFrame(_spriteFrame->clone(),
|
|
|
|
_delayUnits,
|
2013-12-03 14:47:35 +08:00
|
|
|
_userInfo);
|
2013-06-19 06:06:53 +08:00
|
|
|
|
|
|
|
frame->autorelease();
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Animation
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Animation* Animation::create(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Animation *animation = new Animation();
|
|
|
|
animation->init();
|
|
|
|
animation->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return animation;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:06:17 +08:00
|
|
|
Animation* Animation::createWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay/* = 0.0f*/, unsigned int loops/* = 1*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Animation *animation = new Animation();
|
2014-04-02 18:06:17 +08:00
|
|
|
animation->initWithSpriteFrames(frames, delay, loops);
|
2013-12-18 17:47:20 +08:00
|
|
|
animation->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return animation;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
Animation* Animation::create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops /* = 1 */)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Animation *animation = new Animation();
|
|
|
|
animation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
|
|
|
|
animation->autorelease();
|
|
|
|
return animation;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Animation::init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-28 16:02:03 +08:00
|
|
|
_loops = 1;
|
|
|
|
_delayPerUnit = 0.0f;
|
|
|
|
|
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:03:42 +08:00
|
|
|
bool Animation::initWithSpriteFrames(const Vector<SpriteFrame*>& frames, float delay/* = 0.0f*/, unsigned int loops/* = 1*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delayPerUnit = delay;
|
2014-04-02 11:48:49 +08:00
|
|
|
_loops = loops;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
for (auto& spriteFrame : frames)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
auto animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
|
2013-12-05 10:35:10 +08:00
|
|
|
_frames.pushBack(animFrame);
|
2013-11-28 16:02:03 +08:00
|
|
|
_totalDelayUnits++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
bool Animation::initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delayPerUnit = delayPerUnit;
|
|
|
|
_loops = loops;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
setFrames(arrayOfAnimationFrames);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
for (auto& animFrame : _frames)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits += animFrame->getDelayUnits();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Animation::Animation()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _totalDelayUnits(0.0f)
|
|
|
|
, _delayPerUnit(0.0f)
|
|
|
|
, _duration(0.0f)
|
|
|
|
, _restoreOriginalFrame(false)
|
|
|
|
, _loops(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Animation::~Animation(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing Animation: %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
void Animation::addSpriteFrame(SpriteFrame* spriteFrame)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
AnimationFrame *animFrame = AnimationFrame::create(spriteFrame, 1.0f, ValueMap());
|
2013-12-05 10:35:10 +08:00
|
|
|
_frames.pushBack(animFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update duration
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-15 09:19:16 +08:00
|
|
|
void Animation::addSpriteFrameWithFile(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
2013-07-12 14:47:36 +08:00
|
|
|
Rect rect = Rect::ZERO;
|
2013-07-26 06:53:24 +08:00
|
|
|
rect.size = texture->getContentSize();
|
2013-12-18 17:47:20 +08:00
|
|
|
SpriteFrame *frame = SpriteFrame::createWithTexture(texture, rect);
|
|
|
|
addSpriteFrame(frame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Animation::addSpriteFrameWithTexture(Texture2D *pobTexture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
SpriteFrame *frame = SpriteFrame::createWithTexture(pobTexture, rect);
|
|
|
|
addSpriteFrame(frame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float Animation::getDuration(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _totalDelayUnits * _delayPerUnit;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Animation* Animation::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new Animation();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithAnimationFrames(_frames, _delayPerUnit, _loops);
|
|
|
|
a->setRestoreOriginalFrame(_restoreOriginalFrame);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|