cocos2dx + ios + hello cpp + 启动过程

第一步:hello cpp 启动终极入口函数mian(不同平台,都有该平台单独的main):cocos2d-x-3.4\tests\cpp-empty-test\proj.ios\main.m(注意文件的路径)

int main(int argc,char *argv[]) {
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc,argv,nil,@"AppController");
    [pool release];
    return retVal;
}

UIApplicationMain:该函数如果不理解,直接上ios网站上搜索,现在截取如下:

intUIApplicationMain(argc,char*argv[],249)">NSStringprincipalClassNamedelegateClassName);

principalClassName

The name of theUIApplicationclass or subclass. If you specifynil,UIApplicationis assumed.

delegateClassName The name of the class from which the application delegate is instantiated. IfprincipalClassNamedesignates a subclass ofUIApplication,you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages. Specifynilif you load the delegate object from your application’s main nib file.

第三个参数传入的是nil,andUIApplicationit is。

第四个参数传入的是@"AppController",这个是个类名,这个类的实例肯定就是作为delegate赋值给了UIApplication的delegate属性了。

为了验证一下,我们可以这样子,如果AppController的实例真的赋给了UIApplication的delegate属性,那么AppController一定实现了UIApplicationDelegate协议,我们打开看一下(cocos2d-x-3.4\tests\cpp-empty-test\proj.ios\AppController.h)(注意文件的路径),果真如此:

@class RootViewController;

@interface AppController : NSObject <UIAccelerometerDelegate,UIAlertViewDelegate,UITextFieldDelegate,UIApplicationDelegate> {
    UIWindow *window;
    RootViewController    *viewController;
}

@end

其实一般的ios应用程序,按照惯例,UIApplication delegate的命名是这样子的,看下图:


其实这在coco2dx ios hello cpp中也有一个AppDelegate类的,请看文件(cocos2d-x-3.4\tests\cpp-empty-test\Classes\AppDelegate.h)(注意文件的路径,这个文件已经从平台相关的目录跳出来了,这个类的实现可能已经平台无关了,并且也已经变成c++了,而不是objective-c):

/**
@brief    The cocos2d Application.

The reason for implement as private inheritance is to hide some interface call by Director.
*/
class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();
    virtual ~AppDelegate();

    virtual void initGLContextAttrs();
    
    /**
    @brief    Implement Director and Scene init code here.
    @return true    Initialize success,app continue.
    @return false   Initialize failed,app terminate.
    */
    virtual bool applicationDidFinishLaunching();

    /**
    @brief  The function be called when the application enter background
    @param  the pointer of the application
    */
    virtual void applicationDidEnterBackground();

    /**
    @brief  The function be called when the application enter foreground
    @param  the pointer of the application
    */
    virtual void applicationWillEnterForeground();
};
其实有一点你可能已经想到了,为了某些原因,AppController类一定把几个必要的事件响应函数(delegate嘛)转掉到AppDelegate类了。具体是什么原因,可以这样考虑下:

1AppController应该不仅仅是UIApplication的delegate,还应该是完成了一些其他的功能(并且这些功能可能还是平台相关的)。

2AppDelegate可能已经和平台无关。

这篇暂时先分析到这儿,( ^_^ )。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...