How to take screenshot of the scene in cocos2dx 3.3? - c++

I Am developing a game and i want to take scene screen shot..How it possible in cocos2dx 3.3?Please tell me
My Code is Given Below
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCRenderTexture * tex = CCRenderTexture::create(screenSize.width, screenSize.height);
tex->setPosition(ccp(screenSize.width/2, screenSize.height/2));
tex->begin();
this->getParent()->visit();
tex->end();
tex->saveToFile("Image_Save.png", kCCImageFormatPNG);

use this
utils::captureScreen( CC_CALLBACK_2(GameScene::afterCaptured, this), "screenshot.png");
//callback function
void GameScene::afterCaptured(bool succeed, const std::string &outputFile)
{
if (succeed) {
log(" screen shot%s", outputFile.c_str());
}
}

Just Replace
tex->saveToFile("Image_Save.png", Image::Format::PNG);
Instead of
tex->saveToFile("Image_Save.png", kCCImageFormatPNG);
It might be surely help you

Related

cocos2d-x v3 c++ Drop shadow cocos2d::Sprite

As far as I've found out, cocos doesn't offer a simple filter handling like AS3 for example does.
My situation:
I want to add a realtime shadow to an cocos2d::Sprite.
For example I would like to do something like this (similar to AS3):
auto mySprite = Sprite::createWithSpriteFrameName("myCharacter.png");
DropShadowFilter* dropShadow = new DropShadowFilter();
dropShadow->distance = 0;
dropShadow->angle = 45;
dropShadow->color = 0x333333;
dropShadow->alpha = 1;
dropShadow->blurX = 10;
dropShadow->blurY = 10;
dropShadow->strength = 1;
dropShadow->quality = 15;
mySprite->addFilter(dropShadow);
This should add a shadow to my Sprite to achieve an result like this:
Adobe Drop Shadow Example
Could you help me please?
There isn't any built in support for shadows on Sprites in Cocos2D-X.
The best option, performance-wise, would be to place your shadows in your sprite images already, instead of calculating and drawing them in the code.
Another option is to sub-class Sprite and override the draw method so that you duplicate the sprite and apply your effects and draw it below the original.
One possible way to achieve that is with this snippet from this thread on the Cocos forum. I can't say that I completely follow what this code does with the GL transforms, but you can use this as a starting point to experiment.
void CMySprite::draw()
{
// is_shadow is true if this sprite is to be considered like a shadow sprite, false otherwise.#
if (is_shadow)
{
ccBlendFunc blend;
// Change the default blending factors to this one.
blend.src = GL_SRC_ALPHA;
blend.dst = GL_ONE;
setBlendFunc( blend );
// Change the blending equation to thi in order to subtract from the values already written in the frame buffer
// the ones of the sprite.
glBlendEquationOES(GL_FUNC_REVERSE_SUBTRACT_OES);
}
CCSprite::draw();
if (is_shadow)
{
// The default blending function of cocos2d-x is GL_FUNC_ADD.
glBlendEquationOES(GL_FUNC_ADD_OES);
}
}

cocos2d - Sprite doesn't show properly

I am using cocos2dx v3.11.1 with c++ to create simple game for iOS platform. But I run into strange issue with sprite movement.
Definition of main objects looks like that:
Sprite *logoBackSprite;
SpriteFrameCache *cache;
SpriteBatchNode *batchNode;
I create sprite from the plist like that:
cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("game_sprites1.plist");
batchNode = SpriteBatchNode::create("game_sprites1.png");
addChild(batchNode);
logoBackSprite = Sprite::createWithSpriteFrameName(fileName);
logoBackSprite->setPosition(Vec2(centerPoint.x-4, centerPoint.y + 200-43));
addChild(logoBackSprite, 0);
After that I call function which must perform simple moving animation for sprite. Code of this function looks like that:
void MainMenuScene::playMenuAnimations() {
float startDelay = 0.0f;
auto windowSize = Director::getInstance()->getVisibleSize();
Point centerPoint = Vec2(windowSize.width/2, windowSize.height/2);
logoBackSprite->stopAllActions();
logoBackSprite->setOpacity(0);
logoBackSprite->runAction(Sequence::create(DelayTime::create(startDelay + 0.5 + 0.75),
FadeTo::create(0.5f, 255),
nullptr));
}
But when I run application - nothing happens. Could you point me what I am making wrong? Thank you.
EDIT After few hours of experiments, I have noticed, that runAction method of sprite objects doesn't work. For example, I have created vector of sprite frames and I want to run animation. When I run application I can see this sprite, but animation still does not work. Maybe, I missed smth important?
EDIT2
Today I have noticed that any action works prefect on previous scene.
Issue solved. Problem was in bad configuration of scene where I want to run animations.
I should override onEnter() and onExit() methods. In onEnter() method of scene I call
Layer::onEnter();
Also in scene class I create init() method like that:
bool MainMenuScene::init() {
if ( !Layer::init() )
{
return false;
}
return true;
}
And animation runs perfectly!

