SpriteTest fix

* Image is Ref class, please new & release it. Don't allocate it on stack.
* Fixes complicated logic of getting string from file.
This commit is contained in:
James Chen 2016-01-15 00:54:42 +08:00
parent b04e4754d7
commit 41c0881ab5
1 changed files with 8 additions and 25 deletions

View File

@ -1868,27 +1868,16 @@ void SpriteFramesFromFileContent::onEnter()
SpriteTestDemo::onEnter(); SpriteTestDemo::onEnter();
auto s = Director::getInstance()->getWinSize(); auto s = Director::getInstance()->getWinSize();
std::string plist_content; std::string plist_content = FileUtils::getInstance()->getStringFromFile(sheetName() + ".plist");
{ Data image_content = FileUtils::getInstance()->getDataFromFile(sheetName() + ".png");
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(sheetName() + ".plist");
Data data = FileUtils::getInstance()->getDataFromFile(fullPath);
if (!data.isNull())
plist_content.assign((const char*)data.getBytes(), data.getSize());
}
std::string image_content; Image* image = new (std::nothrow) Image();
{ image->initWithImageData((const uint8_t*)image_content.getBytes(), image_content.getSize());
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(sheetName() + ".png");
Data data = FileUtils::getInstance()->getDataFromFile(fullPath);
if (!data.isNull())
image_content.assign((const char*)data.getBytes(), data.getSize());
}
Image image;
image.initWithImageData((const uint8_t*)image_content.c_str(), image_content.size());
Texture2D* texture = new (std::nothrow) Texture2D(); Texture2D* texture = new (std::nothrow) Texture2D();
texture->initWithImage(&image); texture->initWithImage(image);
texture->autorelease(); texture->autorelease();
CC_SAFE_RELEASE(image);
auto cache = SpriteFrameCache::getInstance(); auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFileContent(plist_content, texture); cache->addSpriteFramesWithFileContent(plist_content, texture);
@ -1918,13 +1907,7 @@ void SpriteFramesFromFileContent::onExit()
{ {
SpriteTestDemo::onExit(); SpriteTestDemo::onExit();
std::string plist_content; std::string plist_content = FileUtils::getInstance()->getStringFromFile("animations/grossini.plist");
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename("animations/grossini.plist");
Data data = FileUtils::getInstance()->getDataFromFile(fullPath);
if (!data.isNull())
plist_content.assign((const char*)data.getBytes(), data.getSize());
}
SpriteFrameCache::getInstance()->removeSpriteFramesFromFileContent(plist_content); SpriteFrameCache::getInstance()->removeSpriteFramesFromFileContent(plist_content);
} }