Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 30 additions & 48 deletions cocos/2d/CCSpriteFrameCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,6 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dict, const std::

void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, Texture2D *texture)
{
if (_spriteFramesCache.hasPlist(plist))
{
return; // We already added it
}

std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);

Expand All @@ -370,10 +365,6 @@ void SpriteFrameCache::addSpriteFramesWithFileContent(const std::string& plist_c
void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist, const std::string& textureFileName)
{
CCASSERT(textureFileName.size()>0, "texture name should not be null");
if (_spriteFramesCache.hasPlist(plist))
{
return; // We already added it
}
const std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plist);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
addSpriteFramesWithDictionary(dict, textureFileName, plist);
Expand All @@ -391,45 +382,42 @@ void SpriteFrameCache::addSpriteFramesWithFile(const std::string& plist)
return;
}

if (!_spriteFramesCache.hasPlist(plist))
{
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);

string texturePath("");
string texturePath("");

if (dict.find("metadata") != dict.end())
{
ValueMap& metadataDict = dict["metadata"].asValueMap();
// try to read texture file name from meta data
texturePath = metadataDict["textureFileName"].asString();
}
if (dict.find("metadata") != dict.end())
{
ValueMap& metadataDict = dict["metadata"].asValueMap();
// try to read texture file name from meta data
texturePath = metadataDict["textureFileName"].asString();
}

if (!texturePath.empty())
{
// build texture path relative to plist file
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath, plist);
}
else
{
// build texture path by replacing file extension
texturePath = plist;
if (!texturePath.empty())
{
// build texture path relative to plist file
texturePath = FileUtils::getInstance()->fullPathFromRelativeFile(texturePath, plist);
}
else
{
// build texture path by replacing file extension
texturePath = plist;

// remove .xxx
size_t startPos = texturePath.find_last_of(".");
texturePath = texturePath.erase(startPos);
// remove .xxx
size_t startPos = texturePath.find_last_of(".");
texturePath = texturePath.erase(startPos);

// append .png
texturePath = texturePath.append(".png");
// append .png
texturePath = texturePath.append(".png");

CCLOG("cocos2d: SpriteFrameCache: Trying to use file %s as texture", texturePath.c_str());
}
addSpriteFramesWithDictionary(dict, texturePath, plist);
CCLOG("cocos2d: SpriteFrameCache: Trying to use file %s as texture", texturePath.c_str());
}
addSpriteFramesWithDictionary(dict, texturePath, plist);
}

bool SpriteFrameCache::isSpriteFramesWithFileLoaded(const std::string& plist) const
{
return _spriteFramesCache.hasPlist(plist);
return _spriteFramesCache.isPlistUsed(plist);
}

