Adding Sceenshot method in cocos-2dx project

Hi all,
I am very new to cocos-2dx. I am wondering if anyone already added screenshot method to there game when user want to share their score to other people.
I would appreciate if someone can help me out. Not writing code for me but at least tell me which file is right place. to add the method.
I have gameScene, when player dies it goes to ScoreScene.cpp.
In score scene I have sharing button which pops up sharing action sheet.
any clue or pointer would be appreciated.

Thanks,
Alix

If you only want to take a screenshot when the share button is pressed, maybe place the screenshot code with the code that gets called when you press the share button.

If you want to know how to take a screenshot, a RenderTexture is probably your best option. A RenderTexture will create a blank image that you can draw nodes onto. I don’t know if this is the best way to do it, but if you use

auto winSize = Director::getInstance()->getWinSize();
auto renderTexture = RenderTexture::create(winSize.width, winSize.height);
renderTexture->begin();
Director::getInstance()->getRunningScene()->visit();
renderTexture->end();

you now have a RenderTexture node that should contain a copy of the screen. You can then add the RenderTexture to your scene to show its image or use its saveToFile function to save the screenshot to file. You can also use renderTexture->getSprite()->getTexture() if you want to use its texture.

If you want to take a screenshot of a specific node or layer, use visit, e.g. node->visit();, instead of visiting the running scene. Also, I believe you can visit nodes after visiting the scene or other nodes if you want to place text, logos or borders on top of your screenshot.

Thx a lot man. your code immensely helped me. It was good starting point. By the end of day I solved that screenshot problem. Credit goes to you. I am just wondering if I dont want to tale whole screenshot . I mean Image get too big like on ipad. I was thinking if I can I just crop or only take screenshot of score area or something like that

This part saying how big area will be rendered,
btw you can do ->visit() Director::getInstance()->getRunningScene()->visit(); to any layer /sprite in your scene , even if it is not visible … and it will be part of screenshot.

Taking a screenshot of part of the screen is a little tricky. You need to know the size of the image you want to take and adjust the size of the RenderTexture to that size like energyy said.

But the RenderTexture seems to work by placing a blank image of the size you chose down the bottom left of the screen. When you visit a node, it will draw any part of that node within the area of that blank image onto the blank image.

What this means is, whatever you want a screenshot of, you need to move it down and left until it is perfectly within the area of the RenderTexture.

Actualy there is this build in function :

utils::capturescreen()

maybe try that out

Good to know they added this function. Although looking at the documentation it takes a capture of the entire screen.

If you wanted to use the capturescreen function, you would have to then load the image as a texture and create a sprite using a rect that matches the area of the image you want. Of course that only works for showing the image on screen. When you share the image, it will still be of the entire screen and you would somehow have it cut it down to size. RenderTexture method allows you to capture just the part of the screen you want, avoid saving to file if you don’t need to and have the exact right image file to share if you need to.

I wonder if the capturescreen function would be easy to modify to specify a region of the screen to capture instead…

Thanks everybosy for replying.

This is how I solved this problem, Of cource @grimfate (Kevin) helped a lot. Just want to return to community. if someone else looking for answer. here is my Method. It working great. taking full screen screenshot. its taking a screenshot and save file in documents directory which can be user to create UIImage later.

void Helper::takeScreenShot(){
    auto winSize = Director::getInstance()->getWinSize();
    auto renderTexture = RenderTexture::create(winSize.width, winSize.height);
    renderTexture->begin();
    Director::getInstance()->getRunningScene()->visit();
    renderTexture->end();
    renderTexture->saveToFile(kSShotFullName, Image::Format::PNG, true, [&] (RenderTexture * rt, const std::string & path) {
        
        CCLOG("Image finally saved…");
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *pathUrl = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        NSString *pathToDocumentDir = [pathUrl.path stringByAppendingString:@"/"];
        
        UIImage *ssImage = [UIImage imageWithContentsOfFile:[pathToDocumentDir stringByAppendingString:@"someFile.png"]];
        
        showActivityControllerWithScreenshot(ssImage);
        
    });
}
1 Like