2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2019-11-23 20:27:39 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCSpriteFrameCacheHelper.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "2d/CCSpriteFrame.h"
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCacheHelper* SpriteFrameCacheHelper::_spriteFrameCacheHelper = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCacheHelper* SpriteFrameCacheHelper::getInstance()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_spriteFrameCacheHelper)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_spriteFrameCacheHelper = new SpriteFrameCacheHelper();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _spriteFrameCacheHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCacheHelper::purge()
|
|
|
|
{
|
|
|
|
delete _spriteFrameCacheHelper;
|
|
|
|
_spriteFrameCacheHelper = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrameCacheHelper::retainSpriteFrames(const std::string& plistPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _usingSpriteFrames.find(plistPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _usingSpriteFrames.end())
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plistPath);
|
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto spriteFramesCache = SpriteFrameCache::getInstance();
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap& framesDict = dict["frames"].asValueMap();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::vector<SpriteFrame*> vec;
|
|
|
|
for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& spriteFrameName = iter->first;
|
2019-11-23 20:27:39 +08:00
|
|
|
SpriteFrame* spriteFrame = spriteFramesCache->getSpriteFrameByName(spriteFrameName);
|
|
|
|
vec.push_back(spriteFrame);
|
|
|
|
CC_SAFE_RETAIN(spriteFrame);
|
|
|
|
}
|
|
|
|
_usingSpriteFrames[plistPath] = vec;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrameCacheHelper::releaseSpriteFrames(const std::string& plistPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto it = _usingSpriteFrames.find(plistPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _usingSpriteFrames.end())
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& vec = it->second;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto itFrame = vec.begin();
|
|
|
|
while (itFrame != vec.end())
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(*itFrame);
|
|
|
|
++itFrame;
|
|
|
|
}
|
|
|
|
vec.clear();
|
|
|
|
_usingSpriteFrames.erase(it);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteFrameCacheHelper::removeSpriteFrameFromFile(const std::string& plistPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(plistPath);
|
|
|
|
releaseSpriteFrames(plistPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteFrameCacheHelper::addSpriteFrameFromFile(const std::string& plistPath, const std::string& imagePath)
|
|
|
|
{
|
2021-08-17 12:51:17 +08:00
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistPath, imagePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
retainSpriteFrames(plistPath);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCacheHelper::SpriteFrameCacheHelper() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
SpriteFrameCacheHelper::~SpriteFrameCacheHelper()
|
|
|
|
{
|
|
|
|
auto i = _usingSpriteFrames.begin();
|
|
|
|
while (i != _usingSpriteFrames.end())
|
|
|
|
{
|
|
|
|
auto j = i++;
|
|
|
|
removeSpriteFrameFromFile(j->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|