程序入口的概念是相对的,AppDelegate作为跨平台程序入口,在这之上做了另一层的封装,封装了不同平台的不同实现。

main函数

比如我们通常认为一个程序是由 main 函数开始运行,那我们就去找寻,我们看到了在 proj.linux 目录下存在 main.cpp 文件,这就是我们要看的内容

int main(int argc, char **argv)
{
    // create the application instance
    AppDelegate app;
    CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str());
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setFrameSize(720, 480);
    eglView->setFrameSize(480, 320);
    return CCApplication::sharedApplication()->run();
}

Android里的init

我们看到main就知道其是入口函数,那么没有 main 函数就没有入口了吗?显然不是,以 Android 平台启动cocos2d-x程序为例。我们找到Android平台与上面等价的入口点,proj.android/jni/hellocpp/main.cpp

extern "C"  
{    
    jint JNI_OnLoad(JavaVM *vm, void *reserved)  
    {  
        JniHelper::setJavaVM(vm);        
        return JNI_VERSION_1_4;  
    }        
    void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv*  env, jobject thiz, jint w, jint h)  
    {  
        if (!CCDirector::sharedDirector()->getOpenGLView())  
        {  
            CCEGLView *view = CCEGLView::sharedOpenGLView();  
            view->setFrameSize(w, h);  
      
            AppDelegate *pAppDelegate = new AppDelegate();  
            CCApplication::sharedApplication()->run();  
        }  
        else  
        {  
            ccDrawInit();  
            ccGLInvalidateStateCache();  
      
            CCShaderCache::sharedShaderCache()->reloadDefaultShaders();  
            CCTextureCache::reloadAllTextures();  
            CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);  
            CCDirector::sharedDirector()->setGLDefaultValues();   
        } 
    } 
}  

由Android启动一个应用,通过各种峰回路转,最终执行到了。Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit函数,由此,变开始了我们cocos2d-x、Android平台的程序入口处。



blog comments powered by Disqus

Published

06 January 2015

Tags