cocos2d-x在iOS的接合简单记录

对比app;

一个app的启动流程是:

 

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, @"AppDelegate");
    }
}

对应

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

中的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // create window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];

    UIViewController *view = [[MainTableViewController alloc]init];
    // root view
    self.window.rootViewController = view;
    
    return YES;
}

可以看到关键是 

window.rootViewController

cocos2d-x中的启动流程基本一致:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    cocos2d::Application *app = cocos2d::Application::getInstance();
    ...
    
    CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
    ...
    // eaglView 继承 UIView
    [eaglView setMultipleTouchEnabled:YES];
    
    viewController.view = eaglView; // 接合的地方
    
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
    // TODO:封装多一层的必要性,需要结合 android 实现理解
    cocos2d::Director::getInstance()->setOpenGLView(glview);
    
    app->run(); // 这里跟进去就进入游戏循环
    return YES;
}

这里出现了3个有点混淆的类:

AppController是iOS的,比较好区分

Application 和 AppDelegate:

Application:个人理解是相当于 mfc中 App 类,也有类似的run接口,对应游戏主循环。

然后 

class  AppDelegate : private cocos2d::Application

然后还有 Director 主要是对应 glview 的操作。

 

run跟进去

-(void) startMainLoop
{
        // Director::setAnimationInterval() is called, we should invalidate it first
    [self stopMainLoop];
    
    displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
    [displayLink setFrameInterval: self.interval];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

理解:

NSRunLoop是 Foundation framework 中 

 

 




 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。