Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
24
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@galigalikun

cocos2dx twitterにスクリーンショット画像を投稿する

cocos2dx 3.3がリリースされてsaveToFileにcallbackが追加された。
http://www.cocos2d-x.org/news/387

・RenderTexture: add a callback function for saveToFile()`

いままではsaveToFileがいつ終わっているのかわからないかったので、runActionでDelayTimeしていた。
tests/cpp-testも同じようにDelayTimeしていたからこれしか方法がなかったと信じている。

callbackが追加されたことでDelayTimeで1秒とか根拠のない値を設定しないですむ。

GameScene.h
#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__

#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
#include "ProtocolShare.h"

class GameScene : public cocos2d::Layer
, public cocos2d::plugin::ShareResultListener
{
protected:
    cocos2d::RenderTexture* mTexture;
    cocos2d::plugin::ProtocolShare* mTwitter;
public:
    static cocos2d::Scene* createScene();

    GameScene();
    virtual ~GameScene();

    virtual bool init();
    virtual void onShareResult(cocos2d::plugin::ShareResultCode, const char*);

    void tweet(cocos2d::Ref*, cocos2d::extension::Control::EventType);

    CREATE_FUNC(GameScene);
};

#endif

GameScene.cpp
#include "GameScene.h"
#include "PluginManager.h"


Scene* GameScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = GameScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

GameScene::GameScene()
: mTwitter(nullptr)
, mTexture(nullptr)
{
}

GameScene::~GameScene()
{
    if(nullptr != mTwitter) {
        plugin::PluginManager::getInstance()->unloadPlugin("ShareTwitter");
        mTwitter = nullptr;
    }
    if(nullptr != mTexture) {
        mTexture->release();
        Director::getInstance()->getTextureCache()->removeUnusedTextures();
    }
}


bool GameScene::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

   Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto btn = ControlButton::create(Scale9Sprite::createWithSpriteFrameName("btn_tweet.png"));
    btn->setAdjustBackgroundImage(false);
    btn->setAnchorPoint(Vec2::ZERO);
    btn->setPosition(Vec2::ZERO);
    btn->addTargetWithActionForControlEvents(this, cccontrol_selector(GameScene::tweet), Control::EventType::TOUCH_DOWN);
    this->addChild(btn);



    mTwitter =  dynamic_cast<plugin::ProtocolShare*>(plugin::PluginManager::getInstance()->loadPlugin("ShareTwitter"));

    plugin::TShareDeveloperInfo pTwitterInfo;
    pTwitterInfo["TwitterKey"] = TWEET_KEY;
    pTwitterInfo["TwitterSecret"] = TWEET_SECRET;

#if defined(COCOS2D_DEBUG)
    mTwitter->setDebugMode(true);
#endif

    mTwitter->configDeveloperInfo(pTwitterInfo);
    mTwitter->setResultListener(this);

    mTexture = RenderTexture::create(visibleSize.width, visibleSize.height);
    mTexture->retain();
    mTexture->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));



    return true;
}

void GameScene::tweet(Ref* pSender, Control::EventType type)
{
    auto btn = static_cast<ControlButton*>(pSender);
    btn->setEnabled(false);

    mTexture->clear(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
    mTexture->begin();
    Director::getInstance()->getRunningScene()->visit();
    mTexture->end();

    // コールバック追加されたやったー!!
    mTexture->saveToFile("a.png", Image::Format::PNG, true, [&](RenderTexture* rt, const std::string& path) {
        plugin::TShareInfo pInfo;
       pInfo["SharedText"] = "tweet msg";
       pInfo["SharedImagePath"] = path;
       mTwitter->share(pInfo);

        btn->setEnabled(true);
    });

/*ちなみにいままではこう書いていた
    if(mTexture->saveToFile("a.png")) {
        this->runAction(Sequence::create(
            DelayTime::create(1.0f),
           CallFunc::create([this](){

                btn->setEnabled(true);

                plugin::TShareInfo pInfo;
                pInfo["SharedText"] = "tweet msg";
                pInfo["SharedImagePath"] = FileUtils::getInstance()->getWritablePath().append("a.png");
                mTwitter->share(pInfo);
            }),
           nullptr));
    }

*/




}

void GameScene::onShareResult(plugin::ShareResultCode ret, const char* msg)
{
    if(ret == plugin::kShareSuccess) {
        MessageBox(msg, "success");
    }
}

こんな感じでかけるようになる。

うれしい。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
24
Help us understand the problem. What are the problem?