cocos2d源码分析(一):cocos2d_tests启动流程
2018-12-30 本文已影响0人
奔向火星005
为了研究cocos2d底层代码,挑选了官方ios平台的cocos2d_tests工程。首先对它的demo的整个流程和界面框架理一遍,以便后续查阅。
ios程序启动后,首先调用testsAppDelegate.mm文件中的AppController的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions代码,如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: cocos2d::GLViewImpl::_multisamplingCount > 0 ? YES : NO
numberOfSamples: cocos2d::GLViewImpl::_multisamplingCount ];
#if !defined(CC_TARGET_OS_TVOS)
[eaglView setMultipleTouchEnabled:YES];
#endif
// Use RootViewController manage CCEAGLView
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
#if !defined(CC_TARGET_OS_TVOS)
viewController.wantsFullScreenLayout = YES;
#endif
viewController.view = eaglView;
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:viewController];
}
[window makeKeyAndVisible];
#if !defined(CC_TARGET_OS_TVOS)
[[UIApplication sharedApplication] setStatusBarHidden:true];
#endif
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
app->run();
return YES;
}
大致流程图为:
![](https://img.haomeiwen.com/i5994163/fbefa8099c42a94d.png)
app->run()后面的代码的流程图:
![](https://img.haomeiwen.com/i5994163/1492c20a95e0611f.png)
AppController相关类图为:
![](https://img.haomeiwen.com/i5994163/fb9455253eb095b8.png)
CCEAGLView相关类图为:
![](https://img.haomeiwen.com/i5994163/acb62302cf3df3a1.png)
AppDelegate相关类图为:
![](https://img.haomeiwen.com/i5994163/7dbf0cbd5b347730.png)