2010-12-31 14:56:24 +08:00
/****************************************************************************
2011-03-19 14:45:51 +08:00
Copyright ( c ) 2010 - 2011 cocos2d - x . org
Copyright ( c ) 2010 Ricardo Quesada
2011-07-01 16:11:31 +08:00
Copyright ( c ) 2011 Zynga Inc .
2010-12-31 14:56:24 +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 "CCAnimationCache.h"
# include "ccMacros.h"
# include "CCAnimation.h"
# include "CCSpriteFrame.h"
2012-03-16 13:42:53 +08:00
# include "CCSpriteFrameCache.h"
# include "CCString.h"
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
using namespace std ;
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
NS_CC_BEGIN
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
CCAnimationCache * CCAnimationCache : : s_pSharedAnimationCache = NULL ;
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
CCAnimationCache * CCAnimationCache : : sharedAnimationCache ( void )
{
if ( ! s_pSharedAnimationCache )
2010-12-31 14:56:24 +08:00
{
2012-03-16 13:42:53 +08:00
s_pSharedAnimationCache = new CCAnimationCache ( ) ;
s_pSharedAnimationCache - > init ( ) ;
2010-12-31 14:56:24 +08:00
}
2012-03-16 13:42:53 +08:00
return s_pSharedAnimationCache ;
}
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
void CCAnimationCache : : purgeSharedAnimationCache ( void )
{
CC_SAFE_RELEASE_NULL ( s_pSharedAnimationCache ) ;
}
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
bool CCAnimationCache : : init ( )
{
2012-03-20 15:04:53 +08:00
m_pAnimations = new CCDictionary ( ) ;
2012-03-16 13:42:53 +08:00
return true ;
}
CCAnimationCache : : CCAnimationCache ( )
: m_pAnimations ( NULL )
{
}
CCAnimationCache : : ~ CCAnimationCache ( )
{
CCLOGINFO ( " cocos2d: deallocing %p " , this ) ;
CC_SAFE_RELEASE ( m_pAnimations ) ;
}
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
void CCAnimationCache : : addAnimation ( CCAnimation * animation , const char * name )
{
2012-03-20 15:04:53 +08:00
m_pAnimations - > setObject ( animation , name ) ;
2012-03-16 13:42:53 +08:00
}
void CCAnimationCache : : removeAnimationByName ( const char * name )
{
if ( ! name )
2010-12-31 14:56:24 +08:00
{
2012-03-16 13:42:53 +08:00
return ;
2010-12-31 14:56:24 +08:00
}
2012-03-20 15:04:53 +08:00
m_pAnimations - > removeObjectForKey ( name ) ;
2012-03-16 13:42:53 +08:00
}
2010-12-31 14:56:24 +08:00
2012-03-16 13:42:53 +08:00
CCAnimation * CCAnimationCache : : animationByName ( const char * name )
{
2012-03-20 15:04:53 +08:00
return ( CCAnimation * ) m_pAnimations - > objectForKey ( name ) ;
2012-03-16 13:42:53 +08:00
}
2010-12-31 14:56:24 +08:00
2012-03-20 15:04:53 +08:00
void CCAnimationCache : : parseVersion1 ( CCDictionary * animations )
2012-03-16 13:42:53 +08:00
{
CCSpriteFrameCache * frameCache = CCSpriteFrameCache : : sharedSpriteFrameCache ( ) ;
2012-03-20 15:04:53 +08:00
CCDictElement * pElement = NULL ;
CCDICT_FOREACH ( animations , pElement )
{
CCDictionary * animationDict = ( CCDictionary * ) pElement - > getObject ( ) ;
CCArray * frameNames = ( CCArray * ) animationDict - > objectForKey ( " frames " ) ;
2012-03-16 13:42:53 +08:00
float delay = ( float ) atof ( valueForKey ( " delay " , animationDict ) ) ;
CCAnimation * animation = NULL ;
2012-03-20 15:04:53 +08:00
if ( frameNames = = NULL )
{
CCLOG ( " cocos2d: CCAnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache. " , pElement - > getStrKey ( ) ) ;
2012-03-16 13:42:53 +08:00
continue ;
}
2012-03-20 15:04:53 +08:00
CCArray * frames = CCArray : : arrayWithCapacity ( frameNames - > count ( ) ) ;
frames - > retain ( ) ;
2012-03-16 13:42:53 +08:00
2012-03-20 15:04:53 +08:00
CCObject * pObj = NULL ;
CCARRAY_FOREACH ( frameNames , pObj )
2012-03-16 13:42:53 +08:00
{
2012-03-20 15:04:53 +08:00
const char * frameName = ( ( CCString * ) pObj ) - > c_str ( ) ;
2012-03-16 13:42:53 +08:00
CCSpriteFrame * spriteFrame = frameCache - > spriteFrameByName ( frameName ) ;
if ( ! spriteFrame ) {
2012-03-20 15:04:53 +08:00
CCLOG ( " cocos2d: CCAnimationCache: Animation '%s' refers to frame '%s' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation. " , pElement - > getStrKey ( ) , frameName ) ;
2012-03-16 13:42:53 +08:00
continue ;
}
CCAnimationFrame * animFrame = new CCAnimationFrame ( ) ;
animFrame - > initWithSpriteFrame ( spriteFrame , 1 , NULL ) ;
frames - > addObject ( animFrame ) ;
animFrame - > release ( ) ;
}
if ( frames - > count ( ) = = 0 ) {
2012-03-20 15:04:53 +08:00
CCLOG ( " cocos2d: CCAnimationCache: None of the frames for animation '%s' were found in the CCSpriteFrameCache. Animation is not being added to the Animation Cache. " , pElement - > getStrKey ( ) ) ;
2012-03-16 13:42:53 +08:00
continue ;
} else if ( frames - > count ( ) ! = frameNames - > count ( ) ) {
2012-03-20 15:04:53 +08:00
CCLOG ( " cocos2d: CCAnimationCache: An animation in your dictionary refers to a frame which is not in the CCSpriteFrameCache. Some or all of the frames for the animation '%s' may be missing. " , pElement - > getStrKey ( ) ) ;
2012-03-16 13:42:53 +08:00
}
animation = CCAnimation : : animationWithAnimationFrames ( frames , delay , 1 ) ;
2012-03-20 15:04:53 +08:00
CCAnimationCache : : sharedAnimationCache ( ) - > addAnimation ( animation , pElement - > getStrKey ( ) ) ;
2012-03-16 13:42:53 +08:00
frames - > release ( ) ;
}
}
2012-03-20 15:04:53 +08:00
void CCAnimationCache : : parseVersion2 ( CCDictionary * animations )
2012-03-16 13:42:53 +08:00
{
CCSpriteFrameCache * frameCache = CCSpriteFrameCache : : sharedSpriteFrameCache ( ) ;
2012-03-20 15:04:53 +08:00
CCDictElement * pElement = NULL ;
CCDICT_FOREACH ( animations , pElement )
2012-03-16 13:42:53 +08:00
{
2012-03-20 15:04:53 +08:00
const char * name = pElement - > getStrKey ( ) ;
CCDictionary * animationDict = ( CCDictionary * ) pElement - > getObject ( ) ;
2012-03-16 13:42:53 +08:00
int loops = atoi ( valueForKey ( " loops " , animationDict ) ) ;
bool restoreOriginalFrame = atoi ( valueForKey ( " restoreOriginalFrame " , animationDict ) ) = = 0 ? false : true ;
2012-03-20 15:04:53 +08:00
CCArray * frameArray = ( CCArray * ) animationDict - > objectForKey ( " frames " ) ;
2012-03-16 13:42:53 +08:00
if ( frameArray = = NULL ) {
2012-03-20 15:04:53 +08:00
CCLOG ( " cocos2d: CCAnimationCache: Animation '%s' found in dictionary without any frames - cannot add to animation cache. " , name ) ;
2012-03-16 13:42:53 +08:00
continue ;
}
// Array of AnimationFrames
2012-03-20 15:04:53 +08:00
CCArray * array = CCArray : : arrayWithCapacity ( frameArray - > count ( ) ) ;
array - > retain ( ) ;
2012-03-16 13:42:53 +08:00
2012-03-20 15:04:53 +08:00
CCObject * pObj = NULL ;
CCARRAY_FOREACH ( frameArray , pObj )
2012-03-16 13:42:53 +08:00
{
2012-03-20 15:04:53 +08:00
CCDictionary * entry = ( CCDictionary * ) ( pObj ) ;
2012-03-16 13:42:53 +08:00
const char * spriteFrameName = valueForKey ( " spriteframe " , entry ) ;
CCSpriteFrame * spriteFrame = frameCache - > spriteFrameByName ( spriteFrameName ) ;
if ( ! spriteFrame ) {
2012-03-20 15:04:53 +08:00
CCLOG ( " cocos2d: CCAnimationCache: Animation '%s' refers to frame '%s' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation. " , name , spriteFrameName ) ;
2012-03-16 13:42:53 +08:00
continue ;
}
float delayUnits = ( float ) atof ( valueForKey ( " delayUnits " , entry ) ) ;
2012-03-20 15:04:53 +08:00
CCDictionary * userInfo = ( CCDictionary * ) entry - > objectForKey ( " notification " ) ;
2012-03-16 13:42:53 +08:00
CCAnimationFrame * animFrame = new CCAnimationFrame ( ) ;
animFrame - > initWithSpriteFrame ( spriteFrame , delayUnits , userInfo ) ;
array - > addObject ( animFrame ) ;
animFrame - > release ( ) ;
}
float delayPerUnit = ( float ) atof ( valueForKey ( " delayPerUnit " , animationDict ) ) ;
CCAnimation * animation = new CCAnimation ( ) ;
animation - > initWithAnimationFrames ( array , delayPerUnit , loops ) ;
array - > release ( ) ;
animation - > setRestoreOriginalFrame ( restoreOriginalFrame ) ;
2012-03-20 15:04:53 +08:00
CCAnimationCache : : sharedAnimationCache ( ) - > addAnimation ( animation , name ) ;
2012-03-16 13:42:53 +08:00
animation - > release ( ) ;
}
}
2012-03-20 15:04:53 +08:00
void CCAnimationCache : : addAnimationsWithDictionary ( CCDictionary * dictionary )
2012-03-16 13:42:53 +08:00
{
2012-03-20 15:04:53 +08:00
CCDictionary * animations = ( CCDictionary * ) dictionary - > objectForKey ( " animations " ) ;
2012-03-16 13:42:53 +08:00
if ( animations = = NULL ) {
CCLOG ( " cocos2d: CCAnimationCache: No animations were found in provided dictionary. " ) ;
return ;
}
unsigned int version = 1 ;
2012-03-20 15:04:53 +08:00
CCDictionary * properties = ( CCDictionary * ) dictionary - > objectForKey ( " properties " ) ;
2012-03-16 13:42:53 +08:00
if ( properties )
{
version = atoi ( valueForKey ( " format " , properties ) ) ;
2012-03-23 17:31:28 +08:00
CCArray * spritesheets = ( CCArray * ) properties - > objectForKey ( " spritesheets " ) ;
2012-03-16 13:42:53 +08:00
2012-03-23 17:31:28 +08:00
CCObject * pObj = NULL ;
CCARRAY_FOREACH ( spritesheets , pObj )
{
CCString * name = ( CCString * ) ( pObj ) ;
CCSpriteFrameCache : : sharedSpriteFrameCache ( ) - > addSpriteFramesWithFile ( name - > c_str ( ) ) ;
}
2012-03-16 13:42:53 +08:00
}
switch ( version ) {
case 1 :
parseVersion1 ( animations ) ;
break ;
case 2 :
parseVersion2 ( animations ) ;
break ;
default :
CCAssert ( false , " Invalid animation format " ) ;
}
}
2012-03-20 15:04:53 +08:00
const char * CCAnimationCache : : valueForKey ( const char * key , CCDictionary * dict )
2012-03-16 13:42:53 +08:00
{
if ( dict )
{
2012-03-20 15:04:53 +08:00
CCString * pString = ( CCString * ) dict - > objectForKey ( key ) ;
2012-03-16 13:42:53 +08:00
return pString ? pString - > m_sString . c_str ( ) : " " ;
}
return " " ;
}
/** Read an NSDictionary from a plist file and parse it automatically for animations */
void CCAnimationCache : : addAnimationsWithFile ( const char * plist )
{
CCAssert ( plist , " Invalid texture file name " ) ;
const char * path = CCFileUtils : : fullPathFromRelativePath ( plist ) ;
2012-03-20 15:04:53 +08:00
CCDictionary * dict = CCFileUtils : : dictionaryWithContentsOfFile ( path ) ;
2012-03-16 13:42:53 +08:00
CCAssert ( dict , " CCAnimationCache: File could not be found " ) ;
addAnimationsWithDictionary ( dict ) ;
}
NS_CC_END