読者です 読者をやめる 読者になる 読者になる

cocos2d-x-3.0beta2のプロジェクトをgradle化してみる(x-plugin使用編<AdMob,twitter plugin)

自メモ)
記事分割
 分割元:


で実際にPluginを使うコードを書いてみる


irof
├── Classes
のコードをいじる

  • AppDelegate.h
#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"
#include "ProtocolAds.h"

/**
@brief    The cocos2d Application.

The reason for implement as private inheritance is to hide some interface call by Director.
*/
class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();
    virtual ~AppDelegate();

    /**
    @brief    Implement Director and Scene init code here.
    @return true    Initialize success, app continue.
    @return false   Initialize failed, app terminate.
    */
    virtual bool applicationDidFinishLaunching();

    /**
    @brief  The function be called when the application enter background
    @param  the pointer of the application
    */
    virtual void applicationDidEnterBackground();

    /**
    @brief  The function be called when the application enter foreground
    @param  the pointer of the application
    */
    virtual void applicationWillEnterForeground();

    bool init();

    cocos2d::plugin::ProtocolAds* getAds();
    static AppDelegate* getInstance();

protected:
    cocos2d::plugin::ProtocolAds* AdsAdmob;
    static AppDelegate * sm_pSharedAppDelegate; //インスタンス参照
};

#endif // _APP_DELEGATE_H_
  • AppDelegate.cpp
#include "AppDelegate.h"

#include "HelloWorldScene.h"
#include "PluginManager.h"

USING_NS_CC;
using namespace cocos2d::plugin;
typedef std::pair<std::string, std::string> TStrStrPair;

AppDelegate * AppDelegate::sm_pSharedAppDelegate = 0; //インスタンス参照の初期化
AppDelegate* AppDelegate::getInstance()
{
    return sm_pSharedAppDelegate;
}

AppDelegate::AppDelegate() {
	AdsAdmob = NULL;
	sm_pSharedAppDelegate = this; //AppDelegateの自己参照をセット
}

cocos2d::plugin::ProtocolAds* AppDelegate::getAds(){
	if(AdsAdmob!=NULL)return AdsAdmob;
	AdsAdmob = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdsAdmob"));
	if(AdsAdmob==NULL){
		CCLOG("AdsAdmob is NULL");
	}
	return AdsAdmob;
}

bool AppDelegate::init(){
	AdsAdmob = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdsAdmob"));
	if(AdsAdmob==NULL){
		CCLOG("AdsAdmob is NULL");
		return false;
	}

	TAdsDeveloperInfo infoM;
	infoM["AdmobID"]="1234567890";
	AdsAdmob->configDeveloperInfo(infoM);
	AdsAdmob->setDebugMode(true);

	return true;
}

AppDelegate::~AppDelegate() 
{
	PluginManager::getInstance()->unloadPlugin("AdsAdmob");
	AdsAdmob = NULL;
}

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }
	
    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    auto scene = HelloWorldScene::createScene();

    // run
    director->runWithScene(scene);

   //Ads初期化コード
    CCLOG("pAppDelegate->init %d",init());
    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();

}
  • HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "PluginManager.h"
#include "ProtocolAds.h"
#include "ProtocolShare.h"
#include "AppDelegate.h"

USING_NS_CC;
using namespace cocos2d::plugin;



bool HelloWorld::init()
{

//〜略〜



        //表示するためのコード
        cocos2d::plugin::ProtocolAds* AdsAdmob = AppDelegate::getInstance()->getAds();//広告のInstanceを取得
	if(AdsAdmob==NULL){
		CCLOG("AdsAdmob is NULL");
	}
        else{
	    TAdsInfo info;
    	    info["AdmobType"] = "1";//AdsAdmob.ADMOB_TYPE_BANNER;
	    info["AdmobSizeEnum"] = "1";//AdmobSizeEnum
	    AdsAdmob->showAds(info, ProtocolAds::kPosTop);
        }
}