How to take a screenshot of the current game using CCRenderTexture::saveBuffer method

I followed the test in cocos2d-x0.9.1,
but i only got is a blank texture,
i must lost some steps.

Could you please tell me your os platform, and type of mobile device?

thanks for your quick reply
i only tested it in win32, what i want is to catch the current game screen instead of the CCRenderTexture object
is there a difference between ios and win32?

maybe i should do something between
CCRenderTexture::begin() & CCRenderTexture::end()

hello, maybe you haven’t draw your scene to the texture

CCRenderTexture * tex = CCRenderTexture::renderTextureWithWidthAndHeight(int(screenWidth), int(screenHeight));
tex->setPosition(ccp(screenWidth/2, screenHeight/2));
tex->begin();
spriteScene->visit();
.......
tex->end();

and you could refer to RenderTextureTest

oh,it works
very nice of your guys,thank u

this is awesome! thanks!

but, how can i save it as a image on android or iphone to show it in the device galery?

to Peter:

you could use CCRenderTexture::saveBuffer() to save the image, and now the image could be saved as png or jpg.

thanks!

i just called this:

const char* file = "picture.png";
tex->saveBuffer(kCCImageFormatPNG, file);

it works on win32 emulator, but on android the app crashes!

ah ok crash is at CCFileUtils::getWriteablePath

http://www.cocos2d-x.org/boards/10/topics/3109?r=3111

ye, this bug has been resolved in the latest code on github.

It works correctly on Android? I have the latest cocos2d-x and getWriteablePath it doesn’t work for me.

However, I would like to store an image on the sdcard. Is this possible with the savebuffer method?

It only works on Android sdcards with permission:

in AndroidManifest.xml

Hi, I’m also trying to get the getWriteablePath and save buffer methods running with no luck, i have added

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to the manifest but even with that, my app crashes when I try to launch those methods, i have also tried to get the path from @ Environment.getExternalStorageDirectory();@ and add it to the saveBuffer method, but it also crashes. any solution? i can add my code if that helps. thanks

CCUserDefault will cause crash on 0.9.1, you can refer #744, fix it by the diff, or you can use latest code from git.

Thanks, I have downloaded the git project and now it doesn’t crash, but I’m not able to see my image in the android phone, where should it be saved??

/data/data/<apk-path

If saveBuffer returns a false, it means that is not saving the image correctly?

Hi again, finally I achieved to save the image into the sdCard, not sure why the saveBuffer method didn’t work for me, so after getting the path for the sdCard, I just added the saveBuffer code into my class :

CCImage *pImage = new CCImage(); tex->getUIImageFromBuffer(pImage, 0,0, 0, 0); pImage->saveToFile(ossFile.str().c_str());

and worked

hello, Jon.
i think there are two points you may check:
firstly, have you render what you want in the texture?
its architecture looks like this:
rend~~>begin;
sprite~~>visit();
rend->end();

secondly, saveBuffer has two override functions as follows, one is for filepath, and the other is for filename, do you use it correctly?
/* saves the texture into a file/
// para szFilePath the absolute path to save
// para x,y the lower left corner coordinates of the buffer to save
// pare nWidth,nHeight the size of the buffer to save
// when nWidth = 0 and nHeight = 0, the image size to save equals to buffer texture size
bool saveBuffer(const char szFilePath, int x = 0, int y = 0, int nWidth = 0, int nHeight = 0);
/
* saves the texture into a file. put format at the first argument, or ti will be overloaded with
* saveBuffer(const char szFilePath, int x = 0, int y = 0, int nWidth = 0, int nHeight = 0)/
// para name the file name to save
// para format the image format to save, here it supports kCCImageFormatPNG and kCCImageFormatJPG */
// para x,y the lower left corner coordinates of the buffer to save
// pare nWidth,nHeight the size of the buffer to save
// when nWidth = 0 and nHeight = 0, the image size to save equals to buffer texture size
bool saveBuffer(int format, const char *name, int x = 0, int y = 0, int nWidth = 0, int nHeight = 0);