axmol/cocos/2d/CCSpriteFrameCache.cpp

410 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
{
_sharedSpriteFrameCache = new (std::nothrow) SpriteFrameCache();
_sharedSpriteFrameCache->init();
}
return _sharedSpriteFrameCache;
}
void SpriteFrameCache::destroyInstance()
{
CC_SAFE_RELEASE_NULL(_sharedSpriteFrameCache);
}
bool SpriteFrameCache::init()
{
_spriteFrames.reserve(20);
clear();
registerSpriteSheetLoader(std::make_unique<PlistSpriteSheetLoader>());
2019-11-23 20:27:39 +08:00
return true;
}
SpriteFrameCache::~SpriteFrameCache()
{
}
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& spriteSheetFileName, const std::string& textureFileName, uint32_t spriteSheetFormat)
{
auto* loader = getSpriteSheetLoader(spriteSheetFormat);
if (loader)
{
loader->load(spriteSheetFileName, textureFileName, *this);
}
}
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& spriteSheetFileName, 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
}
}
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& 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
}
}
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
}
}
bool SpriteFrameCache::isSpriteFramesWithFileLoaded(const std::string& plist) const
2019-11-23 20:27:39 +08:00
{
return isSpriteSheetInUse(plist) && isPlistFull(plist);
2019-11-23 20:27:39 +08:00
}
void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, const std::string& 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()";
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
{
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
}
}
void SpriteFrameCache::removeSpriteFrameByName(const std::string& name)
{
// explicit nil handling
if (name.empty())
return;
eraseFrame(name);
2019-11-23 20:27:39 +08:00
}
void SpriteFrameCache::removeSpriteFramesFromFile(const std::string& atlasPath)
2019-11-23 20:27:39 +08:00
{
//const auto fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
//auto dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
//if (dict.empty())
//{
// 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
}
void SpriteFrameCache::removeSpriteFramesFromFileContent(const std::string& plist_content)
{
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
{
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
}
SpriteFrame* SpriteFrameCache::getSpriteFrameByName(const std::string& name)
{
auto* frame = findFrame(name);
2019-11-23 20:27:39 +08:00
if (!frame)
{
CCLOG("cocos2d: SpriteFrameCache: Frame '%s' isn't found", name.c_str());
2019-11-23 20:27:39 +08:00
}
return frame;
}
bool SpriteFrameCache::reloadTexture(const std::string& 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
{
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
{
removeSpriteSheet(spriteSheetFileName); // we've removed the associated entry, so spriteSheetItr is no longer valid
2019-11-23 20:27:39 +08:00
}
else
{
//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;
}
void SpriteFrameCache::insertFrame(const std::shared_ptr<SpriteSheet>& spriteSheet, const std::string& frameName, SpriteFrame* spriteFrame)
2019-11-23 20:27:39 +08:00
{
spriteSheet->frames.insert(frameName);
_spriteFrames.insert(frameName, spriteFrame); //add SpriteFrame
_spriteSheets[spriteSheet->path] = spriteSheet;
_spriteFrameToSpriteSheetMap[frameName] = spriteSheet; //insert index frameName->plist
2019-11-23 20:27:39 +08:00
}
bool SpriteFrameCache::eraseFrame(const std::string& frameName)
2019-11-23 20:27:39 +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
}
_spriteFrameToSpriteSheetMap.erase(itFrame); //update index frame->plist
// erase all sprite sheets if all frames are erased
//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;
}
bool SpriteFrameCache::removeSpriteSheet(const std::string& spriteSheetFileName)
2019-11-23 20:27:39 +08:00
{
auto it = _spriteSheets.find(spriteSheetFileName);
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);
_spriteFrameToSpriteSheetMap.erase(f); //erase plist frame frameName->plist
2019-11-23 20:27:39 +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();
}
bool SpriteFrameCache::hasFrame(const std::string& frame) const
2019-11-23 20:27:39 +08:00
{
return _spriteFrameToSpriteSheetMap.find(frame) != _spriteFrameToSpriteSheetMap.end();
2019-11-23 20:27:39 +08:00
}
bool SpriteFrameCache::isSpriteSheetInUse(const std::string& 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
SpriteFrame* SpriteFrameCache::findFrame(const std::string& 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);
}
Map<std::string, SpriteFrame*>& SpriteFrameCache::getSpriteFrames()
2019-11-23 20:27:39 +08:00
{
return _spriteFrames;
}
void SpriteFrameCache::registerSpriteSheetLoader(std::unique_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