Cocox2d-x 2.2.5 中 程序启动流程 及 Size 的设置

前端之家收集整理的这篇文章主要介绍了Cocox2d-x 2.2.5 中 程序启动流程 及 Size 的设置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1.  
首先从main.m开始,
  1. int main(int argc,char *argv[]) {
  2. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  3. int retVal = UIApplicationMain(argc,argv,nil,<strong>@"AppController"</strong>);
  4. [pool release];
  5. return retVal;
  6. }
程序进入AppController,AppController继承自NSObject,并遵循UIApplicationDelegate协议
  1. @interface AppController :<strong> NSObject</strong> <UIApplicationDelegate> {
  2. UIWindow *window;
  3. RootViewController *viewController;
  4. }

AppController中开头创建了一个AppDelegate对象,
  1. //创建一个AppDelegate对象
  2. <strong>static AppDelegate s_sharedApplication;</strong>

AppDelegate对象的定义如下,AppDelegate继承自CCApplication:
  1. class AppDelegate : private cocos2d::CCApplication
  2. {
  3. public:
  4. AppDelegate();
  5. virtual ~AppDelegate();
  6. virtual bool applicationDidFinishLaunching();
  7. virtual void applicationDidEnterBackground();
  8. virtual void applicationWillEnterForeground();
  9. };
  10. AppDelegate::AppDelegate() {
  11. }
  12. AppDelegate::~AppDelegate()
  13. {
  14. }
  15. /////////////////////////////////////////////////////
  16. class CC_DLL CCApplication : public CCApplicationProtocol
  17. {
  18. public:
  19. CCApplication();
  20. virtual ~CCApplication();
  21. static CCApplication* sharedApplication();
  22. protected:
  23. static CCApplication * sm_pSharedApplication;
  24. };
  25. CCApplication* CCApplication::sm_pSharedApplication = 0;
  26. CCApplication::CCApplication()
  27. {
  28. CC_ASSERT(! sm_pSharedApplication);
  29. sm_pSharedApplication = this;
  30. }

下面是AppController的实现:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // 窗口
  3. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
  4. // 创建EAGLView EAGLView主要负责创建程序在呈现屏幕图像时将会用到的OpenGL语境。
  5. <strong>EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
  6. pixelFormat: kEAGLColorFormatRGB565
  7. depthFormat: GL_DEPTH24_STENCIL8_OES
  8. preserveBackbuffer: NO
  9. sharegroup: nil
  10. multiSampling: NO
  11. numberOfSamples: 0];</strong>
  12.  
  13. // Use RootViewController manage EAGLView
  14. viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
  15. viewController.wantsFullScreenLayout = YES;
  16. viewController.view = __glView;
  17.  
  18. if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
  19. {
  20. [window addSubview: viewController.view];
  21. }
  22. else
  23. {
  24. [window setRootViewController:viewController];
  25. }
  26. [window makeKeyAndVisible];
  27. [[UIApplication sharedApplication] setStatusBarHidden:true];
  28. <strong> cocos2d::CCApplication::sharedApplication()->run();</strong>
  29.  
  30. return YES;
  31. }
  32. - (void)applicationWillResignActive:(UIApplication *)application {
  33. cocos2d::CCDirector::sharedDirector()->pause();
  34. }
  35. - (void)applicationDidBecomeActive:(UIApplication *)application {
  36. cocos2d::CCDirector::sharedDirector()->resume();
  37. }
  38. - (void)applicationDidEnterBackground:(UIApplication *)application {
  39. cocos2d::CCApplication::sharedApplication()->applicationDidEnterBackground();
  40. }
  41. - (void)applicationWillEnterForeground:(UIApplication *)application {
  42. cocos2d::CCApplication::sharedApplication()->applicationWillEnterForeground();
  43. }

程序一开始就创建了一个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()函数的定义:
  1. int CCApplication::run()
  2. {
  3. if (<strong>applicationDidFinishLaunching()</strong>)
  4. {
  5. <strong> [[CCDirectorCaller sharedDirectorCaller] startMainLoop];</strong>
  6. }
  7. return 0;
  8. }

先执行applicaitonDidFinishLaunching()函数,其定义如下:
  1. bool AppDelegate::applicationDidFinishLaunching()
  2. {
  3. CCConfiguration::sharedConfiguration()->loadConfigFile("configs/config-example.plist");
  4.  
  5. // initialize director
  6. CCDirector *pDirector = CCDirector::sharedDirector();
  7. <strong> pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());</strong>
  8. pDirector->setDisplayStats(true);
  9. <strong> CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
  10.  
  11. CCSize designSize = CCSizeMake(480,320);</strong>
  12. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  13. CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width,designSize.height,kResolutionShowAll);
  14. #else
  15. <strong> CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width,kResolutionNoBorder);</strong>
  16. #endif
  17.  
  18. CCScene * pScene = CCScene::create();
  19. CCLayer * pLayer = new TestController();
  20. pLayer->autorelease();
  21. pScene->addChild(pLayer);
  22. pDirector->runWithScene(pScene);
  23. return true;
  24. }

在applicationDidFinishLaunching()里先创建了一个CCDirector对象,然后设置其OpenGLView为单例CCEGLView::sharedOpenGLView(),
  1. CCEGLView* CCEGLView::sharedOpenGLView()
  2. {
  3. static CCEGLView instance;
  4. return &instance;
  5. }

  1. CCEGLView::CCEGLView()
  2. {
  3. //这里使用参数对两个变量进行了赋值,第一个就是屏幕的大小。第二个就是分辨率大小。
  4. m_obScreenSize.width = m_obDesignResolutionSize.width = [[EAGLView sharedEGLView] getWidth];
  5. m_obScreenSize.height = m_obDesignResolutionSize.height = [[EAGLView sharedEGLView] getHeight];
  6. }

在创建CCEGLView的对象时会用 EGALView 的宽高去初始化其屏幕尺寸m_obScreeenSize和分辨率大小m_obDesignResolutionSize。我们回到Application的run(),在applicationDidFinishLaunching执行完返回true,程序会执行[[CCDirectorCaller sharedDirectorCaller] startMainLoop];
  1. -(void) startMainLoop
  2. {
  3. // CCDirector::setAnimationInterval() is called,we should invalidate it first
  4. [displayLink invalidate];
  5. displayLink = nil;
  6. displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
  7. [displayLink setFrameInterval: self.interval];
  8. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  9. }
  10. -(void) doCaller: (id) sender
  11. {
  12. [EAGLContext setCurrentContext: [[EAGLView sharedEGLView] context]];
  13. cocos2d::CCDirector::sharedDirector()->mainLoop();
  14. }
displayLink是一个CADisplayLink对象,是一个定时器,这里会不断调用doCaller,doCaller里会不断调用CCDirector的mainLoop().

猜你在找的Cocos2d-x相关文章