第一步: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 the |
delegateClassName | The name of the class from which the application delegate is instantiated. IfprincipalClassName designates a subclass ofUIApplication ,you may designate the subclass as the delegate; the subclass instance receives the application-delegate messages. Specifynil if you load the delegate object from your application’s main nib file.
|
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类了。具体是什么原因,可以这样考虑下:
1AppController应该不仅仅是UIApplication的delegate,还应该是完成了一些其他的功能(并且这些功能可能还是平台相关的)。
2AppDelegate可能已经和平台无关。
这篇暂时先分析到这儿,( ^_^ )。