From ad9c104cfd8a975f969afb71437120194be2b20d Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Fri, 7 Mar 2014 17:41:51 +0800 Subject: [PATCH 1/2] migrate saveToFile compatible with new renderer --- cocos/2d/CCRenderTexture.cpp | 28 ++++++++----------- cocos/2d/CCRenderTexture.h | 3 ++ .../RenderTextureTest/RenderTextureTest.cpp | 16 +++++++---- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index d15cf4bf0b..cc242f98f1 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 false; +} +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/tests/Classes/RenderTextureTest/RenderTextureTest.cpp b/tests/Classes/RenderTextureTest/RenderTextureTest.cpp index 7d0aa48a50..b817ed6701 100644 --- a/tests/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/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); From e15d0a9d204dd9d3e8780211fe94691332f53dcb Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 10 Mar 2014 17:42:27 +0800 Subject: [PATCH 2/2] RenderTexture saveToFile will return true --- cocos/2d/CCRenderTexture.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index cc242f98f1..b82eb8ddd8 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -425,7 +425,7 @@ bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format _saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile,this,fullpath); Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand); - return false; + return true; } void RenderTexture::onSaveToFile(const std::string& filename)