2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCAnimation.h"
|
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCTexture2D.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCSpriteFrame.h"
|
|
|
|
#include "CCZone.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CCAnimationFrame::CCAnimationFrame()
|
|
|
|
: m_pSpriteFrame(NULL)
|
|
|
|
, m_fDelayUnits(0.0f)
|
|
|
|
, m_pUserInfo(NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimationFrame::initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCDictionary* userInfo)
|
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setDelayUnits(delayUnits);
|
|
|
|
setUserInfo(userInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimationFrame::~CCAnimationFrame()
|
|
|
|
{
|
|
|
|
CCLOGINFO( "cocos2d: deallocing %s", this);
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE(m_pSpriteFrame);
|
|
|
|
CC_SAFE_RELEASE(m_pUserInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject* CCAnimationFrame::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCZone* pNewZone = NULL;
|
|
|
|
CCAnimationFrame* pCopy = NULL;
|
|
|
|
if(pZone && pZone->m_pCopyObject)
|
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
|
|
|
pCopy = (CCAnimationFrame*)(pZone->m_pCopyObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pCopy = new CCAnimationFrame();
|
|
|
|
pZone = pNewZone = new CCZone(pCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
pCopy->initWithSpriteFrame((CCSpriteFrame*)m_pSpriteFrame->copy()->autorelease(),
|
|
|
|
m_fDelayUnits, m_pUserInfo != NULL ? (CCDictionary*)m_pUserInfo->copy()->autorelease() : NULL);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCAnimation
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
// CCAnimation* CCAnimation::animation(void)
|
|
|
|
// {
|
|
|
|
// return CCAnimation::create();
|
|
|
|
// }
|
|
|
|
|
|
|
|
CCAnimation* CCAnimation::create(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->init();
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
// CCAnimation* CCAnimation::animationWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
|
|
|
|
// {
|
|
|
|
// return CCAnimation::createWithSpriteFrames(frames, delay);
|
|
|
|
// }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->initWithSpriteFrames(frames, delay);
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
// CCAnimation* CCAnimation::animationWithAnimationFrames(CCArray* arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops)
|
|
|
|
// {
|
|
|
|
// return CCAnimation::createWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
|
|
|
|
// }
|
|
|
|
|
|
|
|
CCAnimation* CCAnimation::createWithAnimationFrames(CCArray* arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
2012-06-14 15:13:16 +08:00
|
|
|
pAnimation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
|
2012-04-19 14:35:52 +08:00
|
|
|
pAnimation->autorelease();
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::init()
|
|
|
|
{
|
|
|
|
return initWithSpriteFrames(NULL, 0.0f);
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
bool CCAnimation::initWithSpriteFrames(CCArray *pFrames, float delay/* = 0.0f*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCARRAY_VERIFY_TYPE(pFrames, CCSpriteFrame*);
|
|
|
|
|
|
|
|
m_uLoops = 1;
|
|
|
|
m_fDelayPerUnit = delay;
|
2012-06-14 16:05:58 +08:00
|
|
|
CCArray* pTmpFrames = CCArray::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
setFrames(pTmpFrames);
|
|
|
|
|
|
|
|
if (pFrames != NULL)
|
|
|
|
{
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(pFrames, pObj)
|
|
|
|
{
|
|
|
|
CCSpriteFrame* frame = (CCSpriteFrame*)pObj;
|
|
|
|
CCAnimationFrame *animFrame = new CCAnimationFrame();
|
|
|
|
animFrame->initWithSpriteFrame(frame, 1, NULL);
|
|
|
|
m_pFrames->addObject(animFrame);
|
|
|
|
animFrame->release();
|
|
|
|
|
|
|
|
m_fTotalDelayUnits++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::initWithAnimationFrames(CCArray* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
|
|
|
|
{
|
|
|
|
CCARRAY_VERIFY_TYPE(arrayOfAnimationFrames, CCAnimationFrame*);
|
|
|
|
|
|
|
|
m_fDelayPerUnit = delayPerUnit;
|
|
|
|
m_uLoops = loops;
|
|
|
|
|
2012-06-14 16:05:58 +08:00
|
|
|
setFrames(CCArray::create(arrayOfAnimationFrames));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pFrames, pObj)
|
|
|
|
{
|
|
|
|
CCAnimationFrame* animFrame = (CCAnimationFrame*)pObj;
|
|
|
|
m_fTotalDelayUnits += animFrame->getDelayUnits();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation::CCAnimation()
|
|
|
|
: m_fTotalDelayUnits(0.0f)
|
|
|
|
, m_fDelayPerUnit(0.0f)
|
|
|
|
, m_fDuration(0.0f)
|
|
|
|
, m_pFrames(NULL)
|
|
|
|
, m_bRestoreOriginalFrame(false)
|
|
|
|
, m_uLoops(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation::~CCAnimation(void)
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d, deallocing %p", this);
|
|
|
|
CC_SAFE_RELEASE(m_pFrames);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrame(CCSpriteFrame *pFrame)
|
|
|
|
{
|
|
|
|
CCAnimationFrame *animFrame = new CCAnimationFrame();
|
|
|
|
animFrame->initWithSpriteFrame(pFrame, 1.0f, NULL);
|
|
|
|
m_pFrames->addObject(animFrame);
|
|
|
|
animFrame->release();
|
|
|
|
|
|
|
|
// update duration
|
|
|
|
m_fTotalDelayUnits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrameWithFileName(const char *pszFileName)
|
|
|
|
{
|
|
|
|
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFileName);
|
|
|
|
CCRect rect = CCRectZero;
|
|
|
|
rect.size = pTexture->getContentSize();
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSpriteFrame *pFrame = CCSpriteFrame::create(pTexture, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
addSpriteFrame(pFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrameWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCSpriteFrame *pFrame = CCSpriteFrame::create(pobTexture, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
addSpriteFrame(pFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCAnimation::getDuration(void)
|
|
|
|
{
|
|
|
|
return m_fTotalDelayUnits * m_fDelayPerUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject* CCAnimation::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCZone* pNewZone = NULL;
|
|
|
|
CCAnimation* pCopy = NULL;
|
|
|
|
if(pZone && pZone->m_pCopyObject)
|
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
|
|
|
pCopy = (CCAnimation*)(pZone->m_pCopyObject);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pCopy = new CCAnimation();
|
|
|
|
pZone = pNewZone = new CCZone(pCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
pCopy->initWithAnimationFrames(m_pFrames, m_fDelayPerUnit, m_uLoops);
|
|
|
|
pCopy->setRestoreOriginalFrame(m_bRestoreOriginalFrame);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|