24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-12-16

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");
	}
}

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

うれしい。

24
23
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?