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

前端之家收集整理的这篇文章主要介绍了cocos2d-x_Android应用启动调用过程简析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

调用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

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




来自:http://blog.csdn.net/zimo2013/article/details/41116837

猜你在找的Cocos2d-x相关文章