From 3a7b6731640b8abacd51e233bc3283cd0f29c3fc Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 12 Aug 2014 10:50:57 +0800 Subject: [PATCH] add callback for RenderTexture::saveToFile --- cocos/2d/CCRenderTexture.cpp | 19 +++++++++++++------ cocos/2d/CCRenderTexture.h | 5 +++-- .../RenderTextureTest/RenderTextureTest.cpp | 16 +++++----------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index 8a60335503..9cee8b1434 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -67,6 +67,7 @@ RenderTexture::RenderTexture() , _rtTextureRect(Rect::ZERO) , _fullRect(Rect::ZERO) , _fullviewPort(Rect::ZERO) +, _saveFileCallback(nullptr) { #if CC_ENABLE_CACHE_TEXTURE_DATA // Listen this event to save render texture before come to background. @@ -415,33 +416,36 @@ void RenderTexture::visit(Renderer *renderer, const Mat4 &parentTransform, uint3 _orderOfArrival = 0; } -bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA) +bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA, std::function callback) { std::string basename(filename); std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower); if (basename.find(".png") != std::string::npos) { - return saveToFile(filename, Image::Format::PNG, isRGBA); + return saveToFile(filename, Image::Format::PNG, isRGBA, callback); } else if (basename.find(".jpg") != std::string::npos) { if (isRGBA) CCLOG("RGBA is not supported for JPG format."); - return saveToFile(filename, Image::Format::JPG, false); + return saveToFile(filename, Image::Format::JPG, false, callback); } else { CCLOG("Only PNG and JPG format are supported now!"); } - return saveToFile(filename, Image::Format::JPG, false); + return saveToFile(filename, Image::Format::JPG, false, callback); } -bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA) + +bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA, std::function callback) { CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG, "the image can only be saved as JPG or PNG format"); if (isRGBA && format == Image::Format::JPG) CCLOG("RGBA is not supported for JPG format"); + _saveFileCallback = callback; + std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName; _saveToFileCommand.init(_globalZOrder); _saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA); @@ -457,7 +461,10 @@ void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA) { image->saveToFile(filename.c_str(), !isRGBA); } - + if(_saveFileCallback) + { + _saveFileCallback(this, filename); + } CC_SAFE_DELETE(image); } diff --git a/cocos/2d/CCRenderTexture.h b/cocos/2d/CCRenderTexture.h index 187acc18b3..3d016b0be6 100644 --- a/cocos/2d/CCRenderTexture.h +++ b/cocos/2d/CCRenderTexture.h @@ -103,12 +103,12 @@ public: /** saves the texture into a file using JPEG format. The file will be saved in the Documents folder. Returns true if the operation is successful. */ - bool saveToFile(const std::string& filename, bool isRGBA = true); + bool saveToFile(const std::string& filename, bool isRGBA = true, std::function callback = nullptr); /** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder. Returns true if the operation is successful. */ - bool saveToFile(const std::string& filename, Image::Format format, bool isRGBA = true); + bool saveToFile(const std::string& filename, Image::Format format, bool isRGBA = true, std::function callback = nullptr); /** Listen "come to background" message, and save render texture. It only has effect on Android. @@ -214,6 +214,7 @@ protected: CustomCommand _beginCommand; CustomCommand _endCommand; CustomCommand _saveToFileCommand; + std::function _saveFileCallback; protected: //renderer caches and callbacks void onBegin(); diff --git a/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp b/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp index 334d578b79..c75eb75cd3 100644 --- a/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -135,25 +135,19 @@ void RenderTextureSave::saveImage(cocos2d::Ref *sender) char png[20]; sprintf(png, "image-%d.png", counter); - char jpg[20]; - sprintf(jpg, "image-%d.jpg", counter); - - _target->saveToFile(png, Image::Format::PNG); - _target->saveToFile(jpg, Image::Format::JPG); - std::string fileName = FileUtils::getInstance()->getWritablePath() + jpg; - auto action1 = DelayTime::create(1); - auto func = [&,fileName]() + auto callback = [&](RenderTexture* rt, const std::string& path) { - auto sprite = Sprite::create(fileName); + auto sprite = Sprite::create(path); addChild(sprite); sprite->setScale(0.3f); sprite->setPosition(Vec2(40, 40)); sprite->setRotation(counter * 3); }; - runAction(Sequence::create(action1, CallFunc::create(func), nullptr)); + + _target->saveToFile(png, Image::Format::PNG, true, callback); - CCLOG("Image saved %s and %s", png, jpg); + CCLOG("Image saved %s", png); counter++; }