axmol/HelloWorld/HelloWorld.cpp

113 lines
2.8 KiB
C++
Raw Normal View History

// Application application cpp file.
// Original file name: HelloWorldApp.cpp
// Generated by TOPS Builder:Project wizard,Date:2010-8-3
2010-10-13 17:37:55 +08:00
#include "HelloWorld.h"
2010-08-05 18:58:38 +08:00
using namespace cocos2d;
2010-10-13 17:37:55 +08:00
#define IMG_PATH "/NEWPLUS/TDA_DATA/UserData/HelloWorld.png"
2010-08-05 18:58:38 +08:00
2010-10-13 12:07:47 +08:00
// Layer
2010-09-11 12:02:43 +08:00
class MyLayer : public CCLayer
{
public:
2010-10-13 12:07:47 +08:00
bool init()
{
if( !CCLayer::init())
{
return false;
}
this->setIsTouchEnabled(true);
return true;
};
2010-09-11 12:02:43 +08:00
2010-10-13 12:07:47 +08:00
void ccTouchesEnded(NSSet* pTouches, UIEvent* pEvent)
2010-09-11 12:02:43 +08:00
{
CCDirector::getSharedDirector()->end();
2010-10-13 12:07:47 +08:00
};
2010-09-11 12:02:43 +08:00
2010-10-13 12:07:47 +08:00
LAYER_NODE_FUNC(MyLayer);
2010-09-11 12:02:43 +08:00
};
2010-10-13 12:07:47 +08:00
// AppDelegate
2010-10-13 17:37:55 +08:00
HelloWorldAppDelegate::HelloWorldAppDelegate()
2010-08-05 18:58:38 +08:00
: m_rcWnd(0, 0, GetScreenWidth(), GetScreenHeight())
, m_pMainWnd(NULL)
{
}
2010-10-13 17:37:55 +08:00
bool HelloWorldAppDelegate::initCocos2d()
{
2010-08-05 18:58:38 +08:00
// init director
CCDirector::getSharedDirector()->setOpenGLView(m_pMainWnd);
2010-10-13 12:07:47 +08:00
// set to landscape mode
CCDirector::getSharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
2010-10-29 14:48:00 +08:00
2010-10-13 12:07:47 +08:00
// load background image texture and get window size
CCTexture2D * pTexture = CCTextureCache::sharedTextureCache()->addImage(IMG_PATH);
CGSize size = CCDirector::getSharedDirector()->getWinSize();
2010-10-13 12:07:47 +08:00
// create sprite instance
CCSprite * pSprite = CCSprite::spriteWithTexture(pTexture);
pSprite->setPosition(CGPoint(size.width / 2, size.height / 2));
2010-10-29 14:48:00 +08:00
// create layer instance
2010-10-13 12:07:47 +08:00
CCLayer * pLayer = MyLayer::node();
pLayer->addChild(pSprite);
2010-08-05 18:58:38 +08:00
// add layer to scene
CCScene * pScene = CCScene::node();
pScene->addChild(pLayer);
2010-08-05 18:58:38 +08:00
// add scene to director
CCDirector::getSharedDirector()->runWithScene(pScene);
2010-09-11 12:02:43 +08:00
return true;
}
2010-10-13 17:37:55 +08:00
Boolean HelloWorldAppDelegate::EventHandler(EventType* pEvent)
{
Boolean bHandled = FALSE;
switch(pEvent->eType)
{
case EVENT_AppLoad:
2010-08-05 18:58:38 +08:00
if (! (m_pMainWnd = new CCXEGLView(this)) || ! m_pMainWnd->Create(&m_rcWnd))
{
2010-08-06 09:55:37 +08:00
// create window failed, quit application
SendStopEvent();
2010-08-05 18:58:38 +08:00
bHandled = TRUE;
break;
}
2010-08-05 18:58:38 +08:00
SetActiveWindow(m_pMainWnd);
// do not return bHandle equal TRUE, CCXApplication::EventHandler need do some thing.
break;
}
2010-08-05 18:58:38 +08:00
return (bHandled) ? TRUE : CCXApplication::EventHandler(pEvent);
}
2010-10-29 14:48:00 +08:00
// 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();
}
2010-08-05 18:58:38 +08:00
// application main entry
Int32 TG3AppMain(const TUChar * pAppID, UInt32 nCmd, void * pCmdParam)
{
2010-10-13 17:37:55 +08:00
HelloWorldAppDelegate app;
2010-08-06 09:48:18 +08:00
return app.Run();
}