cocos2d-x安卓应用启动调用过程简析

调用org.cocos2dx.cpp.AppActivity

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

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

<application android:label="@string/app_name"
                 android:icon="@drawable/icon">
        <activity android:name="org.cocos2dx.cpp.AppActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="landscape"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:configChanges="orientation">

            <!-- Tell NativeActivity the name of our .so -->
            <meta-data android:name="android.app.lib_name"
                       android:value="cocos2dcpp" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

加载libcocos2dcpp.so(或者把这个库放在proj.android/libs下也行)

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();
    }

}

我们看到了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();
}
posted @ 2015-02-11 16:11  全绍辉  阅读(995)  评论(0编辑  收藏  举报