Merge pull request #5654 from dabingnn/develop_RenderTextureImprovement

Develop render texture improvement
This commit is contained in:
minggo 2014-03-11 18:50:51 +08:00
commit b2e27947a3
3 changed files with 25 additions and 22 deletions

View File

@ -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 */

View File

@ -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;

View File

@ -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);