cocos2d-x_Android应用启动调用过程简析

调用org.cocos2dx.cpp.AppActivity

AppActivity是位于proj.android/src下,是开发者类(即开发者自定义的类),继承自org.cocos2dx.lib.Cocos2dxActivity,它没有添加任何代码,纯粹是一个Cocos2dxActivity,也是一个Activity。

AppActivity被调用是因为被配置在AndroidManifest.xml


        

            
            

            
                
                
            
        
    

加载libcocos2dcpp.so

libcocos2dcpp.so在编译后生成到proj.android/libs/armeabi下,从上面代码中可以看到android:value=”cocos2dcpp”这行配置内容,它指示了so的名字。

在AppActivity中加载so的代码如下:

@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            Bundle bundle = ai.metaData;
            try {
                String libName = bundle.getString("android.app.lib_name");
                System.loadLibrary(libName);
            } catch (Exception e) {
                 // ERROR
            }
        } catch (PackageManager.NameNotFoundException e) {
             // ERROR
        }

        sContext = this;
        this.mHandler = new Cocos2dxHandler(this);

        this.init();

        Cocos2dxHelper.init(this);
    }

调用cocos_android_app_init

proj.android/jni/hellocpp/main.cpp中的cocos_android_app_init函数被调用。

我们看看this.init();后续代码片段:

public void init() {
        ...
        this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
        ...
    }
@Override
    public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig) {
        Cocos2dxRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight);
        this.mLastTickInNanoSeconds = System.nanoTime();
    }

到现在前面都是java代码,上段中的Cocos2dxRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight);是一句jni调用,java调用c/c++代码。

其代码在cocos2d/cocos/2d/platform/android/javaactivity.cpp中,如下:

void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv*  env, jobject thiz, jint w, jint h)
{
    auto director = cocos2d::Director::getInstance();
    auto glview = director->getOpenGLView();
    if (!glview)
    {
        glview = cocos2d::GLView::create("Android app");
        glview->setFrameSize(w, h);
        director->setOpenGLView(glview);

        //cocos_android_app_init(env, thiz);

        cocos2d::Application::getInstance()->run();
    }
    else
    {
        cocos2d::GL::invalidateStateCache();
        cocos2d::ShaderCache::getInstance()->reloadDefaultShaders();
        cocos2d::DrawPrimitives::init();
        cocos2d::VolatileTextureMgr::reloadAllTextures();

        cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
        director->getEventDispatcher()->dispatchEvent(&foregroundEvent);
        director->setGLDefaultValues();
    }

}
 
  

jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNIEnv*  env, jobject thiz)

{

    cocos_android_app_init(env, thiz);

    cocos2d::Application::getInstance()->initGLContextAttrs(); 

    GLContextAttrs _glContextAttrs = GLView::getGLContextAttrs();

    

    int tmp[6] = {_glContextAttrs.redBits, _glContextAttrs.greenBits, _glContextAttrs.blueBits,

                           _glContextAttrs.alphaBits, _glContextAttrs.depthBits, _glContextAttrs.stencilBits};

    jintArray glContextAttrsJava = env->NewIntArray(6);

        env->SetIntArrayRegion(glContextAttrsJava, 0, 6, tmp); 

    

    return glContextAttrsJava;

}

我们看到了cocos_android_app_init(env, thiz);这句,这就和main.cpp中的cocos_android_app_init连接上了。

创建AppDelegate

cocos_android_app_init中创建了AppDelegate,后面的事就交给cocos2d-x引擎吧

void cocos_android_app_init (JNIEnv* env, jobject thiz) {
    LOGD("cocos_android_app_init");
    AppDelegate *pAppDelegate = new AppDelegate();
}

你可能感兴趣的:(cocos2d-x,cocos2d-x,Android,cocos2dx,c++,c)