2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2013-06-06 12:02:54 +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"
|
2014-11-07 09:13:59 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "2d/CCSpriteFrame.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2013-10-15 18:00:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCacheHelper* SpriteFrameCacheHelper::_spriteFrameCacheHelper = nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCacheHelper* SpriteFrameCacheHelper::getInstance()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_spriteFrameCacheHelper)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_spriteFrameCacheHelper = new SpriteFrameCacheHelper();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 20:24:25 +08:00
|
|
|
return _spriteFrameCacheHelper;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void SpriteFrameCacheHelper::purge()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 20:24:25 +08:00
|
|
|
delete _spriteFrameCacheHelper;
|
2013-11-05 15:32:13 +08:00
|
|
|
_spriteFrameCacheHelper = nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void SpriteFrameCacheHelper::retainSpriteFrames(std::string_view plistPath)
|
2014-11-07 09:13:59 +08:00
|
|
|
{
|
|
|
|
auto it = _usingSpriteFrames.find(plistPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _usingSpriteFrames.end())
|
|
|
|
return;
|
2014-11-07 09:13:59 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plistPath);
|
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
|
2014-11-07 09:13:59 +08:00
|
|
|
auto spriteFramesCache = SpriteFrameCache::getInstance();
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap& framesDict = dict["frames"].asValueMap();
|
2014-11-07 09:13:59 +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-02 22:31:44 +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-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(spriteFrame);
|
2014-11-07 09:13:59 +08:00
|
|
|
}
|
2022-08-09 13:21:53 +08:00
|
|
|
_usingSpriteFrames[plistPath] = std::move(vec);
|
2014-11-07 09:13:59 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void SpriteFrameCacheHelper::releaseSpriteFrames(std::string_view plistPath)
|
2014-11-07 09:13:59 +08:00
|
|
|
{
|
|
|
|
auto it = _usingSpriteFrames.find(plistPath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it == _usingSpriteFrames.end())
|
|
|
|
return;
|
2014-11-07 09:13:59 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& vec = it->second;
|
2014-11-07 09:13:59 +08:00
|
|
|
auto itFrame = vec.begin();
|
|
|
|
while (itFrame != vec.end())
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(*itFrame);
|
2014-11-07 09:13:59 +08:00
|
|
|
++itFrame;
|
|
|
|
}
|
|
|
|
vec.clear();
|
|
|
|
_usingSpriteFrames.erase(it);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void SpriteFrameCacheHelper::removeSpriteFrameFromFile(std::string_view plistPath)
|
2014-11-07 09:13:59 +08:00
|
|
|
{
|
|
|
|
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(plistPath);
|
2014-11-19 15:27:55 +08:00
|
|
|
releaseSpriteFrames(plistPath);
|
2014-11-07 09:13:59 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void SpriteFrameCacheHelper::addSpriteFrameFromFile(std::string_view plistPath, std::string_view imagePath)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2014-01-17 13:35:58 +08:00
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistPath, imagePath);
|
2014-11-19 15:27:55 +08:00
|
|
|
retainSpriteFrames(plistPath);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrameCacheHelper::SpriteFrameCacheHelper() {}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
SpriteFrameCacheHelper::~SpriteFrameCacheHelper()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2015-10-15 17:47:12 +08:00
|
|
|
auto i = _usingSpriteFrames.begin();
|
|
|
|
while (i != _usingSpriteFrames.end())
|
|
|
|
{
|
|
|
|
auto j = i++;
|
|
|
|
removeSpriteFrameFromFile(j->first);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|