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-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2014-08-28 17:03:29 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2015-09-29 21:42:04 +08:00
|
|
|
#include "2d/CCAutoPolygon.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/CCNS.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2015-05-21 16:04:37 +08:00
|
|
|
#include "base/CCNinePatchImageParser.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_sharedSpriteFrameCache = new (std::nothrow) SpriteFrameCache();
|
2013-07-12 06:24:23 +08:00
|
|
|
_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);
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
bool SpriteFrameCache::init()
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
SpriteFrameCache::~SpriteFrameCache()
|
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
|
|
|
}
|
|
|
|
|
2015-10-26 16:36:01 +08:00
|
|
|
void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector<int> &res)
|
2015-09-29 21:42:04 +08:00
|
|
|
{
|
|
|
|
std::string delim(" ");
|
|
|
|
|
|
|
|
size_t n = std::count(string.begin(), string.end(), ' ');
|
|
|
|
res.resize(n+1);
|
|
|
|
|
|
|
|
size_t start = 0U;
|
|
|
|
size_t end = string.find(delim);
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
while (end != std::string::npos)
|
|
|
|
{
|
2015-09-30 21:20:53 +08:00
|
|
|
res[i++] = atoi(string.substr(start, end - start).c_str());
|
2015-09-29 21:42:04 +08:00
|
|
|
start = end + delim.length();
|
|
|
|
end = string.find(delim, start);
|
|
|
|
}
|
|
|
|
|
2015-09-30 21:20:53 +08:00
|
|
|
res[i] = atoi(string.substr(start, end).c_str());
|
2015-09-29 21:42:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCache::initializePolygonInfo(const Size &textureSize,
|
|
|
|
const Size &spriteSize,
|
|
|
|
const std::vector<int> &vertices,
|
|
|
|
const std::vector<int> &verticesUV,
|
|
|
|
const std::vector<int> &triangleIndices,
|
|
|
|
PolygonInfo &info)
|
|
|
|
{
|
|
|
|
size_t vertexCount = vertices.size();
|
|
|
|
size_t indexCount = triangleIndices.size();
|
|
|
|
|
|
|
|
float scaleFactor = CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
|
|
|
V3F_C4B_T2F *vertexData = new V3F_C4B_T2F[vertexCount];
|
|
|
|
for (size_t i = 0; i < vertexCount/2; i++)
|
|
|
|
{
|
|
|
|
vertexData[i].colors = Color4B::WHITE;
|
|
|
|
vertexData[i].vertices = Vec3(vertices[i*2] / scaleFactor,
|
|
|
|
(spriteSize.height - vertices[i*2+1]) / scaleFactor,
|
|
|
|
0);
|
|
|
|
vertexData[i].texCoords = Tex2F(verticesUV[i*2] / textureSize.width,
|
|
|
|
verticesUV[i*2+1] / textureSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short *indexData = new unsigned short[indexCount];
|
|
|
|
for (size_t i = 0; i < indexCount; i++)
|
|
|
|
{
|
|
|
|
indexData[i] = static_cast<unsigned short>(triangleIndices[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.triangles.vertCount = vertexCount;
|
|
|
|
info.triangles.verts = vertexData;
|
|
|
|
info.triangles.indexCount = indexCount;
|
|
|
|
info.triangles.indices = indexData;
|
|
|
|
info.rect = Rect(0, 0, spriteSize.width, spriteSize.height);
|
|
|
|
}
|
|
|
|
|
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+
|
2015-09-29 21:42:04 +08:00
|
|
|
|
|
|
|
Version 3 with TexturePacker 4.0 polygon mesh packing
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
|
2015-11-12 09:49:49 +08:00
|
|
|
#ifdef CC_STUDIO_ENABLED_VIEW // for cocostudio only
|
|
|
|
if (dictionary["frames"].getType() != cocos2d::Value::Type::MAP)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap& framesDict = dictionary["frames"].asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
int format = 0;
|
|
|
|
|
2015-09-29 21:42:04 +08:00
|
|
|
Size textureSize;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// 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();
|
2015-09-29 21:42:04 +08:00
|
|
|
|
|
|
|
if(metadataDict.find("size") != metadataDict.end())
|
|
|
|
{
|
|
|
|
textureSize = SizeFromString(metadataDict["size"].asString());
|
|
|
|
}
|
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
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
auto textureFileName = Director::getInstance()->getTextureCache()->getTextureFilePath(texture);
|
2015-10-03 02:18:07 +08:00
|
|
|
Image* image = nullptr;
|
2015-05-26 17:18:09 +08:00
|
|
|
NinePatchImageParser parser;
|
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)
|
|
|
|
{
|
2015-09-09 11:37:41 +08:00
|
|
|
CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// abs ow/oh
|
|
|
|
ow = abs(ow);
|
|
|
|
oh = abs(oh);
|
|
|
|
// create frame
|
2014-06-10 12:04:20 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture,
|
|
|
|
Rect(x, y, w, h),
|
|
|
|
false,
|
|
|
|
Vec2(ox, oy),
|
|
|
|
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-05-15 01:07:09 +08:00
|
|
|
Vec2 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
|
2014-06-10 12:04:20 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture,
|
|
|
|
frame,
|
|
|
|
rotated,
|
|
|
|
offset,
|
|
|
|
sourceSize
|
|
|
|
);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else if (format == 3)
|
|
|
|
{
|
|
|
|
// get values
|
2013-12-03 16:20:41 +08:00
|
|
|
Size spriteSize = SizeFromString(frameDict["spriteSize"].asString());
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 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
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2011-03-29 11:56:14 +08:00
|
|
|
// create frame
|
2014-06-10 12:04:20 +08:00
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture,
|
|
|
|
Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
|
|
|
|
textureRotated,
|
|
|
|
spriteOffset,
|
|
|
|
spriteSourceSize);
|
2015-09-29 21:42:04 +08:00
|
|
|
|
|
|
|
if(frameDict.find("vertices") != frameDict.end())
|
|
|
|
{
|
|
|
|
std::vector<int> vertices;
|
|
|
|
parseIntegerList(frameDict["vertices"].asString(), vertices);
|
|
|
|
std::vector<int> verticesUV;
|
|
|
|
parseIntegerList(frameDict["verticesUV"].asString(), verticesUV);
|
|
|
|
std::vector<int> indices;
|
|
|
|
parseIntegerList(frameDict["triangles"].asString(), indices);
|
|
|
|
|
|
|
|
PolygonInfo info;
|
|
|
|
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);
|
|
|
|
spriteFrame->setPolygonInfo(info);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-21 16:04:37 +08:00
|
|
|
bool flag = NinePatchImageParser::isNinePatchImage(spriteFrameName);
|
|
|
|
if(flag)
|
|
|
|
{
|
2015-10-03 02:18:07 +08:00
|
|
|
if (image == nullptr) {
|
|
|
|
image = new Image();
|
|
|
|
image->initWithImageFile(textureFileName);
|
|
|
|
}
|
2015-05-26 17:18:09 +08:00
|
|
|
parser.setSpriteFrameInfo(image, spriteFrame->getRectInPixels(), spriteFrame->isRotated());
|
2015-05-21 16:04:37 +08:00
|
|
|
texture->addSpriteFrameCapInset(spriteFrame, parser.parseCapInset());
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
// add sprite frame
|
2013-12-05 10:59:43 +08:00
|
|
|
_spriteFrames.insert(spriteFrameName, spriteFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2015-05-21 16:04:37 +08:00
|
|
|
CC_SAFE_DELETE(image);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, Texture2D *texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-30 15:13:59 +08:00
|
|
|
if (_loadedFileNames->find(plist) != _loadedFileNames->end())
|
2014-05-30 15:10:57 +08:00
|
|
|
{
|
|
|
|
return; // We already added it
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
addSpriteFramesWithDictionary(dict, texture);
|
|
|
|
_loadedFileNames->insert(plist);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:58:33 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFileContent(const std::string& plist_content, Texture2D *texture)
|
|
|
|
{
|
2014-08-21 16:21:23 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromData(plist_content.c_str(), static_cast<int>(plist_content.size()));
|
2014-07-29 17:22:13 +08:00
|
|
|
addSpriteFramesWithDictionary(dict, texture);
|
2014-07-29 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-30 15:13:59 +08:00
|
|
|
CCASSERT(plist.size()>0, "plist filename should not be nullptr");
|
2015-01-21 14:15:30 +08:00
|
|
|
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
|
|
|
|
if (fullPath.size() == 0)
|
|
|
|
{
|
|
|
|
// return if plist file doesn't exist
|
|
|
|
CCLOG("cocos2d: SpriteFrameCache: can not find %s", plist.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-30 15:13:59 +08:00
|
|
|
if (_loadedFileNames->find(plist) == _loadedFileNames->end())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-01-21 14:15:30 +08:00
|
|
|
|
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
|
2014-05-30 15:13:59 +08:00
|
|
|
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath.c_str(), plist);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// build texture path by replacing file extension
|
2014-05-30 15:13:59 +08:00
|
|
|
texturePath = plist;
|
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);
|
2014-05-30 15:13:59 +08:00
|
|
|
_loadedFileNames->insert(plist);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 11:40:15 +08:00
|
|
|
bool SpriteFrameCache::isSpriteFramesWithFileLoaded(const std::string& plist) const
|
2015-04-23 16:50:28 +08:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (_loadedFileNames->find(plist) != _loadedFileNames->end())
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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);
|
2015-06-10 19:37:52 +08:00
|
|
|
spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame);
|
2013-12-03 16:20:41 +08:00
|
|
|
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);
|
2015-05-21 16:04:37 +08:00
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:. 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
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:. 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
|
|
|
}
|
|
|
|
|
2014-07-29 16:58:33 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromFileContent(const std::string& plist_content)
|
|
|
|
{
|
2014-08-21 16:21:23 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromData(plist_content.data(), static_cast<int>(plist_content.size()));
|
2014-07-29 17:06:43 +08:00
|
|
|
if (dict.empty())
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFileContent: create dict by fail.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
removeSpriteFramesFromDictionary(dict);
|
2014-07-29 16:58:33 +08:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-11-12 09:49:49 +08:00
|
|
|
#ifdef CC_STUDIO_ENABLED_VIEW // for cocostudio only
|
|
|
|
if (dictionary["frames"].getType() != cocos2d::Value::Type::MAP)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-12 09:49:49 +08:00
|
|
|
#ifdef CC_STUDIO_ENABLED_VIEW // for cocostudio only
|
|
|
|
void SpriteFrameCache::reloadSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D *texture)
|
|
|
|
{
|
|
|
|
ValueMap& framesDict = dictionary["frames"].asValueMap();
|
|
|
|
int format = 0;
|
|
|
|
|
|
|
|
// get the format
|
|
|
|
if (dictionary.find("metadata") != dictionary.end())
|
|
|
|
{
|
|
|
|
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
|
|
|
|
format = metadataDict["format"].asInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the format
|
|
|
|
CCASSERT(format >= 0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
|
|
|
|
|
|
|
|
for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)
|
|
|
|
{
|
|
|
|
ValueMap& frameDict = iter->second.asValueMap();
|
|
|
|
std::string spriteFrameName = iter->first;
|
|
|
|
|
|
|
|
auto it = _spriteFrames.find(spriteFrameName);
|
|
|
|
if (it != _spriteFrames.end())
|
|
|
|
{
|
|
|
|
_spriteFrames.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
SpriteFrame* spriteFrame = nullptr;
|
|
|
|
|
|
|
|
if (format == 0)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
// check ow/oh
|
|
|
|
if (!ow || !oh)
|
|
|
|
{
|
|
|
|
CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
|
|
|
|
}
|
|
|
|
// abs ow/oh
|
|
|
|
ow = abs(ow);
|
|
|
|
oh = abs(oh);
|
|
|
|
// create frame
|
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture,
|
|
|
|
Rect(x, y, w, h),
|
|
|
|
false,
|
|
|
|
Vec2(ox, oy),
|
|
|
|
Size((float)ow, (float)oh)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (format == 1 || format == 2)
|
|
|
|
{
|
|
|
|
Rect frame = RectFromString(frameDict["frame"].asString());
|
|
|
|
bool rotated = false;
|
|
|
|
|
|
|
|
// rotation
|
|
|
|
if (format == 2)
|
|
|
|
{
|
|
|
|
rotated = frameDict["rotated"].asBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 offset = PointFromString(frameDict["offset"].asString());
|
|
|
|
Size sourceSize = SizeFromString(frameDict["sourceSize"].asString());
|
|
|
|
|
|
|
|
// create frame
|
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture,
|
|
|
|
frame,
|
|
|
|
rotated,
|
|
|
|
offset,
|
|
|
|
sourceSize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (format == 3)
|
|
|
|
{
|
|
|
|
// get values
|
|
|
|
Size spriteSize = SizeFromString(frameDict["spriteSize"].asString());
|
|
|
|
Vec2 spriteOffset = PointFromString(frameDict["spriteOffset"].asString());
|
|
|
|
Size spriteSourceSize = SizeFromString(frameDict["spriteSourceSize"].asString());
|
|
|
|
Rect textureRect = RectFromString(frameDict["textureRect"].asString());
|
|
|
|
bool textureRotated = frameDict["textureRotated"].asBool();
|
|
|
|
|
|
|
|
// get aliases
|
|
|
|
ValueVector& aliases = frameDict["aliases"].asValueVector();
|
|
|
|
|
|
|
|
for (const auto &value : aliases) {
|
|
|
|
std::string oneAlias = value.asString();
|
|
|
|
if (_spriteFramesAliases.find(oneAlias) != _spriteFramesAliases.end())
|
|
|
|
{
|
|
|
|
CCLOGWARN("cocos2d: WARNING: an alias with name %s already exists", oneAlias.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
_spriteFramesAliases[oneAlias] = Value(spriteFrameName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create frame
|
|
|
|
spriteFrame = SpriteFrame::createWithTexture(texture,
|
|
|
|
Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
|
|
|
|
textureRotated,
|
|
|
|
spriteOffset,
|
|
|
|
spriteSourceSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add sprite frame
|
|
|
|
_spriteFrames.insert(spriteFrameName, spriteFrame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteFrameCache::reloadTexture(const std::string& plist)
|
|
|
|
{
|
|
|
|
CCASSERT(plist.size()>0, "plist filename should not be nullptr");
|
|
|
|
|
|
|
|
auto it = _loadedFileNames->find(plist);
|
|
|
|
if (it != _loadedFileNames->end()) {
|
|
|
|
_loadedFileNames->erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//If one plist has't be loaded, we don't load it here.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
|
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
|
|
|
|
|
|
|
string texturePath("");
|
|
|
|
|
|
|
|
if (dict.find("metadata") != dict.end())
|
|
|
|
{
|
|
|
|
ValueMap& metadataDict = dict["metadata"].asValueMap();
|
|
|
|
// try to read texture file name from meta data
|
|
|
|
texturePath = metadataDict["textureFileName"].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!texturePath.empty())
|
|
|
|
{
|
|
|
|
// build texture path relative to plist file
|
|
|
|
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath.c_str(), plist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// build texture path by replacing file extension
|
|
|
|
texturePath = plist;
|
|
|
|
|
|
|
|
// remove .xxx
|
|
|
|
size_t startPos = texturePath.find_last_of(".");
|
|
|
|
texturePath = texturePath.erase(startPos);
|
|
|
|
|
|
|
|
// append .png
|
|
|
|
texturePath = texturePath.append(".png");
|
|
|
|
|
|
|
|
CCLOG("cocos2d: SpriteFrameCache: Trying to use file %s as texture", texturePath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D *texture = nullptr;
|
|
|
|
if (Director::getInstance()->getTextureCache()->reloadTexture(texturePath.c_str()))
|
|
|
|
texture = Director::getInstance()->getTextureCache()->getTextureForKey(texturePath);
|
|
|
|
|
|
|
|
if (texture)
|
|
|
|
{
|
|
|
|
reloadSpriteFramesWithDictionary(dict, texture);
|
|
|
|
_loadedFileNames->insert(plist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|