2010-12-27 10:26:56 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-01 16:11:31 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-12-27 10:26:56 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCAnimation.h"
|
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCTexture2D.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCSpriteFrame.h"
|
2012-03-16 13:42:53 +08:00
|
|
|
#include "CCZone.h"
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
CCAnimationFrame::CCAnimationFrame()
|
|
|
|
: m_pSpriteFrame(NULL)
|
|
|
|
, m_fDelayUnits(0.0f)
|
|
|
|
, m_pUserInfo(NULL)
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
bool CCAnimationFrame::initWithSpriteFrame(CCSpriteFrame* spriteFrame, float delayUnits, CCObjectDictionary* userInfo)
|
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setDelayUnits(delayUnits);
|
|
|
|
setUserInfo(userInfo);
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CCAnimationFrame::~CCAnimationFrame()
|
|
|
|
{
|
|
|
|
CCLOGINFO( "cocos2d: deallocing %s", this);
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CC_SAFE_RELEASE(m_pSpriteFrame);
|
|
|
|
CC_SAFE_RELEASE(m_pUserInfo);
|
|
|
|
}
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
CCObject* CCAnimationFrame::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCZone* pNewZone = NULL;
|
|
|
|
CCAnimationFrame* pCopy = NULL;
|
|
|
|
if(pZone && pZone->m_pCopyObject)
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-03-16 13:42:53 +08:00
|
|
|
//in case of being called at sub class
|
|
|
|
pCopy = (CCAnimationFrame*)(pZone->m_pCopyObject);
|
2010-12-27 10:26:56 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
else
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-03-16 13:42:53 +08:00
|
|
|
pCopy = new CCAnimationFrame();
|
|
|
|
pZone = pNewZone = new CCZone(pCopy);
|
2010-12-27 10:26:56 +08:00
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
pCopy->initWithSpriteFrame((CCSpriteFrame*)m_pSpriteFrame->copy()->autorelease(),
|
|
|
|
m_fDelayUnits, (CCObjectDictionary*)m_pUserInfo->copy()->autorelease());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of CCAnimation
|
|
|
|
|
|
|
|
CCAnimation* CCAnimation::animation(void)
|
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->init();
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation* CCAnimation::animationWithSpriteFrames(CCMutableArray<CCSpriteFrame*> *frames)
|
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->initWithSpriteFrames(frames);
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation* CCAnimation::animationWithSpriteFrames(CCMutableArray<CCSpriteFrame*> *frames, float delay)
|
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->initWithSpriteFrames(frames, delay);
|
|
|
|
pAnimation->autorelease();
|
|
|
|
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAnimation* CCAnimation::animationWithAnimationFrames(CCMutableArray<CCAnimationFrame*>* arrayOfSpriteFrameNames, float delayPerUnit, unsigned int loops)
|
|
|
|
{
|
|
|
|
CCAnimation *pAnimation = new CCAnimation();
|
|
|
|
pAnimation->initWithAnimationFrames(arrayOfSpriteFrameNames, delayPerUnit, loops);
|
|
|
|
pAnimation->autorelease();
|
|
|
|
return pAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::init()
|
|
|
|
{
|
|
|
|
return initWithSpriteFrames(NULL, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::initWithSpriteFrames(CCMutableArray<CCSpriteFrame*>* pFrames)
|
|
|
|
{
|
|
|
|
return initWithSpriteFrames(pFrames, 0.0f);
|
|
|
|
}
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
bool CCAnimation::initWithSpriteFrames(CCMutableArray<CCSpriteFrame*> *pFrames, float delay)
|
|
|
|
{
|
|
|
|
m_uLoops = 1;
|
|
|
|
m_fDelayPerUnit = delay;
|
|
|
|
CCMutableArray<CCAnimationFrame*>* pTmpFrames = new CCMutableArray<CCAnimationFrame*>();
|
|
|
|
pTmpFrames->autorelease();
|
|
|
|
setFrames(pTmpFrames);
|
|
|
|
|
|
|
|
if (pFrames != NULL)
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-03-16 13:42:53 +08:00
|
|
|
for(CCMutableArray<CCSpriteFrame*>::CCMutableArrayIterator it = pFrames->begin(); it != pFrames->end(); ++it)
|
|
|
|
{
|
|
|
|
CCSpriteFrame* frame = *it;
|
|
|
|
CCAnimationFrame *animFrame = new CCAnimationFrame();
|
|
|
|
animFrame->initWithSpriteFrame(frame, 1, NULL);
|
|
|
|
m_pFrames->addObject(animFrame);
|
|
|
|
animFrame->release();
|
|
|
|
|
|
|
|
m_fTotalDelayUnits++;
|
|
|
|
}
|
2010-12-27 10:26:56 +08:00
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAnimation::initWithAnimationFrames(CCMutableArray<CCAnimationFrame*>* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops)
|
|
|
|
{
|
|
|
|
m_fDelayPerUnit = delayPerUnit;
|
|
|
|
m_uLoops = loops;
|
|
|
|
|
|
|
|
setFrames(CCMutableArray<CCAnimationFrame*>::arrayWithArray(arrayOfAnimationFrames));
|
|
|
|
|
|
|
|
for( CCMutableArray<CCAnimationFrame*>::CCMutableArrayIterator it = m_pFrames->begin(); it != m_pFrames->end(); ++it )
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-03-16 13:42:53 +08:00
|
|
|
CCAnimationFrame *animFrame = *it;
|
|
|
|
m_fTotalDelayUnits += animFrame->getDelayUnits();
|
2010-12-27 10:26:56 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
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);
|
|
|
|
}
|
2010-12-27 10:26:56 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
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();
|
|
|
|
CCSpriteFrame *pFrame = CCSpriteFrame::frameWithTexture(pTexture, rect);
|
|
|
|
addSpriteFrame(pFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAnimation::addSpriteFrameWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
|
|
|
|
{
|
|
|
|
CCSpriteFrame *pFrame = CCSpriteFrame::frameWithTexture(pobTexture, rect);
|
|
|
|
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)
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-03-16 13:42:53 +08:00
|
|
|
//in case of being called at sub class
|
|
|
|
pCopy = (CCAnimation*)(pZone->m_pCopyObject);
|
2010-12-27 10:26:56 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
else
|
2010-12-27 10:26:56 +08:00
|
|
|
{
|
2012-03-16 13:42:53 +08:00
|
|
|
pCopy = new CCAnimation();
|
|
|
|
pZone = pNewZone = new CCZone(pCopy);
|
2010-12-27 10:26:56 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
|
|
|
pCopy->initWithAnimationFrames(m_pFrames, m_fDelayPerUnit, m_uLoops);
|
|
|
|
pCopy->setRestoreOriginalFrame(m_bRestoreOriginalFrame);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|