axmol/cocos/2d/CCSpriteFrameCache.cpp

766 lines
26 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2009 Jason Booth
Copyright (c) 2009 Robert J Payne
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
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 "2d/CCSpriteFrameCache.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include <vector>
2014-08-28 17:03:29 +08:00
#include "2d/CCSprite.h"
#include "2d/CCAutoPolygon.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
#include "base/CCNS.h"
#include "base/ccMacros.h"
#include "base/ccUTF8.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/CCDirector.h"
2014-08-28 17:03:29 +08:00
#include "renderer/CCTexture2D.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "renderer/CCTextureCache.h"
#include "base/CCNinePatchImageParser.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
using namespace std;
NS_CC_BEGIN
static SpriteFrameCache *_sharedSpriteFrameCache = nullptr;
SpriteFrameCache* SpriteFrameCache::getInstance()
{
if (! _sharedSpriteFrameCache)
{
_sharedSpriteFrameCache = new (std::nothrow) SpriteFrameCache();
_sharedSpriteFrameCache->init();
}
return _sharedSpriteFrameCache;
}
void SpriteFrameCache::destroyInstance()
{
CC_SAFE_RELEASE_NULL(_sharedSpriteFrameCache);
}
2014-05-30 15:13:59 +08:00
bool SpriteFrameCache::init()
{
_spriteFrames.reserve(20);
_spriteFramesAliases.reserve(20);
_loadedFileNames = new std::set<std::string>();
return true;
}
2014-05-30 15:13:59 +08:00
SpriteFrameCache::~SpriteFrameCache()
{
CC_SAFE_DELETE(_loadedFileNames);
}
void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector<int> &res)
{
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)
{
res[i++] = atoi(string.substr(start, end - start).c_str());
start = end + delim.length();
end = string.find(delim, start);
}
res[i] = atoi(string.substr(start, end).c_str());
}
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 (std::nothrow) 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]);
}
2016-04-18 16:01:19 +08:00
info.triangles.vertCount = static_cast<int>(vertexCount);
info.triangles.verts = vertexData;
2016-04-18 16:01:19 +08:00
info.triangles.indexCount = static_cast<int>(indexCount);
info.triangles.indices = indexData;
info.setRect(Rect(0, 0, spriteSize.width, spriteSize.height));
}
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D* texture)
{
/*
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+
Version 3 with TexturePacker 4.0 polygon mesh packing
*/
if (dictionary["frames"].getType() != cocos2d::Value::Type::MAP)
return;
ValueMap& framesDict = dictionary["frames"].asValueMap();
int format = 0;
Size textureSize;
// get the format
if (dictionary.find("metadata") != dictionary.end())
{
ValueMap& metadataDict = dictionary["metadata"].asValueMap();
format = metadataDict["format"].asInt();
if(metadataDict.find("size") != metadataDict.end())
{
textureSize = SizeFromString(metadataDict["size"].asString());
}
}
// check the format
CCASSERT(format >=0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
auto textureFileName = Director::getInstance()->getTextureCache()->getTextureFilePath(texture);
Image* image = nullptr;
NinePatchImageParser parser;
for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)
{
ValueMap& frameDict = iter->second.asValueMap();
std::string spriteFrameName = iter->first;
SpriteFrame* spriteFrame = _spriteFrames.at(spriteFrameName);
if (spriteFrame)
{
continue;
}
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)
{
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");
}
// abs ow/oh
2016-07-11 12:01:09 +08:00
ow = std::abs(ow);
oh = std::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();
}
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
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());
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
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);
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);
}
if (frameDict.find("anchor") != frameDict.end())
{
spriteFrame->setAnchorPoint(PointFromString(frameDict["anchor"].asString()));
}
}
bool flag = NinePatchImageParser::isNinePatchImage(spriteFrameName);
if(flag)
{
if (image == nullptr) {
image = new (std::nothrow) Image();
image->initWithImageFile(textureFileName);
}
parser.setSpriteFrameInfo(image, spriteFrame->getRectInPixels(), spriteFrame->isRotated());
texture->addSpriteFrameCapInset(spriteFrame, parser.parseCapInset());
}
// add sprite frame
_spriteFrames.insert(spriteFrameName, spriteFrame);
}
CC_SAFE_DELETE(image);
}
void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dict, const std::string &texturePath)
{
std::string pixelFormatName;
if (dict.find("metadata") != dict.end())
{
ValueMap& metadataDict = dict.at("metadata").asValueMap();
if (metadataDict.find("pixelFormat") != metadataDict.end())
{
pixelFormatName = metadataDict.at("pixelFormat").asString();
}
}
Texture2D *texture = nullptr;
static std::unordered_map<std::string, Texture2D::PixelFormat> pixelFormats = {
{"RGBA8888", Texture2D::PixelFormat::RGBA8888},
{"RGBA4444", Texture2D::PixelFormat::RGBA4444},
{"RGB5A1", Texture2D::PixelFormat::RGB5A1},
2016-02-12 22:25:11 +08:00
{"RGBA5551", Texture2D::PixelFormat::RGB5A1},
{"RGB565", Texture2D::PixelFormat::RGB565},
{"A8", Texture2D::PixelFormat::A8},
2016-02-12 22:25:11 +08:00
{"ALPHA", Texture2D::PixelFormat::A8},
{"I8", Texture2D::PixelFormat::I8},
{"AI88", Texture2D::PixelFormat::AI88},
2016-02-12 22:25:11 +08:00
{"ALPHA_INTENSITY", Texture2D::PixelFormat::AI88},
//{"BGRA8888", Texture2D::PixelFormat::BGRA8888}, no Image conversion RGBA -> BGRA
{"RGB888", Texture2D::PixelFormat::RGB888}
};
auto pixelFormatIt = pixelFormats.find(pixelFormatName);
if (pixelFormatIt != pixelFormats.end())
{
const Texture2D::PixelFormat pixelFormat = (*pixelFormatIt).second;
const Texture2D::PixelFormat currentPixelFormat = Texture2D::getDefaultAlphaPixelFormat();
Texture2D::setDefaultAlphaPixelFormat(pixelFormat);
texture = Director::getInstance()->getTextureCache()->addImage(texturePath);
Texture2D::setDefaultAlphaPixelFormat(currentPixelFormat);
}
else
{
texture = Director::getInstance()->getTextureCache()->addImage(texturePath);
}
if (texture)
{
addSpriteFramesWithDictionary(dict, texture);
}
else
{
CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
}
}
2014-05-30 15:13:59 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, Texture2D *texture)
{
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);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
2014-05-30 15:13:59 +08:00
addSpriteFramesWithDictionary(dict, texture);
_loadedFileNames->insert(plist);
}
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);
}
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName)
{
CCASSERT(textureFileName.size()>0, "texture name should not be null");
if (_loadedFileNames->find(plist) != _loadedFileNames->end())
{
return; // We already added it
}
const std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
addSpriteFramesWithDictionary(dict, textureFileName);
_loadedFileNames->insert(plist);
}
2014-05-30 15:13:59 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist)
{
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;
}
2014-05-30 15:13:59 +08:00
if (_loadedFileNames->find(plist) == _loadedFileNames->end())
{
2015-01-21 14:15:30 +08:00
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, plist);
}
else
{
// build texture path by replacing file extension
2014-05-30 15:13:59 +08:00
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());
}
addSpriteFramesWithDictionary(dict, texturePath);
_loadedFileNames->insert(plist);
}
}
2015-04-24 11:40:15 +08:00
bool SpriteFrameCache::isSpriteFramesWithFileLoaded(const std::string& plist) const
{
bool result = false;
if (_loadedFileNames->find(plist) != _loadedFileNames->end())
{
result = true;
}
return result;
}
void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, const std::string& frameName)
{
_spriteFrames.insert(frameName, frame);
}
void SpriteFrameCache::removeSpriteFrames()
{
_spriteFrames.clear();
_spriteFramesAliases.clear();
_loadedFileNames->clear();
}
void SpriteFrameCache::removeUnusedSpriteFrames()
{
bool removed = false;
std::vector<std::string> toRemoveFrames;
for (auto iter = _spriteFrames.begin(); iter != _spriteFrames.end(); ++iter)
{
SpriteFrame* spriteFrame = iter->second;
2014-01-22 13:47:29 +08:00
if( spriteFrame->getReferenceCount() == 1 )
{
toRemoveFrames.push_back(iter->first);
spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame);
CCLOG("cocos2d: SpriteFrameCache: removing unused frame: %s", iter->first.c_str());
removed = true;
}
}
_spriteFrames.erase(toRemoveFrames);
// FIXME:. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache
if( removed )
2012-06-12 01:43:07 +08:00
{
_loadedFileNames->clear();
}
}
void SpriteFrameCache::removeSpriteFrameByName(const std::string& name)
{
// explicit nil handling
2013-11-20 10:39:30 +08:00
if( !(name.size()>0) )
return;
// Is this an alias ?
std::string key = _spriteFramesAliases[name].asString();
if (!key.empty())
{
_spriteFrames.erase(key);
_spriteFramesAliases.erase(key);
}
else
{
_spriteFrames.erase(name);
}
// FIXME:. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache
_loadedFileNames->clear();
}
void SpriteFrameCache::removeSpriteFramesFromFile(const std::string& plist)
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
if (dict.empty())
{
CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFile: create dict by %s fail.",plist.c_str());
return;
}
removeSpriteFramesFromDictionary(dict);
2012-06-12 01:43:07 +08:00
// remove it from the cache
set<string>::iterator ret = _loadedFileNames->find(plist);
if (ret != _loadedFileNames->end())
2012-06-12 01:43:07 +08:00
{
_loadedFileNames->erase(ret);
2012-06-12 01:43:07 +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()));
if (dict.empty())
{
CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFileContent: create dict by fail.");
return;
}
removeSpriteFramesFromDictionary(dict);
}
void SpriteFrameCache::removeSpriteFramesFromDictionary(ValueMap& dictionary)
{
if (dictionary["frames"].getType() != cocos2d::Value::Type::MAP)
return;
ValueMap framesDict = dictionary["frames"].asValueMap();
std::vector<std::string> keysToRemove;
for (auto iter = framesDict.cbegin(); iter != framesDict.cend(); ++iter)
{
if (_spriteFrames.at(iter->first))
{
keysToRemove.push_back(iter->first);
}
}
_spriteFrames.erase(keysToRemove);
}
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
{
std::vector<std::string> keysToRemove;
for (auto iter = _spriteFrames.cbegin(); iter != _spriteFrames.cend(); ++iter)
{
std::string key = iter->first;
SpriteFrame* frame = _spriteFrames.at(key);
if (frame && (frame->getTexture() == texture))
{
keysToRemove.push_back(key);
}
}
_spriteFrames.erase(keysToRemove);
}
SpriteFrame* SpriteFrameCache::getSpriteFrameByName(const std::string& name)
{
SpriteFrame* frame = _spriteFrames.at(name);
if (!frame)
{
// try alias dictionary
std::string key = _spriteFramesAliases[name].asString();
if (!key.empty())
{
frame = _spriteFrames.at(key);
if (!frame)
{
CCLOG("cocos2d: SpriteFrameCache: Frame '%s' not found", name.c_str());
}
}
}
return frame;
}
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)
{
2016-07-25 01:53:22 +08:00
CCLOGWARN("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist");
}
// abs ow/oh
2016-07-11 12:01:09 +08:00
ow = std::abs(ow);
oh = std::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, 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");
}
Texture2D *texture = nullptr;
if (Director::getInstance()->getTextureCache()->reloadTexture(texturePath))
texture = Director::getInstance()->getTextureCache()->getTextureForKey(texturePath);
if (texture)
{
reloadSpriteFramesWithDictionary(dict, texture);
_loadedFileNames->insert(plist);
}
else
{
CCLOG("cocos2d: SpriteFrameCache: Couldn't load texture");
}
return true;
}
NS_CC_END