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
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
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-31 12:12:40 +08:00
|
|
|
void SpriteFrameCacheHelper::retainSpriteFrames(std::string_view 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;
|
2022-08-03 09:53:53 +08:00
|
|
|
|
|
|
|
SpriteFrame* spriteFrame = spriteFramesCache->findFrame(spriteFrameName);
|
|
|
|
AXASSERT(spriteFrame, "spriteframe is null!");
|
|
|
|
|
2022-08-09 09:54:53 +08:00
|
|
|
vec.emplace_back(spriteFrame);
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(spriteFrame);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2022-08-09 13:21:53 +08:00
|
|
|
_usingSpriteFrames[plistPath] = std::move(vec);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCacheHelper::releaseSpriteFrames(std::string_view 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())
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(*itFrame);
|
2019-11-23 20:27:39 +08:00
|
|
|
++itFrame;
|
|
|
|
}
|
|
|
|
vec.clear();
|
|
|
|
_usingSpriteFrames.erase(it);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCacheHelper::removeSpriteFrameFromFile(std::string_view plistPath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(plistPath);
|
|
|
|
releaseSpriteFrames(plistPath);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteFrameCacheHelper::addSpriteFrameFromFile(std::string_view plistPath, std::string_view imagePath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
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
|