How to remove a sprite permanently in Cocos2d-x

I am developing a game using cocos2d-x. I want to remove sprites permanently. I have two sprites & making collision between the sprites. When collision happens, i want to remove those sprites permanently. I am using the following code to making collision & remove sprites.
CCARRAY_FOREACH(_sprrand24, stwentyfour)
{
CCSize size=sprrand24->getContentSize();
CCSprite *sprrand24 = dynamic_cast<CCSprite*>(stwentyfour);
CCRect sprrand24Rect = CCRectMake(
sprrand24->getPosition().x - (size.width/2),
sprrand24->getPosition().y - (size.height/2),
size.width/2,
size.height/2);
CCARRAY_FOREACH(_sprrand25, stwentyfive)
{
CCSize size=sprrand25->getContentSize();
CCSprite *sprrand25 = dynamic_cast<CCSprite*>(stwentyfive);
CCRect sprrand25Rect = CCRectMake(
sprrand25->getPosition().x - (size.width/2),
sprrand25->getPosition().y - (size.height/2),
size.width/2,
size.height/2);
if (sprrand24Rect.intersectsRect(sprrand25Rect))
{
this->removeChild(sprrand24, true);
this->removeChild(sprrand25, true);
}
}
}
To remove the sprite you can use
sprrand24.removeFromParentAndCleanup(true);
if you assign each of your Sprites a tag, you can then do removeChildByTag(tag);
sprite->setTag(99); // i made this up
this->removeChildByTag(99);

SDL_FillRect Not Drawing?

I'm making a game in C++ with SDL, and I want to render particles with SDL_FillRect().
I've played with the code for hours, but no matter what I do, the particles are not drawing.
This is the code in my Render function (I made sure that I was in fact calling the function):
void Particle::Render()
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
//rect.w = Particle::Particle_Size;
//rect.h = Particle::Particle_Size;
rect.w = 8;
rect.h = 8;
surface = SDL_CreateRGBSurface(SDL_SWSURFACE,8,8,32,0,0,0,0);
if(SDL_FillRect(surface,&rect,SDL_MapRGB(surface->format,0,0,0)) != 0) printf("Error");
//SDL_RenderCopy(renderer,texture,NULL,&rect);
}
The console isn't printing "Error", so the SDL_FillRect() is successful. However, no rects are being drawn to the screen.
I tried creating a texture with SDL_CreateTextureFromSurface() with that surface passed in, and then used SDL_RenderCopy, which is commented out in the above function, but it worked before I commented it out. I want to use SDL_FillRect so I could have colored textures though.
Am I missing anything?
I think you could use this function to do what you are looking for:
SDL_RenderFillRect()
https://wiki.libsdl.org/SDL_RenderFillRect
You would have to set the renderer color before with:
SDL_SetRenderDrawColor();
I think you could also update the window surface to get what you have to work.
That would use
SDL_UpdateWindowSurface().
https://wiki.libsdl.org/SDL_UpdateWindowSurface?highlight=%28%5CbCategoryVideo%5Cb%29%7C%28CategoryEnum%29%7C%28CategoryStruct%29
Hope it helps!
The SDL_CreateRGBSurface() function creates an off-screen surface. If you want to draw to the screen, you will have to draw to the surface returned by SDL_GetWindowSurface().
That is, if you are using SDL 2.0.

Collision detection between layers

I have a few layers, which holds CCSprites .
I have a main CCScene that is adding these layers at the start.
I have to check collision between sprites from different layers .
I know how to check a simple collision with CGRectContains , my problem is , that i need some kind of a class that holds all the sprites positions from all layers ,and to check each tick for collision.
Question: what is the right way to create such a class,that save/check sprites from all layers on the scene ?
THanks .
If you don't care about empty space, then you can just set self.contentSize for every layer. You can do it like this:
CGRect rect = CGRectNull;
for (CCNode *node in self.children) {
rect = CGRectUnion(rect, node.boundingBox);
}
self.contentSize = rect.size;
But if you care about empty space, then you need to use something like this:
// put it somewhere outside class implementation
static inline BOOL CCLayerIntersectsCCLayer(CCLayer l1, CCLayer l2) {
for (CCNode *n1 in l1.children) {
for (CCNode *n2 in l2.children) {
CGRect r1 = n1.boundingBox;
CGRect r2 = n2.boundingBox;
r1.origin = [l1.parent convertToNodeSpace:r1.origin];
r2.origin = [l2.parent convertToNodeSpace:r2.origin];
if (CGRectIntersectsCGRect(r1, r2)) {
return YES;
}
}
}
return NO;
}
But you must understand, that all sprites (even circles) are actually rectangles. So if you need very precise collision detection, then you should use box2d in your project. If you are interesting in this, here are some links to read:
Box2d
Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls
Box2D Tutorial for iOS: How To Use Box2D For Just Collision Detection with Cocos2D iPhone