mirror of https://github.com/axmolengine/axmol.git
When loading csb files, prevent repeated loading of plist files (#1844)
* When loading csb files, prevent repeated loading of plist files * Add more feature * Rename function * Use isSpriteFramesWithFileLoaded to determine whether the atlas is loaded * use string_view instead of string * Update SpriteFrameCache.cpp * Fix compilation error
This commit is contained in:
parent
f159250c4e
commit
16cb4b5e9d
|
@ -170,6 +170,33 @@ void SpriteFrameCache::removeUnusedSpriteFrames()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SpriteFrameCache::removeUnusedSpriteSheets()
|
||||||
|
{
|
||||||
|
std::vector<std::string_view> willRemoveSpriteSheetFileNames;
|
||||||
|
for (auto&& it : _spriteSheets)
|
||||||
|
{
|
||||||
|
bool isUsed = false;
|
||||||
|
for (auto&& frame : it.second->frames)
|
||||||
|
{
|
||||||
|
auto spriteFrame = getSpriteFrameByName(frame);
|
||||||
|
if (spriteFrame && spriteFrame->getReferenceCount() > 1)
|
||||||
|
{
|
||||||
|
isUsed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isUsed)
|
||||||
|
willRemoveSpriteSheetFileNames.push_back(it.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& spriteSheetFileName : willRemoveSpriteSheetFileNames)
|
||||||
|
{
|
||||||
|
AXLOG("axmol: SpriteFrameCache: removing unused sprite sheet file : %s", spriteSheetFileName.data());
|
||||||
|
removeSpriteSheet(spriteSheetFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SpriteFrameCache::removeSpriteFrameByName(std::string_view name)
|
void SpriteFrameCache::removeSpriteFrameByName(std::string_view name)
|
||||||
{
|
{
|
||||||
// explicit nil handling
|
// explicit nil handling
|
||||||
|
@ -376,6 +403,18 @@ SpriteFrame* SpriteFrameCache::findFrame(std::string_view frame)
|
||||||
return _spriteFrames.at(frame);
|
return _spriteFrames.at(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string_view SpriteFrameCache::getSpriteFrameName(SpriteFrame* frame)
|
||||||
|
{
|
||||||
|
for (auto& it : _spriteFrames)
|
||||||
|
{
|
||||||
|
if (it.second == frame)
|
||||||
|
{
|
||||||
|
return it.first;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
void SpriteFrameCache::addSpriteFrameCapInset(SpriteFrame* spriteFrame, const Rect& capInsets, Texture2D* texture)
|
void SpriteFrameCache::addSpriteFrameCapInset(SpriteFrame* spriteFrame, const Rect& capInsets, Texture2D* texture)
|
||||||
{
|
{
|
||||||
texture->addSpriteFrameCapInset(spriteFrame, capInsets);
|
texture->addSpriteFrameCapInset(spriteFrame, capInsets);
|
||||||
|
|
|
@ -199,6 +199,8 @@ public:
|
||||||
*/
|
*/
|
||||||
void removeUnusedSpriteFrames();
|
void removeUnusedSpriteFrames();
|
||||||
|
|
||||||
|
void removeUnusedSpriteSheets();
|
||||||
|
|
||||||
/** Deletes an sprite frame from the sprite frame cache.
|
/** Deletes an sprite frame from the sprite frame cache.
|
||||||
*
|
*
|
||||||
* @param name The name of the sprite frame that needs to removed.
|
* @param name The name of the sprite frame that needs to removed.
|
||||||
|
@ -246,6 +248,8 @@ public:
|
||||||
|
|
||||||
SpriteFrame* findFrame(std::string_view frame);
|
SpriteFrame* findFrame(std::string_view frame);
|
||||||
|
|
||||||
|
std::string_view getSpriteFrameName(SpriteFrame* frame);
|
||||||
|
|
||||||
/** Record SpriteFrame with plist and frame name, add frame name
|
/** Record SpriteFrame with plist and frame name, add frame name
|
||||||
* and plist to index
|
* and plist to index
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -466,8 +466,11 @@ Node* CSLoader::loadNodeWithContent(std::string_view content)
|
||||||
std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
|
std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
|
||||||
plist = _jsonPath + plist;
|
plist = _jsonPath + plist;
|
||||||
png = _jsonPath + png;
|
png = _jsonPath + png;
|
||||||
|
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
|
||||||
|
{
|
||||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// decode node tree
|
// decode node tree
|
||||||
const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE);
|
const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE);
|
||||||
|
@ -949,9 +952,12 @@ Node* CSLoader::createNode(const Data& data, const ccNodeLoadCallback& callback)
|
||||||
AXLOG("textureSize = %d", textureSize);
|
AXLOG("textureSize = %d", textureSize);
|
||||||
for (int i = 0; i < textureSize; ++i)
|
for (int i = 0; i < textureSize; ++i)
|
||||||
{
|
{
|
||||||
std::string plist = textures->Get(i)->c_str();
|
std::string_view plist = textures->Get(i)->c_str();
|
||||||
|
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
|
||||||
|
{
|
||||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
|
node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
|
||||||
} while (0);
|
} while (0);
|
||||||
|
@ -1073,9 +1079,12 @@ Node* CSLoader::nodeWithFlatBuffersFile(std::string_view fileName, const ccNodeL
|
||||||
int textureSize = textures->size();
|
int textureSize = textures->size();
|
||||||
for (int i = 0; i < textureSize; ++i)
|
for (int i = 0; i < textureSize; ++i)
|
||||||
{
|
{
|
||||||
std::string plist = textures->Get(i)->c_str();
|
std::string_view plist = textures->Get(i)->c_str();
|
||||||
|
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
|
||||||
|
{
|
||||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
|
Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
|
||||||
|
|
||||||
|
@ -1438,7 +1447,11 @@ Node* CSLoader::createNodeWithFlatBuffersForSimulator(std::string_view filename)
|
||||||
// AXLOG("textureSize = %d", textureSize);
|
// AXLOG("textureSize = %d", textureSize);
|
||||||
for (int i = 0; i < textureSize; ++i)
|
for (int i = 0; i < textureSize; ++i)
|
||||||
{
|
{
|
||||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
|
std::string_view plist = textures->Get(i)->c_str();
|
||||||
|
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
|
||||||
|
{
|
||||||
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto nodeTree = csparsebinary->nodeTree();
|
auto nodeTree = csparsebinary->nodeTree();
|
||||||
|
|
Loading…
Reference in New Issue