2010-07-30 14:10:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "ccMacros.h"
|
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCSpriteFrameCache.h"
|
|
|
|
#include "CCSpriteFrame.h"
|
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "support/TransformUtils.h"
|
2010-08-25 17:21:38 +08:00
|
|
|
#include "CCXFileUtils.h"
|
2010-09-10 14:08:25 +08:00
|
|
|
#include "NSString.h"
|
2010-07-30 14:10:45 +08:00
|
|
|
|
2010-08-04 15:46:12 +08:00
|
|
|
namespace cocos2d {
|
2010-07-30 14:10:45 +08:00
|
|
|
|
|
|
|
static CCSpriteFrameCache *pSharedSpriteFrameCache = NULL;
|
|
|
|
|
|
|
|
CCSpriteFrameCache* CCSpriteFrameCache::sharedSpriteFrameCache(void)
|
|
|
|
{
|
|
|
|
if (! pSharedSpriteFrameCache)
|
|
|
|
{
|
|
|
|
pSharedSpriteFrameCache = new CCSpriteFrameCache();
|
|
|
|
pSharedSpriteFrameCache->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pSharedSpriteFrameCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::purgeSharedSpriteFrameCache(void)
|
|
|
|
{
|
2010-09-01 15:36:25 +08:00
|
|
|
CCX_SAFE_RELEASE_NULL(pSharedSpriteFrameCache);
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCSpriteFrameCache::init(void)
|
2010-07-30 14:10:45 +08:00
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames= new NSDictionary<std::string, CCSpriteFrame*>();
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSpriteFrameCache::~CCSpriteFrameCache(void)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames->release();
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
2010-09-10 14:08:25 +08:00
|
|
|
void CCSpriteFrameCache::addSpriteFramesWithDictionary(NSDictionary<std::string, NSObject*> *dictionary, CCTexture2D *pobTexture)
|
2010-07-30 14:10:45 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Supported Zwoptex Formats:
|
|
|
|
enum {
|
|
|
|
ZWTCoordinatesListXMLFormat_Legacy = 0
|
|
|
|
ZWTCoordinatesListXMLFormat_v1_0,
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2010-09-10 14:08:25 +08:00
|
|
|
NSDictionary<std::string, NSObject*> *metadataDict = (NSDictionary<std::string, NSObject*>*)dictionary->objectForKey(std::string("metadata"));
|
|
|
|
NSDictionary<std::string, NSObject*> *framesDict = (NSDictionary<std::string, NSObject*>*)dictionary->objectForKey(std::string("frames"));
|
|
|
|
int format = 0;
|
|
|
|
|
|
|
|
// get the format
|
|
|
|
if(metadataDict != NULL)
|
|
|
|
{
|
|
|
|
format = atoi(valueForKey("format", metadataDict));
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the format
|
|
|
|
if(format < 0 || format > 1)
|
|
|
|
{
|
|
|
|
NSAssert(0, "cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
framesDict->begin();
|
|
|
|
std::string key = "";
|
|
|
|
NSDictionary<std::string, NSObject*> *frameDict = NULL;
|
|
|
|
while( frameDict = (NSDictionary<std::string, NSObject*>*)framesDict->next(&key) )
|
|
|
|
{
|
|
|
|
CCSpriteFrame *spriteFrame = m_pSpriteFrames->objectForKey(key);
|
|
|
|
if (spriteFrame)
|
2010-07-30 14:10:45 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-09-10 14:08:25 +08:00
|
|
|
|
|
|
|
if(format == 0)
|
|
|
|
{
|
|
|
|
float x = (float)atof(valueForKey("x", frameDict));
|
|
|
|
float y = (float)atof(valueForKey("y", frameDict));
|
|
|
|
float w = (float)atof(valueForKey("width", frameDict));
|
|
|
|
float h = (float)atof(valueForKey("height", frameDict));
|
|
|
|
float ox = (float)atof(valueForKey("offsetX", frameDict));
|
|
|
|
float oy = (float)atof(valueForKey("offsetY", frameDict));
|
|
|
|
int ow = atoi(valueForKey("originalWidth", frameDict));
|
|
|
|
int oh = atoi(valueForKey("originalHeight", frameDict));
|
|
|
|
// check ow/oh
|
|
|
|
if(!ow || !oh)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
|
|
|
|
}
|
|
|
|
// abs ow/oh
|
|
|
|
ow = abs(ow);
|
|
|
|
oh = abs(oh);
|
|
|
|
// create frame
|
|
|
|
spriteFrame = CCSpriteFrame::frameWithTexture(pobTexture, CGRectMake(x, y, w, h), CGPointMake(ox, oy), CGSizeMake((float)ow, (float)oh));
|
|
|
|
}
|
|
|
|
else if(format == 1)
|
|
|
|
{
|
|
|
|
/** @todo
|
2010-07-30 14:10:45 +08:00
|
|
|
CGRect frame = CGRectFromString([frameDict objectForKey:@"frame"]);
|
|
|
|
CGPoint offset = CGPointFromString([frameDict objectForKey:@"offset"]);
|
|
|
|
CGSize sourceSize = CGSizeFromString([frameDict objectForKey:@"sourceSize"]);
|
|
|
|
|
|
|
|
// create frame
|
2010-09-10 14:08:25 +08:00
|
|
|
spriteFrame = [CCSpriteFrame frameWithTexture:texture rect:frame offset:offset originalSize:sourceSize];
|
|
|
|
*/
|
|
|
|
}
|
2010-09-24 18:10:32 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Unsupported Zwoptex version. Update cocos2d.");
|
|
|
|
}
|
2010-09-10 14:08:25 +08:00
|
|
|
// add sprite frame
|
|
|
|
m_pSpriteFrames->setObject(spriteFrame, key);
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist, CCTexture2D *pobTexture)
|
|
|
|
{
|
2010-09-07 16:30:31 +08:00
|
|
|
const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist);
|
2010-09-10 14:08:25 +08:00
|
|
|
NSDictionary<std::string, NSObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath);
|
2010-08-03 13:52:41 +08:00
|
|
|
|
|
|
|
return addSpriteFramesWithDictionary(dict, pobTexture);
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist)
|
|
|
|
{
|
2010-09-07 16:30:31 +08:00
|
|
|
const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist);
|
2010-09-10 14:08:25 +08:00
|
|
|
NSDictionary<std::string, NSObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath);
|
2010-08-03 13:52:41 +08:00
|
|
|
|
|
|
|
string texturePath = string(pszPlist);
|
|
|
|
|
|
|
|
// remove .xxx
|
|
|
|
size_t startPos = texturePath.find_last_of(".");
|
|
|
|
texturePath = texturePath.erase(startPos);
|
|
|
|
|
|
|
|
// append .png
|
|
|
|
texturePath = texturePath.append(".png");
|
|
|
|
|
|
|
|
CCTexture2D *pTexture = CCTextureCache::sharedTextureCache()->addImage(texturePath.c_str());
|
|
|
|
|
|
|
|
return addSpriteFramesWithDictionary(dict, pTexture);
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames->setObject(pobFrame, std::string(pszFrameName));
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::removeSpriteFrames(void)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames->removeAllObjects();
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::removeUnusedSpriteFrames(void)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames->begin();
|
|
|
|
std::string key = "";
|
|
|
|
CCSpriteFrame *spriteFrame = NULL;
|
|
|
|
while( spriteFrame = m_pSpriteFrames->next(&key) )
|
|
|
|
{
|
|
|
|
if( spriteFrame->retainCount() == 1 )
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCSpriteFrameCache: removing unused frame: %s", key.c_str());
|
|
|
|
m_pSpriteFrames->removeObjectForKey(key);
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames->end();
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCSpriteFrameCache::removeSpriteFrameByName(const char *pszName)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
m_pSpriteFrames->removeObjectForKey(std::string(pszName));
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCSpriteFrame* CCSpriteFrameCache::spriteFrameByName(const char *pszName)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
CCSpriteFrame *frame = m_pSpriteFrames->objectForKey(std::string(pszName));
|
|
|
|
if( ! frame )
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCSpriteFrameCache: Frame '%s' not found", pszName);
|
|
|
|
}
|
|
|
|
return frame;
|
2010-07-30 14:10:45 +08:00
|
|
|
}
|
2010-08-03 13:52:41 +08:00
|
|
|
|
|
|
|
CCSprite* CCSpriteFrameCache::createSpriteWithFrameName(const char *pszName)
|
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
CCSpriteFrame *frame = m_pSpriteFrames->objectForKey(std::string(pszName));
|
|
|
|
return CCSprite::spriteWithSpriteFrame(frame);
|
|
|
|
}
|
|
|
|
const char * CCSpriteFrameCache::valueForKey(const char *key, NSDictionary<std::string, NSObject*> *dict)
|
|
|
|
{
|
|
|
|
if (dict)
|
|
|
|
{
|
|
|
|
NSString *pString = (NSString*)dict->objectForKey(std::string(key));
|
|
|
|
return pString ? pString->m_sString.c_str() : "";
|
|
|
|
}
|
|
|
|
return "";
|
2010-08-03 13:52:41 +08:00
|
|
|
}
|
2010-08-04 15:46:12 +08:00
|
|
|
}//namespace cocos2d
|