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


首先从main.m开始,

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, designSize.height, 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().











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值