cocos2dx + ios + hello cpp + 启动过程

本文介绍了cocos2d-x在iOS上的集成过程,特别是hello cpp项目的启动入口。主要涉及UIApplicationMain函数及其参数,以及AppController类作为UIApplication的delegate。同时,探讨了AppDelegate类在cocos2d-x项目中的角色,可能是平台无关并负责一些跨平台功能。
摘要由CSDN通过智能技术生成

第一步: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网站上搜索,现在截取如下:

int UIApplicationMain ( int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName );

principalClassName

The name of the UIApplication class or subclass. If you specify nilUIApplication is assumed.

delegateClassName

The name of the class from which the application delegate is instantiated. IfprincipalClassName designates a subclass of UIApplication, you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages. Specify nil if you load the delegate object from your application’s main nib file.

第三个参数传入的是nil,and UIApplication it 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类了。具体是什么原因,可以这样考虑下:

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

AppDelegate可能已经和平台无关。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值