mirror of https://github.com/axmolengine/axmol.git
add callback for RenderTexture::saveToFile
This commit is contained in:
parent
bed1ccf0f3
commit
3a7b673164
|
@ -67,6 +67,7 @@ RenderTexture::RenderTexture()
|
||||||
, _rtTextureRect(Rect::ZERO)
|
, _rtTextureRect(Rect::ZERO)
|
||||||
, _fullRect(Rect::ZERO)
|
, _fullRect(Rect::ZERO)
|
||||||
, _fullviewPort(Rect::ZERO)
|
, _fullviewPort(Rect::ZERO)
|
||||||
|
, _saveFileCallback(nullptr)
|
||||||
{
|
{
|
||||||
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
||||||
// Listen this event to save render texture before come to background.
|
// 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;
|
_orderOfArrival = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA)
|
bool RenderTexture::saveToFile(const std::string& filename, bool isRGBA, std::function<void (RenderTexture*, const std::string&)> callback)
|
||||||
{
|
{
|
||||||
std::string basename(filename);
|
std::string basename(filename);
|
||||||
std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower);
|
std::transform(basename.begin(), basename.end(), basename.begin(), ::tolower);
|
||||||
|
|
||||||
if (basename.find(".png") != std::string::npos)
|
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)
|
else if (basename.find(".jpg") != std::string::npos)
|
||||||
{
|
{
|
||||||
if (isRGBA) CCLOG("RGBA is not supported for JPG format.");
|
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
|
else
|
||||||
{
|
{
|
||||||
CCLOG("Only PNG and JPG format are supported now!");
|
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<void (RenderTexture*, const std::string&)> callback)
|
||||||
{
|
{
|
||||||
CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG,
|
CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG,
|
||||||
"the image can only be saved as JPG or PNG format");
|
"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");
|
if (isRGBA && format == Image::Format::JPG) CCLOG("RGBA is not supported for JPG format");
|
||||||
|
|
||||||
|
_saveFileCallback = callback;
|
||||||
|
|
||||||
std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;
|
std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;
|
||||||
_saveToFileCommand.init(_globalZOrder);
|
_saveToFileCommand.init(_globalZOrder);
|
||||||
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA);
|
_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);
|
image->saveToFile(filename.c_str(), !isRGBA);
|
||||||
}
|
}
|
||||||
|
if(_saveFileCallback)
|
||||||
|
{
|
||||||
|
_saveFileCallback(this, filename);
|
||||||
|
}
|
||||||
CC_SAFE_DELETE(image);
|
CC_SAFE_DELETE(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -103,12 +103,12 @@ public:
|
||||||
/** saves the texture into a file using JPEG format. The file will be saved in the Documents folder.
|
/** 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.
|
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<void (RenderTexture*, const std::string&)> callback = nullptr);
|
||||||
|
|
||||||
/** saves the texture into a file. The format could be JPG or PNG. The file will be saved in the Documents folder.
|
/** 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.
|
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<void (RenderTexture*, const std::string&)> callback = nullptr);
|
||||||
|
|
||||||
/** Listen "come to background" message, and save render texture.
|
/** Listen "come to background" message, and save render texture.
|
||||||
It only has effect on Android.
|
It only has effect on Android.
|
||||||
|
@ -214,6 +214,7 @@ protected:
|
||||||
CustomCommand _beginCommand;
|
CustomCommand _beginCommand;
|
||||||
CustomCommand _endCommand;
|
CustomCommand _endCommand;
|
||||||
CustomCommand _saveToFileCommand;
|
CustomCommand _saveToFileCommand;
|
||||||
|
std::function<void (RenderTexture*, const std::string&)> _saveFileCallback;
|
||||||
protected:
|
protected:
|
||||||
//renderer caches and callbacks
|
//renderer caches and callbacks
|
||||||
void onBegin();
|
void onBegin();
|
||||||
|
|
|
@ -135,25 +135,19 @@ void RenderTextureSave::saveImage(cocos2d::Ref *sender)
|
||||||
|
|
||||||
char png[20];
|
char png[20];
|
||||||
sprintf(png, "image-%d.png", counter);
|
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 callback = [&](RenderTexture* rt, const std::string& path)
|
||||||
auto action1 = DelayTime::create(1);
|
|
||||||
auto func = [&,fileName]()
|
|
||||||
{
|
{
|
||||||
auto sprite = Sprite::create(fileName);
|
auto sprite = Sprite::create(path);
|
||||||
addChild(sprite);
|
addChild(sprite);
|
||||||
sprite->setScale(0.3f);
|
sprite->setScale(0.3f);
|
||||||
sprite->setPosition(Vec2(40, 40));
|
sprite->setPosition(Vec2(40, 40));
|
||||||
sprite->setRotation(counter * 3);
|
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++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue