加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 20:01:11 所属栏目:百科 来源:网络整理
导读:首先从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;} 程序进入AppContr
首先从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,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().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读