axmol/HelloWorld/HelloWorld.cpp

117 lines
2.9 KiB
C++

// Application application cpp file.
// Original file name: HelloWorldApp.cpp
// Generated by TOPS Builder:Project wizard,Date:2010-8-3
#include "HelloWorld.h"
using namespace cocos2d;
#define IMG_PATH "/NEWPLUS/TDA_DATA/UserData/HelloWorld.png"
// Layer
class MyLayer : public CCLayer
{
public:
bool init()
{
if( !CCLayer::init())
{
return false;
}
this->setIsTouchEnabled(true);
return true;
};
void ccTouchesEnded(NSSet* pTouches, UIEvent* pEvent)
{
CCDirector::getSharedDirector()->end();
};
LAYER_NODE_FUNC(MyLayer);
};
// AppDelegate
HelloWorldAppDelegate::HelloWorldAppDelegate()
: m_rcWnd(0, 0, GetScreenWidth(), GetScreenHeight())
, m_pMainWnd(NULL)
{
}
bool HelloWorldAppDelegate::initCocos2d()
{
// init director
CCDirector::getSharedDirector()->setOpenGLView(m_pMainWnd);
// set to landscape mode
CCDirector::getSharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
// load background image texture and get window size
CCTexture2D * pTexture = CCTextureCache::sharedTextureCache()->addImage(IMG_PATH);
CGSize size = CCDirector::getSharedDirector()->getWinSize();
// create sprite instance
CCSprite * pSprite = CCSprite::spriteWithTexture(pTexture);
pSprite->setPosition(CGPoint(size.width / 2, size.height / 2));
// create layer instance
CCLayer * pLayer = MyLayer::node();
pLayer->addChild(pSprite);
// add layer to scene
CCScene * pScene = CCScene::node();
pScene->addChild(pLayer);
// add scene to director
CCDirector::getSharedDirector()->runWithScene(pScene);
return true;
}
Boolean HelloWorldAppDelegate::EventHandler(EventType* pEvent)
{
Boolean bHandled = FALSE;
switch(pEvent->eType)
{
case EVENT_AppLoad:
if (! (m_pMainWnd = new CCXEGLView(this)) || ! m_pMainWnd->Create(&m_rcWnd))
{
// create window failed, clear the heap
delete m_pMainWnd;
CCScheduler::purgeSharedScheduler();
// quit application
SendStopEvent();
bHandled = TRUE;
break;
}
SetActiveWindow(m_pMainWnd);
// do not return bHandle equal TRUE, CCXApplication::EventHandler need do some thing.
break;
}
return (bHandled) ? TRUE : CCXApplication::EventHandler(pEvent);
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void HelloWorldAppDelegate::applicationDidEnterBackground()
{
CCDirector::getSharedDirector()->stopAnimation();
}
// this function will be called when the app is active again
void HelloWorldAppDelegate::applicationWillEnterForeground()
{
CCDirector::getSharedDirector()->startAnimation();
}
// application main entry
Int32 TG3AppMain(const TUChar * pAppID, UInt32 nCmd, void * pCmdParam)
{
HelloWorldAppDelegate app;
return app.Run();
}