int main(int argc,char *argv[]) { NSautoreleasePool * pool = [[NSautoreleasePool alloc] init]; int retVal = UIApplicationMain(argc,argv,nil,<strong>@"AppController"</strong>); [pool release]; return retVal;}程序进入AppController,AppController继承自NSObject,并遵循UIApplicationDelegate协议
@interface AppController :<strong> NSObject</strong> <UIApplicationDelegate> { UIWindow *window; RootVIEwController *vIEwController;}
AppController中开头创建了一个AppDelegate对象,
//创建一个AppDelegate对象<strong>static AppDelegate s_sharedApplication;</strong>
AppDelegate对象的定义如下,AppDelegate继承自CCApplication:
class AppDelegate : private cocos2d::CCApplication{public: AppDelegate(); virtual ~AppDelegate(); virtual bool applicationDIDFinishLaunching(); virtual voID applicationDIDEnterBackground(); virtual voID applicationWillEnterForeground();};AppDelegate::AppDelegate() {}AppDelegate::~AppDelegate() {}/////////////////////////////////////////////////////class CC_DLL CCApplication : public CCApplicationProtocol{public: CCApplication(); virtual ~CCApplication(); static CCApplication* sharedApplication();protected: static CCApplication * sm_pSharedApplication;};CCApplication* CCApplication::sm_pSharedApplication = 0;CCApplication::CCApplication(){ CC_ASSERT(! sm_pSharedApplication); sm_pSharedApplication = this;}
下面是AppController的实现:
- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 窗口 window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; // 创建EAGLVIEw EAGLVIEw主要负责创建程序在呈现屏幕图像时将会用到的OpenGL语境。 <strong>EAGLVIEw *__glVIEw = [EAGLVIEw vIEwWithFrame: [window bounds] pixelFormat: kEAGLcolorFormatRGB565 depthFormat: GL_DEPTH24_STENCIL8_OES preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples: 0];</strong> // Use RootVIEwController manage EAGLVIEw vIEwController = [[RootVIEwController alloc] initWithNibname:nil bundle:nil]; vIEwController.wantsFullScreenLayout = YES; vIEwController.vIEw = __glVIEw; if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { [window addSubvIEw: vIEwController.vIEw]; } else { [window setRootVIEwController:vIEwController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusbarHIDden:true]; <strong> cocos2d::CCApplication::sharedApplication()->run();</strong> return YES;}- (voID)applicationWillResignActive:(UIApplication *)application { cocos2d::CCDirector::sharedDirector()->pause();}- (voID)applicationDIDBecomeActive:(UIApplication *)application { cocos2d::CCDirector::sharedDirector()->resume();}- (voID)applicationDIDEnterBackground:(UIApplication *)application { cocos2d::CCApplication::sharedApplication()->applicationDIDEnterBackground();}- (voID)applicationWillEnterForeground:(UIApplication *)application { cocos2d::CCApplication::sharedApplication()->applicationWillEnterForeground();}
程序一开始就创建了一个EAGLVIEw的对象__glVIEw,__glVIEw的frame被设置成[window bounds],EAGLVIEw中定义了一个静态对象 static EAGLVIEw *vIEw=0,可以通过单例方法sharedEGLVIEw获取,获取__glVIEw可以通过调用[EgalVIEw sharedVIEw]得到。__glVIEw创建完之后将其用作RootVIEwController的VIEw,再添加到程序的window上。接下来通过语句:cocos2d::CCApplication::sharedApplicaiton()->run()让游戏跑起来;先来看看CCApplicaiton的run()函数的定义:
int CCApplication::run(){ if (<strong>applicationDIDFinishLaunching()</strong>) { <strong> [[CCDirectorCaller sharedDirectorCaller] startMainLoop];</strong> } return 0;}
先执行applicaitonDIDFinishLaunching()函数,其定义如下:
bool AppDelegate::applicationDIDFinishLaunching(){ CCConfiguration::sharedConfiguration()->loadConfigfile("configs/config-example.pList"); // initialize director CCDirector *pDirector = CCDirector::sharedDirector(); <strong> pDirector->setopenGLVIEw(CCEGLVIEw::sharedOpenGLVIEw());</strong> pDirector->setdisplayStats(true); <strong> CCSize screenSize = CCEGLVIEw::sharedOpenGLVIEw()->getFrameSize(); CCSize designSize = CCSizeMake(480,320);</strong>#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCEGLVIEw::sharedOpenGLVIEw()->setDesignResolutionSize(designSize.wIDth,designSize.height,kResolutionShowAll);#else <strong> CCEGLVIEw::sharedOpenGLVIEw()->setDesignResolutionSize(designSize.wIDth,kResolutionNoborder);</strong>#endif CCScene * pScene = CCScene::create(); cclayer * pLayer = new TestController(); pLayer->autorelease(); pScene->addChild(pLayer); pDirector->runWithScene(pScene); return true;}
在applicationDIDFinishLaunching()里先创建了一个CCDirector对象,然后设置其OpenGLVIEw为单例CCEGLVIEw::sharedOpenGLVIEw(),
CCEGLVIEw* CCEGLVIEw::sharedOpenGLVIEw(){ static CCEGLVIEw instance; return &instance;}
CCEGLVIEw::CCEGLVIEw(){ //这里使用参数对两个变量进行了赋值,第一个就是屏幕的大小。第二个就是分辨率大小。 m_obScreenSize.wIDth = m_obDesignResolutionSize.wIDth = [[EAGLVIEw sharedEGLVIEw] getWIDth]; m_obScreenSize.height = m_obDesignResolutionSize.height = [[EAGLVIEw sharedEGLVIEw] getHeight];}
在创建CCEGLVIEw的对象时会用 EgalVIEw 的宽高去初始化其屏幕尺寸m_obScreeenSize和分辨率大小m_obDesignResolutionSize。我们回到Application的run(),在applicationDIDFinishLaunching执行完返回true,程序会执行[[CCDirectorCaller sharedDirectorCaller] startMainLoop];
-(voID) startMainLoop{ // CCDirector::setAnimationInterval() is called,we should invalIDate it first [displaylink invalIDate]; displaylink = nil; displaylink = [NSClassFromString(@"CAdisplaylink") displaylinkWithTarget:self selector:@selector(doCaller:)]; [displaylink setFrameInterval: self.interval]; [displaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];}-(voID) doCaller: (ID) sender{ [EAGLContext setCurrentContext: [[EAGLVIEw sharedEGLVIEw] context]]; cocos2d::CCDirector::sharedDirector()->mainLoop();}displaylink是一个CAdisplaylink对象,是一个定时器,这里会不断调用doCaller,doCaller里会不断调用CCDirector的mainLoop(). 总结
以上是内存溢出为你收集整理的Cocox2d-x 2.2.5 中 程序启动流程 及 Size 的设置全部内容,希望文章能够帮你解决Cocox2d-x 2.2.5 中 程序启动流程 及 Size 的设置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)