2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Jason Booth
|
|
|
|
Copyright (c) 2009 Robert J Payne
|
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 "base/CCNS.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCTextureCache.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
|
|
|
#include "2d/CCSpriteFrame.h"
|
|
|
|
#include "2d/CCSprite.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "math/TransformUtils.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/platform/CCFileUtils.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
static SpriteFrameCache *_sharedSpriteFrameCache = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
SpriteFrameCache* SpriteFrameCache::getInstance()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
if (! _sharedSpriteFrameCache)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
_sharedSpriteFrameCache = new SpriteFrameCache();
|
|
|
|
_sharedSpriteFrameCache->init();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
return _sharedSpriteFrameCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_sharedSpriteFrameCache);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteFrameCache::init(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
_spriteFrames.reserve(20);
|
2013-12-03 16:20:41 +08:00
|
|
|
_spriteFramesAliases.reserve(20);
|
2013-06-15 14:03:30 +08:00
|
|
|
_loadedFileNames = new std::set<std::string>();
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrameCache::~SpriteFrameCache(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_loadedFileNames);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D* texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Supported Zwoptex Formats:
|
|
|
|
|
|
|
|
ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version
|
|
|
|
ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b
|
|
|
|
ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1
|
|
|
|
ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+
|
|
|
|
*/
|
|
|
|
|
2013-12-06 16:46:19 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap& framesDict = dictionary["frames"].asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
int format = 0;
|
|
|
|
|
|
|
|
// get the format
|
2013-12-06 16:46:19 +08:00
|
|
|
if (dictionary.find("metadata") != dictionary.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-06 16:46:19 +08:00
|
|
|
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
|
2013-12-03 16:20:41 +08:00
|
|
|
format = metadataDict["format"].asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the format
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(format >=0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap& frameDict = iter->second.asValueMap();
|
2013-12-03 16:20:41 +08:00
|
|
|
std::string spriteFrameName = iter->first;
|
2013-12-05 10:59:43 +08:00
|
|
|
SpriteFrame* spriteFrame = _spriteFrames.at(spriteFrameName);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (spriteFrame)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(format == 0)
|
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
float x = frameDict["x"].asFloat();
|
|
|
|
float y = frameDict["y"].asFloat();
|
|
|
|
float w = frameDict["width"].asFloat();
|
|
|
|
float h = frameDict["height"].asFloat();
|
|
|
|
float ox = frameDict["offsetX"].asFloat();
|
|
|
|
float oy = frameDict["offsetY"].asFloat();
|
|
|
|
int ow = frameDict["originalWidth"].asInt();
|
|
|
|
int oh = frameDict["originalHeight"].asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
// check ow/oh
|
|
|
|
if(!ow || !oh)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// abs ow/oh
|
|
|
|
ow = abs(ow);
|
|
|
|
oh = abs(oh);
|
|
|
|
// create frame
|
2013-06-20 14:13:12 +08:00
|
|
|
spriteFrame = new SpriteFrame();
|
2013-12-03 16:20:41 +08:00
|
|
|
spriteFrame->initWithTexture(texture,
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect(x, y, w, h),
|
2012-04-19 14:35:52 +08:00
|
|
|
false,
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2(ox, oy),
|
2013-07-12 14:30:26 +08:00
|
|
|
Size((float)ow, (float)oh)
|
2012-04-19 14:35:52 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else if(format == 1 || format == 2)
|
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
Rect frame = RectFromString(frameDict["frame"].asString());
|
2012-04-19 14:35:52 +08:00
|
|
|
bool rotated = false;
|
|
|
|
|
|
|
|
// rotation
|
|
|
|
if (format == 2)
|
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
rotated = frameDict["rotated"].asBool();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 offset = PointFromString(frameDict["offset"].asString());
|
2013-12-03 16:20:41 +08:00
|
|
|
Size sourceSize = SizeFromString(frameDict["sourceSize"].asString());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// create frame
|
2013-06-20 14:13:12 +08:00
|
|
|
spriteFrame = new SpriteFrame();
|
2013-12-03 16:20:41 +08:00
|
|
|
spriteFrame->initWithTexture(texture,
|
2012-04-19 14:35:52 +08:00
|
|
|
frame,
|
|
|
|
rotated,
|
|
|
|
offset,
|
|
|
|
sourceSize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (format == 3)
|
|
|
|
{
|
|
|
|
// get values
|
2013-12-03 16:20:41 +08:00
|
|
|
Size spriteSize = SizeFromString(frameDict["spriteSize"].asString());
|
2014-04-15 18:13:57 +08:00
|
|
|
Vector2 spriteOffset = PointFromString(frameDict["spriteOffset"].asString());
|
2013-12-03 16:20:41 +08:00
|
|
|
Size spriteSourceSize = SizeFromString(frameDict["spriteSourceSize"].asString());
|
|
|
|
Rect textureRect = RectFromString(frameDict["textureRect"].asString());
|
|
|
|
bool textureRotated = frameDict["textureRotated"].asBool();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// get aliases
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector& aliases = frameDict["aliases"].asValueVector();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &value : aliases) {
|
2013-12-03 16:20:41 +08:00
|
|
|
std::string oneAlias = value.asString();
|
|
|
|
if (_spriteFramesAliases.find(oneAlias) != _spriteFramesAliases.end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-08 14:11:48 +08:00
|
|
|
CCLOGWARN("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
_spriteFramesAliases[oneAlias] = Value(spriteFrameName);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2013-12-03 16:20:41 +08:00
|
|
|
|
2011-03-29 11:56:14 +08:00
|
|
|
// create frame
|
2013-06-20 14:13:12 +08:00
|
|
|
spriteFrame = new SpriteFrame();
|
2013-12-03 16:20:41 +08:00
|
|
|
spriteFrame->initWithTexture(texture,
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
|
2011-03-29 11:56:14 +08:00
|
|
|
textureRotated,
|
|
|
|
spriteOffset,
|
2012-04-19 14:35:52 +08:00
|
|
|
spriteSourceSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add sprite frame
|
2013-12-05 10:59:43 +08:00
|
|
|
_spriteFrames.insert(spriteFrameName, spriteFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
spriteFrame->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& pszPlist, Texture2D *pobTexture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszPlist);
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
addSpriteFramesWithDictionary(dict, pobTexture);
|
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-06 11:02:03 +08:00
|
|
|
CCASSERT(textureFileName.size()>0, "texture name should not be null");
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(textureFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
addSpriteFramesWithFile(plist, texture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-06 11:02:03 +08:00
|
|
|
CCLOG("cocos2d: SpriteFrameCache: couldn't load texture file. File not found %s", textureFileName.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& pszPlist)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT(pszPlist.size()>0, "plist filename should not be nullptr");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_loadedFileNames->find(pszPlist) == _loadedFileNames->end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszPlist);
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
string texturePath("");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-06 16:46:19 +08:00
|
|
|
if (dict.find("metadata") != dict.end())
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-12-06 16:46:19 +08:00
|
|
|
ValueMap& metadataDict = dict["metadata"].asValueMap();
|
2012-06-08 14:11:48 +08:00
|
|
|
// try to read texture file name from meta data
|
2013-12-03 16:20:41 +08:00
|
|
|
texturePath = metadataDict["textureFileName"].asString();
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
if (!texturePath.empty())
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-01-25 20:51:52 +08:00
|
|
|
// build texture path relative to plist file
|
2013-07-12 06:24:23 +08:00
|
|
|
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath.c_str(), pszPlist);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// build texture path by replacing file extension
|
2013-01-18 21:54:48 +08:00
|
|
|
texturePath = pszPlist;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
// remove .xxx
|
|
|
|
size_t startPos = texturePath.find_last_of(".");
|
|
|
|
texturePath = texturePath.erase(startPos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
// append .png
|
|
|
|
texturePath = texturePath.append(".png");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: SpriteFrameCache: Trying to use file %s as texture", texturePath.c_str());
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(texturePath.c_str());
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
if (texture)
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
addSpriteFramesWithDictionary(dict, texture);
|
2013-06-15 14:03:30 +08:00
|
|
|
_loadedFileNames->insert(pszPlist);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 10:59:43 +08:00
|
|
|
void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, const std::string& frameName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 10:59:43 +08:00
|
|
|
_spriteFrames.insert(frameName, frame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFrames()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 10:59:43 +08:00
|
|
|
_spriteFrames.clear();
|
2013-12-03 16:20:41 +08:00
|
|
|
_spriteFramesAliases.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_loadedFileNames->clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
void SpriteFrameCache::removeUnusedSpriteFrames()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool removed = false;
|
2013-12-03 16:20:41 +08:00
|
|
|
std::vector<std::string> toRemoveFrames;
|
|
|
|
|
|
|
|
for (auto iter = _spriteFrames.begin(); iter != _spriteFrames.end(); ++iter)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
SpriteFrame* spriteFrame = iter->second;
|
2014-01-22 13:47:29 +08:00
|
|
|
if( spriteFrame->getReferenceCount() == 1 )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
toRemoveFrames.push_back(iter->first);
|
|
|
|
CCLOG("cocos2d: SpriteFrameCache: removing unused frame: %s", iter->first.c_str());
|
2013-12-18 17:47:20 +08:00
|
|
|
removed = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
_spriteFrames.erase(toRemoveFrames);
|
2013-12-03 16:20:41 +08:00
|
|
|
|
2012-06-12 01:43:07 +08:00
|
|
|
// XXX. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache
|
2013-12-18 17:47:20 +08:00
|
|
|
if( removed )
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loadedFileNames->clear();
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFrameByName(const std::string& name)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// explicit nil handling
|
2013-11-20 10:39:30 +08:00
|
|
|
if( !(name.size()>0) )
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Is this an alias ?
|
2013-12-03 16:20:41 +08:00
|
|
|
std::string key = _spriteFramesAliases[name].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
if (!key.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
_spriteFrames.erase(key);
|
2013-12-03 16:20:41 +08:00
|
|
|
_spriteFramesAliases.erase(key);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
_spriteFrames.erase(name);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2012-06-12 01:43:07 +08:00
|
|
|
// XXX. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache
|
2013-06-15 14:03:30 +08:00
|
|
|
_loadedFileNames->clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromFile(const std::string& plist)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2013-12-03 16:20:41 +08:00
|
|
|
if (dict.empty())
|
2013-10-10 10:28:49 +08:00
|
|
|
{
|
2013-11-06 11:02:03 +08:00
|
|
|
CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFile: create dict by %s fail.",plist.c_str());
|
2013-10-10 10:28:49 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-12-03 16:20:41 +08:00
|
|
|
removeSpriteFramesFromDictionary(dict);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-12 01:43:07 +08:00
|
|
|
// remove it from the cache
|
2013-06-15 14:03:30 +08:00
|
|
|
set<string>::iterator ret = _loadedFileNames->find(plist);
|
|
|
|
if (ret != _loadedFileNames->end())
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_loadedFileNames->erase(ret);
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap framesDict = dictionary["frames"].asValueMap();
|
2013-12-03 16:20:41 +08:00
|
|
|
std::vector<std::string> keysToRemove;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
for (auto iter = framesDict.cbegin(); iter != framesDict.cend(); ++iter)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 10:59:43 +08:00
|
|
|
if (_spriteFrames.at(iter->first))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
keysToRemove.push_back(iter->first);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
_spriteFrames.erase(keysToRemove);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
std::vector<std::string> keysToRemove;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-03 16:20:41 +08:00
|
|
|
for (auto iter = _spriteFrames.cbegin(); iter != _spriteFrames.cend(); ++iter)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
std::string key = iter->first;
|
2013-12-05 10:59:43 +08:00
|
|
|
SpriteFrame* frame = _spriteFrames.at(key);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (frame && (frame->getTexture() == texture))
|
|
|
|
{
|
2013-12-03 16:20:41 +08:00
|
|
|
keysToRemove.push_back(key);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
_spriteFrames.erase(keysToRemove);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 11:02:03 +08:00
|
|
|
SpriteFrame* SpriteFrameCache::getSpriteFrameByName(const std::string& name)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 10:59:43 +08:00
|
|
|
SpriteFrame* frame = _spriteFrames.at(name);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
// try alias dictionary
|
2013-12-03 16:20:41 +08:00
|
|
|
std::string key = _spriteFramesAliases[name].asString();
|
|
|
|
if (!key.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-05 10:59:43 +08:00
|
|
|
frame = _spriteFrames.at(key);
|
2013-12-03 16:20:41 +08:00
|
|
|
if (!frame)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-06 11:02:03 +08:00
|
|
|
CCLOG("cocos2d: SpriteFrameCache: Frame '%s' not found", name.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2013-12-05 11:44:37 +08:00
|
|
|
|