根据某个层来截图

#include "HelloWorldScene.h"


USING_NS_CC;


CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

    return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();


    /

    // 2. add a menu item with "X" image, which is clicked to quit the program

    //    you may modify it.


    // add a "close" icon to exit the progress. it's an autorelease object

    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(

                                        "CloseNormal.png",

                                        "CloseSelected.png",

                                        this,

                                        menu_selector(HelloWorld::menuCloseCallback));

    

pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,

                                origin.y + pCloseItem->getContentSize().height/2));


    // create menu, it's an autorelease object

    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);

    pMenu->setPosition(CCPointZero);

    this->addChild(pMenu, 1);


    /

    // 3. add your codes below...


    // add a label shows "Hello World"

    // create and initialize a label

    CCSprite * sp2 = CCSprite::create("Default-568h@2x.png");


    // position the sprite on the center of the screen

    sp2->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    this->addChild(sp2);

    

    

    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

    

    // position the label on the center of the screen

    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,

                            origin.y + visibleSize.height - pLabel->getContentSize().height));


    // add the label as a child to this layer

    this->addChild(pLabel, 1);


    // add "HelloWorld" splash screen"

    sp = CCSprite::create("HelloWorld.png");


    // position the sprite on the center of the screen

    sp->setPosition(ccp(500,500));


    // add the sprite as a child to this layer

    this->addChild(sp, 0);

   

    return true;

}



void HelloWorld::menuCloseCallback(CCObject* pSender)

{

    //截图

    CCRenderTexture* renderTexture = CCRenderTexture::create(sp->getContentSize().width,sp->getContentSize().height);

    renderTexture->ignoreAnchorPointForPosition(true);


    renderTexture->begin();

    // 为了从左下角开始截图

    sp->setPosition(ccp(sp->getContentSize().width/2,sp->getContentSize().height/2));

    sp->visit();

    renderTexture->end();

    

    

    sp->setPosition(ccp(500, 500));

    const std::string& fileName = "xdd.png";

    auto ddd = cocos2d::CCFileUtils::sharedFileUtils()->getWritablePath()+fileName;

    

    const char *string=ddd.c_str();

    CCLOG("%s",string);

    

    renderTexture->saveToFile(string);


    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值