码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-03-11 00:31:43      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

对比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 中 

 

 




 

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

标签:

原文地址:http://www.cnblogs.com/d20062303732/p/4328734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!