void SpriteFrameCache::addSpriteFrame(SpriteFrame* frame, const std::string& frameName)
Expand Down Expand Up @@ -686,7 +674,7 @@ bool SpriteFrameCache::reloadTexture(const std::string& plist)
{
CCASSERT(plist.size()>0, "plist filename should not be nullptr");

if (_spriteFramesCache.hasPlist(plist)) {
if (_spriteFramesCache.isPlistUsed(plist)) {
_spriteFramesCache.erasePlistIndex(plist);
}
else
Expand Down Expand Up @@ -749,13 +737,6 @@ void SpriteFrameCache::PlistFramesCache::insertFrame(const std::string &plist, c
_indexFrame2plist[frame] = plist; //insert index frameName->plist
}

bool SpriteFrameCache::PlistFramesCache::isPlistUsed(const std::string &plist) const
{
//plist loaded && not empty
auto it = _indexPlist2Frames.find(plist);
return it != _indexPlist2Frames.end() && !it->second.empty();
}

bool SpriteFrameCache::PlistFramesCache::eraseFrame(const std::string &frame)
{
_spriteFrames.erase(frame); //drop SpriteFrame
Expand Down Expand Up @@ -811,10 +792,11 @@ bool SpriteFrameCache::PlistFramesCache::hasFrame(const std::string &frame) cons
return _indexFrame2plist.find(frame) != _indexFrame2plist.end();
}

bool SpriteFrameCache::PlistFramesCache::hasPlist(const std::string &plist) const
bool SpriteFrameCache::PlistFramesCache::isPlistUsed(const std::string &plist) const
{
return _indexPlist2Frames.find(plist) != _indexPlist2Frames.end();
}
auto frames = _indexPlist2Frames.find(plist);
return frames != _indexPlist2Frames.end() && frames->second.size() > 0;
}

SpriteFrame * SpriteFrameCache::PlistFramesCache::at(const std::string &frame)
{
Expand Down
4 changes: 1 addition & 3 deletions cocos/2d/CCSpriteFrameCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ class CC_DLL SpriteFrameCache : public Ref
void clear();

inline bool hasFrame(const std::string &frame) const;
inline bool hasPlist(const std::string &plist) const;
inline bool isPlistUsed(const std::string &plist) const;

inline SpriteFrame *at(const std::string &frame);
inline Map<std::string, SpriteFrame*>& getSpriteFrames();

inline bool isPlistUsed(const std::string &plist) const;

private:
Map<std::string, SpriteFrame*> _spriteFrames;
std::unordered_map<std::string, std::set<std::string>> _indexPlist2Frames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ USING_NS_CC;
SpriteFrameCacheTests::SpriteFrameCacheTests()
{
ADD_TEST_CASE(SpriteFrameCachePixelFormatTest);
ADD_TEST_CASE(SpriteFrameCacheLoadMultipleTimes);
}

SpriteFrameCachePixelFormatTest::SpriteFrameCachePixelFormatTest()
Expand Down Expand Up @@ -84,4 +85,43 @@ void SpriteFrameCachePixelFormatTest::loadSpriteFrames(const std::string &file,

SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file);
Director::getInstance()->getTextureCache()->removeTexture(texture);
}




SpriteFrameCacheLoadMultipleTimes::SpriteFrameCacheLoadMultipleTimes()
{
const Size screenSize = Director::getInstance()->getWinSize();

infoLabel = Label::create();
infoLabel->setAnchorPoint(Point(0.5f, 1.0f));
infoLabel->setAlignment(cocos2d::TextHAlignment::CENTER);
infoLabel->setPosition(screenSize.width * 0.5f, screenSize.height * 0.7f);
addChild(infoLabel);

// load atlas definition with specified PixelFormat and check that it matches to expected format
loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", Texture2D::PixelFormat::RGBA8888);
loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", Texture2D::PixelFormat::RGBA8888);
loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", Texture2D::PixelFormat::RGBA8888);
loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", Texture2D::PixelFormat::RGBA8888);


}


void SpriteFrameCacheLoadMultipleTimes::loadSpriteFrames(const std::string &file, cocos2d::Texture2D::PixelFormat expectedFormat)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
SpriteFrame *spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini.png");
Texture2D *texture = spriteFrame->getTexture();
const ssize_t bitsPerKB = 8 * 1024;
const double memorySize = 1.0 * texture->getBitsPerPixelForFormat() * texture->getContentSizeInPixels().width * texture->getContentSizeInPixels().height / bitsPerKB;
CC_ASSERT(texture->getPixelFormat() == expectedFormat);

const std::string textureInfo = StringUtils::format("%s: %.2f KB\r\n", texture->getStringForFormat(), memorySize);
infoLabel->setString(infoLabel->getString() + textureInfo);

SpriteFrameCache::getInstance()->removeSpriteFrameByName("grossini.png");
Director::getInstance()->getTextureCache()->removeTexture(texture);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ class SpriteFrameCachePixelFormatTest : public TestCase
private:
void loadSpriteFrames(const std::string &file, cocos2d::Texture2D::PixelFormat expectedFormat);

private:
cocos2d::Label *infoLabel;
};

class SpriteFrameCacheLoadMultipleTimes : public TestCase
{
public:
CREATE_FUNC(SpriteFrameCacheLoadMultipleTimes);

virtual std::string title() const override { return "Load same plist multiple times"; }
virtual std::string subtitle() const override { return "It shouldn't crash"; }

SpriteFrameCacheLoadMultipleTimes();

private:
void loadSpriteFrames(const std::string &file, cocos2d::Texture2D::PixelFormat expectedFormat);

private:
cocos2d::Label *infoLabel;
};