axmol/extensions/CocoStudio/Armature/utils/CCSpriteFrameCacheHelper.cpp

185 lines
6.5 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
Copyright (c) 2013 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 "CCSpriteFrameCacheHelper.h"
namespace cocos2d { namespace extension { namespace armature {
2013-06-06 12:02:54 +08:00
SpriteFrameCacheHelper *SpriteFrameCacheHelper::s_SpriteFrameCacheHelper = NULL;
2013-06-06 12:02:54 +08:00
SpriteFrameCacheHelper *SpriteFrameCacheHelper::sharedSpriteFrameCacheHelper()
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
if(!s_SpriteFrameCacheHelper)
{
s_SpriteFrameCacheHelper = new SpriteFrameCacheHelper();
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
return s_SpriteFrameCacheHelper;
2013-06-06 12:02:54 +08:00
}
void SpriteFrameCacheHelper::purgeSpriteFrameCacheHelper()
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
delete s_SpriteFrameCacheHelper;
s_SpriteFrameCacheHelper = NULL;
2013-06-06 12:02:54 +08:00
}
void SpriteFrameCacheHelper::addSpriteFrameFromFile(const char *plistPath, const char *imagePath)
2013-06-06 12:02:54 +08:00
{
std::string path = FileUtils::getInstance()->fullPathForFilename(plistPath);
Dictionary *dict = Dictionary::createWithContentsOfFileThreadSafe(path.c_str());
2013-06-06 12:02:54 +08:00
Texture2D *pobTexture = TextureCache::getInstance()->addImage(imagePath);
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
addSpriteFrameFromDict(dict, pobTexture, imagePath);
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
dict->release();
2013-06-06 12:02:54 +08:00
}
void SpriteFrameCacheHelper::addSpriteFrameFromDict(Dictionary *dictionary, Texture2D *pobTexture, const char *imagePath)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +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+
*/
Dictionary *metadataDict = (Dictionary *)dictionary->objectForKey("metadata");
Dictionary *framesDict = (Dictionary *)dictionary->objectForKey("frames");
2013-06-07 10:52:32 +08:00
int format = 0;
// get the format
if(metadataDict != NULL)
{
format = metadataDict->valueForKey("format")->intValue();
}
// check the format
CCASSERT(format >= 0 && format <= 3, "format is not supported for SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
2013-06-07 10:52:32 +08:00
DictElement *pElement = NULL;
2013-06-07 10:52:32 +08:00
CCDICT_FOREACH(framesDict, pElement)
{
Dictionary *frameDict = static_cast<Dictionary*>(pElement->getObject());
2013-06-07 10:52:32 +08:00
std::string spriteFrameName = pElement->getStrKey();
_display2ImageMap[spriteFrameName] = imagePath;
2013-06-07 10:52:32 +08:00
//log("spriteFrameName : %s, imagePath : %s", spriteFrameName.c_str(), _imagePath);
2013-06-07 10:52:32 +08:00
SpriteFrame *spriteFrame = (SpriteFrame *)SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName.c_str());
2013-06-07 10:52:32 +08:00
if (spriteFrame)
{
continue;
}
if(format == 0)
{
float x = frameDict->valueForKey("x")->floatValue();
float y = frameDict->valueForKey("y")->floatValue();
float w = frameDict->valueForKey("width")->floatValue();
float h = frameDict->valueForKey("height")->floatValue();
float ox = frameDict->valueForKey("offsetX")->floatValue();
float oy = frameDict->valueForKey("offsetY")->floatValue();
int ow = frameDict->valueForKey("originalWidth")->intValue();
int oh = frameDict->valueForKey("originalHeight")->intValue();
// check ow/oh
if(!ow || !oh)
{
CCLOG("cocos2d: WARNING: originalWidth/Height not found on the SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
2013-06-07 10:52:32 +08:00
}
// abs ow/oh
ow = abs(ow);
oh = abs(oh);
// create frame
spriteFrame = new SpriteFrame();
spriteFrame->initWithTexture(pobTexture, Rect(x, y, w, h), false, Point(ox, oy), Size((float)ow, (float)oh));
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
else if(format == 1 || format == 2)
{
Rect frame = RectFromString(frameDict->valueForKey("frame")->getCString());
2013-06-07 10:52:32 +08:00
bool rotated = false;
// rotation
if (format == 2)
{
rotated = frameDict->valueForKey("rotated")->boolValue();
}
Point offset = PointFromString(frameDict->valueForKey("offset")->getCString());
Size sourceSize = SizeFromString(frameDict->valueForKey("sourceSize")->getCString());
2013-06-07 10:52:32 +08:00
// create frame
spriteFrame = new SpriteFrame();
2013-06-07 10:52:32 +08:00
spriteFrame->initWithTexture(pobTexture, frame, rotated, offset, sourceSize );
}
else if (format == 3)
{
}
// add sprite frame
SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, spriteFrameName.c_str());
2013-06-07 10:52:32 +08:00
spriteFrame->release();
}
2013-06-06 12:02:54 +08:00
}
const char *SpriteFrameCacheHelper::getDisplayImagePath(const char *displayName)
2013-06-06 12:02:54 +08:00
{
return _display2ImageMap[displayName].c_str();
2013-06-06 12:02:54 +08:00
}
TextureAtlas *SpriteFrameCacheHelper::getTextureAtlas(const char *displayName)
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
const char *textureName = getDisplayImagePath(displayName);
TextureAtlas *atlas = (TextureAtlas *)_display2TextureAtlas->objectForKey(textureName);
2013-06-07 10:52:32 +08:00
if (atlas == NULL)
{
atlas = TextureAtlas::createWithTexture(TextureCache::getInstance()->addImage(textureName), 4);
_display2TextureAtlas->setObject(atlas, textureName);
2013-06-07 10:52:32 +08:00
}
return atlas;
2013-06-06 12:02:54 +08:00
}
SpriteFrameCacheHelper::SpriteFrameCacheHelper()
2013-06-06 12:02:54 +08:00
{
_display2TextureAtlas = new Dictionary();
_display2TextureAtlas->init();
2013-06-06 12:02:54 +08:00
}
SpriteFrameCacheHelper::~SpriteFrameCacheHelper()
2013-06-06 12:02:54 +08:00
{
CC_SAFE_RELEASE_NULL(_display2TextureAtlas);
2013-06-06 12:02:54 +08:00
}
}}} // namespace cocos2d { namespace extension { namespace armature {