How to get a screenshot in cocos2dx-3.0 rc0

1,save a screenshot

Size s = Director::getInstance() -> getWinSize();
auto renderTexture = RenderTexture::create(s.width, s.height, Texture2D::PixelFormat::RGBA8888);
renderTexture -> begin();
Director::getInstance()->getRunningScene()->visit();
renderTexture->end();
renderTexture->saveToFile(“coco2d-x-screenshot.png”,Image::Format::PNG);

2, get you saved screenshot
std::string fileName = FileUtils::getInstance()->getWritablePath() + “coco2d-x-screenshot.png”;
auto sprite = Sprite::create(fileName);
addChild(sprite);
sprite->setScale(0.3f);
sprite->setPosition(Point(40, 40));

1 Like

Hi yuye,
Thank you!! it’s always nice to know this…
I have a problem can you help me out?

Cheers,
MadMax

Thanks @yuye

Can we use this to make a video( say 24fps) out of the screenshots?
Also, is there a way to record the sound for playback?

@indygamer you just can record it by youself。

i wouldn’t recommend you to make a video, its going to slow your game down significantly

@yuye @Wuhao

The video playback is a feature of the game. It is not for a explainer video or a gameplay video.

It is for in game - video playback feature where players can rewind and watch their gameplay.

Is this possible (without slowing down game) via screenshot approach via CCDirector save image.

Alternatively, I can record the player input moves in a stack and re render the scenes during rewind.

I want to generate the slow motion video playback of the users gameplay in reverse direction while rewinding - like in a movie playback.

the best way to implement replays is to re-run the game

-you must use fixed update delta time,
-you must modify you random number to seeded random number, and use the same seed

Hi Yuye,
Could you please help, following code is not working ,i am using cocos2dx 3.0 -> here is the code. I think problem is with opengl portion , may be i am doing it wrong …

RenderTexture* rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(color.r, color.g, color.b, color.a);

float gradientAlpha = 0.7f;
Point vertices[4];
Color4F colors[4];
int nVertices = 0;

vertices[nVertices] = Point(0, 0);
colors[nVertices++] = Color4F(0, 0, 0, 0);
vertices[nVertices] = Point(textureWidth, 0);
colors[nVertices++] = Color4F(0, 0, 0, 0);
vertices[nVertices] = Point(0, textureHeight);
colors[nVertices++] = Color4F(0, 0, 0, gradientAlpha);
vertices[nVertices] = Point(textureWidth, textureHeight);
colors[nVertices++] = Color4F(0, 0, 0, gradientAlpha);

// Set the shader program for OpenGL
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
CC_NODE_DRAW_SETUP();

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);


Sprite* noise = Sprite::create("Noise-hd.png");
BlendFunc blendFunc;
blendFunc.src = GL_DST_COLOR;
blendFunc.dst = GL_ZERO;
noise->setBlendFunc(blendFunc);
noise->setPosition(Point(textureWidth / 2, textureHeight/2));
noise->visit(); 
rt->end();
Sprite* sprite = Sprite::createWithTexture(rt->getSprite()->getTexture());
return sprite;