Take Screenshot In Cocos2d-x C++
Cocos2d-x v3-
Sonar Systems adminLikes 0Problem Description
So you want the user to be able to take a screenshot to brag to their friends, in Cocos2d-x C++ it’s extremely simple.
Solution DescriptionVideo Solution
Is called after the screenshot has been captured
Header:
void afterCaptured( bool succeed, const std::string &outputFile );
Implementation:
void HelloWorld::afterCaptured( bool succeed, const std::string &outputFile ) { if (succeed) { auto sprite = Sprite::create(outputFile); this->addChild(sprite, 0); Size screenSize = Director::getInstance()->getWinSize(); sprite->setPosition(screenSize.width / 2, screenSize.height / 2); sprite->setScale(0.25); } else { log("Screen capture failed"); } }
Code to take screenshot:
utils::captureScreen( CC_CALLBACK_2(HelloWorld::afterCaptured, this), "Screenshot.png" );
-
abhishekLikes 0
Hi,
I tried to implement this in my cocos2d-x v3.0 c++ project. But I am not able to get the code to run. I have the following problems:
1 – I get an error that says: ‘use of undeclared identifier utils’
2 – I am not getting the captureScreen function after i type utils, the only related options i get are capture and captured
Am i doing something wrong?
Please help me out here as I really want to implement this feature in my current game.
Thanks
-
Sonar Systems adminLikes 0
Do you have the cocos2d namespace ?
-
abhishekLikes 0
hi..
i was able to get this to work after updating from cocos2d-x 3.0 to 3.5 !!
so i am able to get a screenshot of scale 25 percent on the screen
but now i also want to save this screenshot in iOS as well as ANDROID
will you please help me out with this?
i already tried this method, but i get an error
THIS IS WHAT I TRIED (the code in bold is what i wrote after your code)
auto sprite = Sprite::create(outputFile);
this->addChild(sprite);
Size screenSize = Director::getInstance()->getWinSize();
sprite->setPosition(screenSize.width/2, screenSize.height/2);
sprite->setScale(0.25);RenderTexture* renderTexture = RenderTexture::create(sprite->getBoundingBox().size.width, sprite->getBoundingBox().size.height, Texture2D::PixelFormat::RGBA8888);
renderTexture->begin();
sprite->visit();
renderTexture->end();
renderTexture->saveToFile("screenshot.png", Image::Format::PNG);
AND THIS IS THE ERROR I GET
XCODE DEBUGGER LINES:
Assert failed: Cannot add command while rendering
Assertion failed: (!_isRendering), function addCommand, file /Users/Abhishek/Desktop/PuzzleIslands/cocos2d/cocos/renderer/CCRenderer.cpp, line 367.THE FILE WHERE THE ERROR RE-DIRECTS TO:
This function in CCRenderer.cpp
void Renderer::addCommand(RenderCommand* command, int renderQueue)
{
CCASSERT(!_isRendering, "Cannot add command while rendering");
CCASSERT(renderQueue >=0, "Invalid render queue");
CCASSERT(command->getType() != RenderCommand::Type::UNKNOWN_COMMAND, "Invalid Command Type");_renderGroups[renderQueue].push_back(command);
}
-
Sonar Systems adminLikes 0
We don’t know how to save it to the phones gallery.
-
abhishekLikes 0
hi..
i followed this tutorial and was able to capture screenshot..
but there is a problem i am facing..
I AM USING THE EXACT SAME CODE AS YOU HAVE USED:utils::captureScreen( CC_CALLBACK_2(HelloWorld::afterCaptured, this), "Screenshot.png" );
but the problem is..
say if you have multiple items on the screen that can be moved around using drag and drop..
so if you capture screenshot and then move anything on the screen and then capture screenshot AGAIN..
i still get the original image.. the new one does not get saved..
so after you capture the screenshot for the first time.. thats the image that keeps getting displayed..
no matter how many more times you tap the capture screen button, i keep getting the first image only..
so can you please help me to fix this?
thanks
FOR NOW..
What i did is used a counter to rename screenshot.png..AppDelegate::getDelegate()->screenshot++;
std::string str = StringUtils::format("screenshot_%d.png", AppDelegate::getDelegate()->screenshot);
utils::captureScreen(CC_CALLBACK_2(Game05Scene::fn_capture, this), str);
this does solve the problem, because everytime i tap on the screenshot button..
a new name will be given to the screenshot
but is this the correct way?
will this code keep create multiple png files and can that cause memory issues?
thanks
-
Sonar Systems adminLikes 0
Replied to your other comment
-
amishaLikes 0
sir ,i implement the code for take the screen shot,but now i want to share this screen shot on facebook then how can i do share screen shot using below line ??
SonarCocosHelper::Facebook::Share("", "","My game iMage", "","sp.png");
-
Sonar Systems adminLikes 0
iOS or Android?
-
amishaLikes 0
ios
-
Sonar Systems adminLikes 0
Have you got the image working in a sprite.
-
fernando4relaxLikes 0
I didn't got the image working in a sprite. I get error in the sprite. Please help me
-
Sonar Systems adminLikes 0
What error did you get
-
fernando4relaxLikes 0
@Sonar Systems : i get error in attachment file. I run code in Xcode. I did log path of screenshot in log. You can see it. Please help me. Thanks
-
Sonar Systems adminLikes 0
Try this->addChild(“sprite name”);
-
fernando4relaxLikes 0
I try above but i stil get same error. I think “outputFile” has problem
-
Sonar Systems adminLikes 0
Try logging out outputFile to see what string it prints out so we can see if it is a correct path.
-
fernando4relaxLikes 1
Thanks Sonar Systems. I fixed it. Error cause by: folder contains screenshot image is hidden (default). I must show folder first on my computer then code run well.
This reply has been verified.
-
Sonar Systems adminLikes 0
Great to hear :D
Login to reply