axmol/cocos/2d/CCSpriteFrameCache.cpp

419 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
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.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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"
#include <vector>
#include "2d/CCSprite.h"
#include "2d/CCAutoPolygon.h"
#include "2d/CCPlistSpriteSheetLoader.h"
2019-11-23 20:27:39 +08:00
#include "platform/CCFileUtils.h"
#include "base/ccMacros.h"
#include "base/CCDirector.h"
#include "renderer/CCTexture2D.h"
#include "base/CCNinePatchImageParser.h"
using namespace std;
NS_CC_BEGIN
static SpriteFrameCache* _sharedSpriteFrameCache = nullptr;
2019-11-23 20:27:39 +08:00
SpriteFrameCache* SpriteFrameCache::getInstance()
{
if (!_sharedSpriteFrameCache)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_sharedSpriteFrameCache = new SpriteFrameCache();
2019-11-23 20:27:39 +08:00
_sharedSpriteFrameCache->init();
}
return _sharedSpriteFrameCache;
}
void SpriteFrameCache::destroyInstance()
{
CC_SAFE_RELEASE_NULL(_sharedSpriteFrameCache);
}
bool SpriteFrameCache::init()
{
_spriteFrames.reserve(20);
clear();
2021-08-20 12:43:19 +08:00
registerSpriteSheetLoader(std::make_shared<PlistSpriteSheetLoader>());
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
SpriteFrameCache::~SpriteFrameCache() {}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(std::string_view spriteSheetFileName,
std::string_view textureFileName,
2021-12-25 10:04:45 +08:00
uint32_t spriteSheetFormat)
{
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
if (loader)
{
loader->load(spriteSheetFileName, textureFileName, *this);
}
}
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(std::string_view spriteSheetFileName,
2021-12-25 10:04:45 +08:00
Texture2D* texture,
uint32_t spriteSheetFormat)
2019-11-23 20:27:39 +08:00
{
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
if (loader)
2019-11-23 20:27:39 +08:00
{
loader->load(spriteSheetFileName, texture, *this);
2019-11-23 20:27:39 +08:00
}
}
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::addSpriteFramesWithFile(std::string_view spriteSheetFileName, uint32_t spriteSheetFormat)
2019-11-23 20:27:39 +08:00
{
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
if (loader)
2019-11-23 20:27:39 +08:00
{
loader->load(spriteSheetFileName, *this);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void SpriteFrameCache::addSpriteFramesWithFileContent(const Data& content,
Texture2D* texture,
uint32_t spriteSheetFormat)
2019-11-23 20:27:39 +08:00
{
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
if (loader)
2019-11-23 20:27:39 +08:00
{
loader->load(content, texture, *this);
2019-11-23 20:27:39 +08:00
}
}
2021-12-26 23:26:34 +08:00
bool SpriteFrameCache::isSpriteFramesWithFileLoaded(std::string_view plist) const
2019-11-23 20:27:39 +08:00
{
return isSpriteSheetInUse(plist) && isPlistFull(plist);
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, std::string_view frameName)
2019-11-23 20:27:39 +08:00
{
CCASSERT(frame, "frame should not be nil");
2019-11-23 20:27:39 +08:00
const std::string name = "by#addSpriteFrame()";
2021-12-25 10:04:45 +08:00
auto&& itr = _spriteSheets.find(name);
if (itr != _spriteSheets.end())
2019-11-23 20:27:39 +08:00
{
insertFrame(itr->second, frameName, frame);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
auto spriteSheet = std::make_shared<SpriteSheet>();
spriteSheet->path = name;
insertFrame(spriteSheet, frameName, frame);
2019-11-23 20:27:39 +08:00
}
}
void SpriteFrameCache::removeSpriteFrames()
{
clear();
2019-11-23 20:27:39 +08:00
}
void SpriteFrameCache::removeUnusedSpriteFrames()
{
auto removed = false;
2019-11-23 20:27:39 +08:00
std::vector<std::string> toRemoveFrames;
const auto frames = getSpriteFrames();
for (auto& iter : frames)
2019-11-23 20:27:39 +08:00
{
auto* spriteFrame = iter.second;
if (spriteFrame->getReferenceCount() == 1)
2019-11-23 20:27:39 +08:00
{
toRemoveFrames.push_back(iter.first);
spriteFrame->getTexture()->removeSpriteFrameCapInset(spriteFrame);
CCLOG("cocos2d: SpriteFrameCache: removing unused frame: %s", iter.first.c_str());
removed = true;
}
}
if (removed)
2019-11-23 20:27:39 +08:00
{
eraseFrames(toRemoveFrames);
2019-11-23 20:27:39 +08:00
}
}
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::removeSpriteFrameByName(std::string_view name)
2019-11-23 20:27:39 +08:00
{
// explicit nil handling
if (name.empty())
return;
eraseFrame(name);
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::removeSpriteFramesFromFile(std::string_view atlasPath)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// const auto fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
// auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
// if (dict.empty())
//{
2021-12-25 10:04:45 +08:00
// CCLOG("cocos2d:SpriteFrameCache:removeSpriteFramesFromFile: create dict by %s fail.",plist.c_str());
// return;
// }
// removeSpriteFramesFromDictionary(dict);
2019-11-23 20:27:39 +08:00
// remove it from the cache
removeSpriteSheet(atlasPath);
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
void SpriteFrameCache::removeSpriteFramesFromFileContent(std::string_view plist_content)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto dict =
FileUtils::getInstance()->getValueMapFromData(plist_content.data(), static_cast<int>(plist_content.size()));
2019-11-23 20:27:39 +08:00
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;
const auto& framesDict = dictionary["frames"].asValueMap();
2019-11-23 20:27:39 +08:00
std::vector<std::string> keysToRemove;
for (const auto& iter : framesDict)
{
if (findFrame(iter.first))
2019-11-23 20:27:39 +08:00
{
keysToRemove.push_back(iter.first);
}
}
eraseFrames(keysToRemove);
2019-11-23 20:27:39 +08:00
}
void SpriteFrameCache::removeSpriteFramesFromTexture(Texture2D* texture)
{
std::vector<std::string> keysToRemove;
for (auto& iter : getSpriteFrames())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto key = iter.first;
auto* frame = findFrame(key);
2019-11-23 20:27:39 +08:00
if (frame && (frame->getTexture() == texture))
{
keysToRemove.push_back(key);
}
}
eraseFrames(keysToRemove);
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
SpriteFrame* SpriteFrameCache::getSpriteFrameByName(std::string_view name)
2019-11-23 20:27:39 +08:00
{
auto* frame = findFrame(name);
2019-11-23 20:27:39 +08:00
if (!frame)
{
2021-12-26 23:26:34 +08:00
CCLOG("cocos2d: SpriteFrameCache: Frame '%s' isn't found", name.data());
2019-11-23 20:27:39 +08:00
}
return frame;
}
2021-12-26 23:26:34 +08:00
bool SpriteFrameCache::reloadTexture(std::string_view spriteSheetFileName)
2019-11-23 20:27:39 +08:00
{
CCASSERT(!spriteSheetFileName.empty(), "plist filename should not be nullptr");
2019-11-23 20:27:39 +08:00
const auto spriteSheetItr = _spriteSheets.find(spriteSheetFileName);
if (spriteSheetItr == _spriteSheets.end())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
return false; // Sprite sheet wasn't loaded, so don't reload it
2019-11-23 20:27:39 +08:00
}
const auto format = spriteSheetItr->second->format;
2019-11-23 20:27:39 +08:00
if (isSpriteSheetInUse(spriteSheetFileName))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
removeSpriteSheet(
spriteSheetFileName); // we've removed the associated entry, so spriteSheetItr is no longer valid
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
// If one plist hasn't be loaded, we don't load it here.
2019-11-23 20:27:39 +08:00
return false;
}
auto* loader = getSpriteSheetLoader(format);
if (loader)
2019-11-23 20:27:39 +08:00
{
loader->reload(spriteSheetFileName, *this);
2019-11-23 20:27:39 +08:00
}
return true;
}
2021-12-25 10:04:45 +08:00
void SpriteFrameCache::insertFrame(const std::shared_ptr<SpriteSheet>& spriteSheet,
2021-12-26 23:26:34 +08:00
std::string_view frameName,
2021-12-25 10:04:45 +08:00
SpriteFrame* spriteFrame)
2019-11-23 20:27:39 +08:00
{
2021-12-26 23:26:34 +08:00
spriteSheet->frames.emplace(frameName);
2021-12-25 10:04:45 +08:00
_spriteFrames.insert(frameName, spriteFrame); // add SpriteFrame
_spriteSheets[spriteSheet->path] = spriteSheet;
2021-12-26 23:26:34 +08:00
hlookup::set_item(_spriteFrameToSpriteSheetMap, frameName, spriteSheet); // _spriteFrameToSpriteSheetMap[frameName] = spriteSheet; // insert
// index frameName->plist
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
bool SpriteFrameCache::eraseFrame(std::string_view frameName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_spriteFrames.erase(frameName); // drop SpriteFrame
const auto itFrame = _spriteFrameToSpriteSheetMap.find(frameName);
if (itFrame != _spriteFrameToSpriteSheetMap.end())
2019-11-23 20:27:39 +08:00
{
auto& spriteSheet = itFrame->second;
spriteSheet->full = false;
spriteSheet->frames.erase(frameName);
if (spriteSheet->frames.empty())
2019-11-23 20:27:39 +08:00
{
_spriteSheets.erase(spriteSheet->path);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_spriteFrameToSpriteSheetMap.erase(itFrame); // update index frame->plist
// erase all sprite sheets if all frames are erased
2021-12-25 10:04:45 +08:00
// if (_spriteFrameToSpriteSheetMap.empty())
//{
// _spriteSheets.clear();
//}
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
bool SpriteFrameCache::eraseFrames(const std::vector<std::string>& frames)
2019-11-23 20:27:39 +08:00
{
auto ret = false;
for (const auto& frame : frames)
2019-11-23 20:27:39 +08:00
{
ret |= eraseFrame(frame);
}
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-26 23:26:34 +08:00
bool SpriteFrameCache::removeSpriteSheet(std::string_view spriteSheetFileName)
2019-11-23 20:27:39 +08:00
{
auto it = _spriteSheets.find(spriteSheetFileName);
2021-12-25 10:04:45 +08:00
if (it == _spriteSheets.end())
return false;
2019-11-23 20:27:39 +08:00
auto& frames = it->second->frames;
2019-11-23 20:27:39 +08:00
for (const auto& f : frames)
{
// !!do not!! call `_spriteFrames.erase(f);` to erase SpriteFrame
// only erase index here
_spriteFrames.erase(f);
2021-12-25 10:04:45 +08:00
_spriteFrameToSpriteSheetMap.erase(f); // erase plist frame frameName->plist
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_spriteSheets.erase(spriteSheetFileName); // update index plist->[frameNames]
2019-11-23 20:27:39 +08:00
return true;
}
void SpriteFrameCache::clear()
2019-11-23 20:27:39 +08:00
{
_spriteSheets.clear();
_spriteFrameToSpriteSheetMap.clear();
2019-11-23 20:27:39 +08:00
_spriteFrames.clear();
}
2021-12-26 23:26:34 +08:00
bool SpriteFrameCache::hasFrame(std::string_view frame) const
2019-11-23 20:27:39 +08:00
{
return _spriteFrameToSpriteSheetMap.find(frame) != _spriteFrameToSpriteSheetMap.end();
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
bool SpriteFrameCache::isSpriteSheetInUse(std::string_view spriteSheetFileName) const
2019-11-23 20:27:39 +08:00
{
const auto spriteSheetItr = _spriteSheets.find(spriteSheetFileName);
return spriteSheetItr != _spriteSheets.end() && !spriteSheetItr->second->frames.empty();
}
2019-11-23 20:27:39 +08:00
2021-12-26 23:26:34 +08:00
SpriteFrame* SpriteFrameCache::findFrame(std::string_view frame)
2019-11-23 20:27:39 +08:00
{
return _spriteFrames.at(frame);
}
void SpriteFrameCache::addSpriteFrameCapInset(SpriteFrame* spriteFrame, const Rect& capInsets, Texture2D* texture)
{
texture->addSpriteFrameCapInset(spriteFrame, capInsets);
}
2021-12-26 23:26:34 +08:00
StringMap<SpriteFrame*>& SpriteFrameCache::getSpriteFrames()
2019-11-23 20:27:39 +08:00
{
return _spriteFrames;
}
2021-08-20 12:43:19 +08:00
void SpriteFrameCache::registerSpriteSheetLoader(std::shared_ptr<ISpriteSheetLoader> loader)
{
auto format = loader->getFormat();
if (_spriteSheetLoaders.find(format) != _spriteSheetLoaders.end())
{
return;
}
_spriteSheetLoaders.emplace(format, std::move(loader));
}
void SpriteFrameCache::deregisterSpriteSheetLoader(uint32_t spriteSheetFormat)
{
auto&& itr = _spriteSheetLoaders.find(spriteSheetFormat);
if (itr != _spriteSheetLoaders.end())
{
_spriteSheetLoaders.erase(itr);
}
}
ISpriteSheetLoader* SpriteFrameCache::getSpriteSheetLoader(uint32_t spriteSheetFormat)
{
auto&& itr = _spriteSheetLoaders.find(spriteSheetFormat);
if (itr != _spriteSheetLoaders.end())
{
return itr->second.get();
}
return nullptr;
}
2019-11-23 20:27:39 +08:00
NS_CC_END