Search code examples
androidcocos2d-xscreenshotsystemtime

Cocos2dx save image with system time for Android


I am using this code for saving a screenshot

 Size size = Director::getInstance()->getWinSize();
    auto renderTexture = RenderTexture::create((size.width/5)*3.98, (size.height/5)*3.45, Texture2D::PixelFormat::RGBA8888);
    renderTexture->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f);
    Director::getInstance()->getRunningScene()->visit();
    renderTexture->end();
    renderTexture->saveToFile("screenshot.png" , kCCImageFormatPNG);

How can I save the image file using current system time as the filename like " screenshot" + current time +".png"?


Solution

  • To get system time you can simply use the time() function : docs

    As for glueing it into a string together you can use std::stringstream for example.

    #include <sstream>
    #include <time.h>
    
    //--- in your save method ---
    std::stringstream filename;
    filename << "screenshot_" << time(NULL) << ".png";
    
    renderTexture->saveToFile(filename.str(), kCCImageFormatPNG);