axmol/HelloWorld/HelloWorldApp.cpp

103 lines
2.7 KiB
C++

// Application application cpp file.
// Original file name: HelloWorldApp.cpp
// Generated by TOPS Builder:Project wizard,Date:2010-8-3
#include "HelloWorldApp.h"
#include "cocos2d.h"
using namespace cocos2d;
#define IMG_PATH "/NEWPLUS/TDA_DATA/UserData/HelloWorld.png"
//------------------------------------------------------------------
//
// MyLayer
//
//------------------------------------------------------------------
class MyLayer : public CCLayer
{
public:
virtual void onEnter()
{
CCLayer::onEnter();
setIsTouchEnabled(true);
}
virtual bool ccTouchBegan(CCTouch *pTouch, UIEvent *pEvent)
{
CCDirector::getSharedDirector()->end();
return true;
}
virtual void registerWithTouchDispatcher(void)
{
CCTouchDispatcher::getSharedDispatcher()->addTargetedDelegate(this,0,true);
}
};
THelloWorldApp::THelloWorldApp()
: m_rcWnd(0, 0, GetScreenWidth(), GetScreenHeight())
, m_pMainWnd(NULL)
{
}
bool THelloWorldApp::initCocos2d()
{
// init director
CCDirector::getSharedDirector()->setOpenGLView(m_pMainWnd);
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 = new CCSprite();
pSprite->initWithTexture(pTexture);
pSprite->setPosition(CGPoint(size.width / 2, size.height / 2));
// create layer instance
CCLayer * pLayer = new MyLayer();
pLayer->addChild(pSprite);
// add layer to scene
CCScene * pScene = CCScene::node();
pScene->addChild(pLayer);
// add scene to director
CCDirector::getSharedDirector()->runWithScene(pScene);
pSprite->release();
pLayer->release();
return true;
}
Boolean THelloWorldApp::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, 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);
}
// application main entry
Int32 TG3AppMain(const TUChar * pAppID, UInt32 nCmd, void * pCmdParam)
{
THelloWorldApp app;
return app.Run();
}