So saveToFile() actually doesn't save to file immediately? Why?

Is this a joke or the developer lacks real-life experience?

saveToFile() relies on the #%@ render and it’s an async method? And what’s more fun is there is no callback for this “async” method.

…I have no words for such stupid design. U simply won’t be able to find any job in any other company.

Yeah, i got it, u guys want to show how “fast” cocos2d-x could be…just by dropping off all those important methods.

Can you elaborate with some code on what you are doing, platform, cocos2d-x version, etc, etc. Otherwise you are sort of stating a problem inside a black box.

I’m on my iPhone, so cannot type a lot.
It’s simple;
render texture* p= …
p->begin
P->end
P->savetofile
Uiimage* image =…

It’s not going to work since the file is not written.

ok, well when you get back to a computer please post and I’d be happy to test and involve those that would need to look at this.

It’s extremely easy to test, just visit a node in render texture and the call savetofile, the make a breakpoint to check if the file is written.

ok, see what you are saying… Something like:

cocos2d::Sprite* mySprite = cocos2d::Sprite::create("circle.png");
    
    cocos2d::RenderTexture* rtX = cocos2d::RenderTexture::create(mySprite->getContentSize().width, mySprite->getContentSize().height);
    rtX->retain();
    rtX->begin();
    mySprite->visit();
    rtX->end();
    rtX->saveToFile("JasonTesting.png");
    
    std::cout << "breakpoint..." << std::endl;

I placed a breakpoint in RenderTexture::saveToFile() and RenderTexture::onSaveToFile() and I see what you mean. Provided I’m using it right you seem correct. The saving occurs afterwards. I’ll ask one of the developers to have a look and chime in here.

Thanks for bringing this to my attention.

@likexx
Sorry about it. It is a history problem.
As you know, after v3.0, we have a new renderer, Node::visit will not draw itself immediately.
But we forgot to update this function to work with new renderer.
We will fix it soon, and thanks for pointing out it.

@zhangxm is this issue fixed in v3.3 beta?

Yep.
We added a call back function.

It’s an open source project lol, go write smth yourself

bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, std::function<void()> callback)
{
    saveToFile(fileName, format);
    onSaveToFileCallback = callback;
    return true;
}

void RenderTexture::onSaveToFile(const std::string& filename)
{
    Image *image = newImage(true);
    if (image)
    {
        image->saveToFile(filename.c_str(), true);
    }

    CC_SAFE_DELETE(image);

    if (onSaveToFileCallback) {
        Director::getInstance()->getScheduler()->performFunctionInCocosThread(onSaveToFileCallback);
        onSaveToFileCallback = nullptr;
    }
}

onSaveToFileCallback is std::function added to class declaration and initialized as nullptr in constructor.

Hi to all,

Sorry for replying to the old blog. But would like to share the workaround to use render texture in version 3.0 and above. I was facing the same issue while using RenderTexture. Finally I got success. Please have a look at my blog http://blog.snkitsolutions.com/cocos2d-x-version-3-0-rendertexture-savetofile/

Hope this may help to someone like me… :wink: