diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index d15cf4bf0b..b82eb8ddd8 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -413,34 +413,30 @@ void RenderTexture::visit(Renderer *renderer, const kmMat4 &parentTransform, boo bool RenderTexture::saveToFile(const std::string& filename) { - bool ret = false; - - Image *image = newImage(true); - if (image) - { - ret = image->saveToFile(filename); - } - - CC_SAFE_DELETE(image); - return ret; + return saveToFile(filename,Image::Format::JPG); } bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format) { - bool ret = false; CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG, "the image can only be saved as JPG or PNG format"); + + std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName; + _saveToFileCommand.init(_globalZOrder); + _saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile,this,fullpath); + + Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand); + return true; +} +void RenderTexture::onSaveToFile(const std::string& filename) +{ Image *image = newImage(true); if (image) { - std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName; - - ret = image->saveToFile(fullpath.c_str(), true); + image->saveToFile(filename.c_str(), true); } CC_SAFE_DELETE(image); - - return ret; } /* get buffer as Image */ diff --git a/cocos/2d/CCRenderTexture.h b/cocos/2d/CCRenderTexture.h index ff1803e634..2ca4a53f92 100644 --- a/cocos/2d/CCRenderTexture.h +++ b/cocos/2d/CCRenderTexture.h @@ -214,6 +214,7 @@ protected: CustomCommand _clearCommand; CustomCommand _beginCommand; CustomCommand _endCommand; + CustomCommand _saveToFileCommand; protected: //renderer caches and callbacks void onBegin(); @@ -221,6 +222,8 @@ protected: void onClear(); void onClearDepth(); + + void onSaveToFile(const std::string& fileName); kmMat4 _oldTransMatrix, _oldProjMatrix; kmMat4 _transformMatrix, _projectionMatrix; diff --git a/samples/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp b/samples/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp index 7d0aa48a50..b817ed6701 100644 --- a/samples/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/samples/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -145,12 +145,16 @@ void RenderTextureSave::saveImage(cocos2d::Ref *sender) _target->saveToFile(jpg, Image::Format::JPG); std::string fileName = FileUtils::getInstance()->getWritablePath() + jpg; - auto sprite = Sprite::create(fileName); - - sprite->setScale(0.3f); - addChild(sprite); - sprite->setPosition(Point(40, 40)); - sprite->setRotation(counter * 3); + auto action1 = DelayTime::create(1); + auto func = [&,fileName]() + { + auto sprite = Sprite::create(fileName); + addChild(sprite); + sprite->setScale(0.3f); + sprite->setPosition(Point(40, 40)); + sprite->setRotation(counter * 3); + }; + runAction(Sequence::create(action1, CallFunc::create(func), NULL)); CCLOG("Image saved %s and %s", png, jpg);