2013-04-26 17:18:17 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012-2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2013-04-19 16:19:04 +08:00
|
|
|
#include "HelloWorldScene.h"
|
2013-04-22 11:17:31 +08:00
|
|
|
#include "PluginManager.h"
|
2013-04-19 16:19:04 +08:00
|
|
|
|
|
|
|
USING_NS_CC;
|
2013-04-22 11:17:31 +08:00
|
|
|
using namespace cocos2d::plugin;
|
|
|
|
|
|
|
|
const std::string s_aTestCases[] = {
|
2013-04-26 17:16:06 +08:00
|
|
|
"Admob",
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string s_aTestTypes[] = {
|
|
|
|
"Banner",
|
|
|
|
"Full Screen",
|
2013-04-22 11:17:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const std::string s_aTestPoses[] = {
|
2013-04-26 17:16:06 +08:00
|
|
|
"Pos: Center",
|
2013-04-22 11:17:31 +08:00
|
|
|
"Pos: Top",
|
|
|
|
"Pos: TopLeft",
|
|
|
|
"Pos: TopRight",
|
|
|
|
"Pos: Bottom",
|
|
|
|
"Pos: BottomLeft",
|
|
|
|
"Pos: BottomRight",
|
|
|
|
};
|
2013-04-19 16:19:04 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-27 14:40:08 +08:00
|
|
|
m_pAdmob = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdsAdmob"));
|
2013-04-26 17:16:06 +08:00
|
|
|
TAdsDeveloperInfo devInfo;
|
|
|
|
devInfo["AdmobID"] = "a1516fb6b16b12f";
|
|
|
|
m_pAdmob->configDeveloperInfo(devInfo);
|
|
|
|
m_pListener = new MyAdsListener();
|
|
|
|
m_pAdmob->setAdsListener(m_pListener);
|
2013-04-22 11:17:31 +08:00
|
|
|
m_pAdmob->setDebugMode(true);
|
|
|
|
|
2013-04-19 16:19:04 +08:00
|
|
|
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
|
|
|
|
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
|
2013-04-22 11:17:31 +08:00
|
|
|
CCPoint posMid = ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
|
2013-04-19 16:19:04 +08:00
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
// 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);
|
2013-04-22 11:17:31 +08:00
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
CCLabelTTF* label1 = CCLabelTTF::create("ShowAds", "Arial", 24);
|
2013-04-22 11:17:31 +08:00
|
|
|
CCMenuItemLabel* pItemShow = CCMenuItemLabel::create(label1, this, menu_selector(HelloWorld::testShow));
|
|
|
|
pItemShow->setAnchorPoint(ccp(0.5f, 0));
|
|
|
|
pMenu->addChild(pItemShow, 0);
|
2013-04-26 17:16:06 +08:00
|
|
|
pItemShow->setPosition(ccpAdd(posMid, ccp(-100, -120)));
|
2013-04-22 11:17:31 +08:00
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
CCLabelTTF* label2 = CCLabelTTF::create("HideAds", "Arial", 24);
|
2013-04-22 11:17:31 +08:00
|
|
|
CCMenuItemLabel* pItemHide = CCMenuItemLabel::create(label2, this, menu_selector(HelloWorld::testHide));
|
|
|
|
pItemHide->setAnchorPoint(ccp(0.5f, 0));
|
|
|
|
pMenu->addChild(pItemHide, 0);
|
2013-04-26 17:16:06 +08:00
|
|
|
pItemHide->setPosition(ccpAdd(posMid, ccp(100, -120)));
|
2013-04-22 11:17:31 +08:00
|
|
|
|
|
|
|
// create optional menu
|
2013-04-26 17:16:06 +08:00
|
|
|
// cases item
|
2013-04-22 11:17:31 +08:00
|
|
|
m_pCaseItem = CCMenuItemToggle::createWithTarget(this,
|
|
|
|
menu_selector(HelloWorld::caseChanged),
|
|
|
|
CCMenuItemFont::create( s_aTestCases[0].c_str() ),
|
|
|
|
NULL );
|
|
|
|
int caseLen = sizeof(s_aTestCases) / sizeof(std::string);
|
|
|
|
for (int i = 1; i < caseLen; ++i)
|
|
|
|
{
|
|
|
|
m_pCaseItem->getSubItems()->addObject( CCMenuItemFont::create( s_aTestCases[i].c_str() ) );
|
|
|
|
}
|
2013-04-26 17:16:06 +08:00
|
|
|
m_pCaseItem->setPosition(ccpAdd(posMid, ccp(-200, 120)));
|
2013-04-22 11:17:31 +08:00
|
|
|
pMenu->addChild(m_pCaseItem);
|
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
// type item
|
|
|
|
m_pTypeItem = CCMenuItemToggle::createWithTarget(this,
|
|
|
|
menu_selector(HelloWorld::typeChanged),
|
|
|
|
CCMenuItemFont::create( s_aTestTypes[0].c_str() ),
|
|
|
|
NULL );
|
|
|
|
int typeLen = sizeof(s_aTestTypes) / sizeof(std::string);
|
|
|
|
for (int i = 1; i < typeLen; ++i)
|
|
|
|
{
|
|
|
|
m_pTypeItem->getSubItems()->addObject( CCMenuItemFont::create( s_aTestTypes[i].c_str() ) );
|
|
|
|
}
|
|
|
|
m_pTypeItem->setPosition(ccpAdd(posMid, ccp(0, 120)));
|
|
|
|
pMenu->addChild(m_pTypeItem);
|
|
|
|
|
|
|
|
// poses item
|
2013-04-22 11:17:31 +08:00
|
|
|
m_pPosItem = CCMenuItemToggle::createWithTarget(this,
|
|
|
|
menu_selector(HelloWorld::posChanged),
|
|
|
|
CCMenuItemFont::create( s_aTestPoses[0].c_str() ),
|
|
|
|
NULL );
|
|
|
|
int posLen = sizeof(s_aTestPoses) / sizeof(std::string);
|
|
|
|
for (int i = 1; i < posLen; ++i)
|
|
|
|
{
|
|
|
|
m_pPosItem->getSubItems()->addObject( CCMenuItemFont::create( s_aTestPoses[i].c_str() ) );
|
|
|
|
}
|
2013-04-26 17:16:06 +08:00
|
|
|
m_pPosItem->setPosition(ccpAdd(posMid, ccp(200, 120)));
|
2013-04-22 11:17:31 +08:00
|
|
|
pMenu->addChild(m_pPosItem);
|
|
|
|
|
|
|
|
// init options
|
|
|
|
m_pAds = m_pAdmob;
|
2013-04-26 17:16:06 +08:00
|
|
|
m_ePos = ProtocolAds::kPosCenter;
|
|
|
|
m_eType = ProtocolAds::kBannerAd;
|
2013-04-22 11:17:31 +08:00
|
|
|
|
2013-04-19 16:19:04 +08:00
|
|
|
this->addChild(pMenu, 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-22 11:17:31 +08:00
|
|
|
void HelloWorld::testShow(CCObject* pSender)
|
|
|
|
{
|
2013-04-24 16:53:02 +08:00
|
|
|
int nSize = 0;
|
2013-04-22 11:17:31 +08:00
|
|
|
if (m_pAds == m_pAdmob)
|
|
|
|
{
|
2013-05-27 14:40:08 +08:00
|
|
|
nSize = 0;
|
2013-04-24 16:53:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pAds)
|
|
|
|
{
|
2013-04-26 17:16:06 +08:00
|
|
|
m_pAds->showAds(m_eType, nSize, m_ePos);
|
2013-04-22 11:17:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HelloWorld::testHide(CCObject* pSender)
|
|
|
|
{
|
2013-04-26 17:16:06 +08:00
|
|
|
m_pAds->hideAds(m_eType);
|
2013-04-22 11:17:31 +08:00
|
|
|
}
|
2013-04-19 16:19:04 +08:00
|
|
|
|
|
|
|
void HelloWorld::menuCloseCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCDirector::sharedDirector()->end();
|
|
|
|
|
2013-04-22 11:17:31 +08:00
|
|
|
if (m_pAdmob != NULL)
|
|
|
|
{
|
|
|
|
PluginManager::getInstance()->unloadPlugin("AdsAdmob");
|
|
|
|
m_pAdmob = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL != m_pListener)
|
|
|
|
{
|
|
|
|
delete m_pListener;
|
|
|
|
m_pListener = NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
PluginManager::end();
|
2013-04-19 16:19:04 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
exit(0);
|
|
|
|
#endif
|
|
|
|
}
|
2013-04-22 11:17:31 +08:00
|
|
|
|
|
|
|
void HelloWorld::caseChanged(CCObject* pSender)
|
|
|
|
{
|
|
|
|
std::string strLog = "";
|
|
|
|
switch (m_pCaseItem->getSelectedIndex())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
m_pAds = m_pAdmob;
|
|
|
|
strLog = "Admob";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CCLog("case selected change to : %s", strLog.c_str());
|
|
|
|
}
|
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
void HelloWorld::typeChanged(CCObject* pSender)
|
2013-04-22 11:17:31 +08:00
|
|
|
{
|
2013-04-26 17:16:06 +08:00
|
|
|
int selectIndex = m_pTypeItem->getSelectedIndex();
|
|
|
|
m_eType = (ProtocolAds::AdsType) selectIndex;
|
|
|
|
CCLog("type selected change to : %d", m_eType);
|
2013-04-22 11:17:31 +08:00
|
|
|
}
|
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
void HelloWorld::posChanged(CCObject* pSender)
|
2013-04-22 11:17:31 +08:00
|
|
|
{
|
2013-04-26 17:16:06 +08:00
|
|
|
int selectIndex = m_pPosItem->getSelectedIndex();
|
|
|
|
m_ePos = (ProtocolAds::AdsPos) selectIndex;
|
|
|
|
CCLog("pos selected change to : %d", m_ePos);
|
2013-04-22 11:17:31 +08:00
|
|
|
}
|
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
void MyAdsListener::onAdsResult(AdsResultCode code, const char* msg)
|
2013-04-22 11:17:31 +08:00
|
|
|
{
|
2013-04-26 17:16:06 +08:00
|
|
|
CCLog("OnAdsResult, code : %d, msg : %s", code, msg);
|
2013-04-22 11:17:31 +08:00
|
|
|
}
|
|
|
|
|
2013-04-27 11:32:12 +08:00
|
|
|
void MyAdsListener::onPlayerGetPoints(cocos2d::plugin::ProtocolAds* pAdsPlugin, int points)
|
2013-04-22 11:17:31 +08:00
|
|
|
{
|
2013-04-26 17:16:06 +08:00
|
|
|
CCLog("Player get points : %d", points);
|
2013-04-22 11:17:31 +08:00
|
|
|
|
2013-04-26 17:16:06 +08:00
|
|
|
// @warning should add code to give game-money to player here
|
|
|
|
|
|
|
|
// spend the points of player
|
|
|
|
if (pAdsPlugin != NULL) {
|
|
|
|
pAdsPlugin->spendPoints(points);
|
|
|
|
}
|
2013-04-22 11:17:31 +08:00
|
|
|
}
|