版本:cocos2d-x 3.0 Beta
分析的是android部分的源码
在cocos2d-x自动生成的android部分,下jni目录,main.cpp是java调用的部分,代码如下
void cocos_android_app_init (struct android_app* app) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new
AppDelegate();
}
在这个版本中,android部分使用了nativeActivity
在cocos\2d\platform\android目录下,有nativeactivity.cpp文件,进行了初使化
static void cocos_init(cocos_dimensions d, struct android_app* app) { LOGI("cocos_init(...)"); pthread_t thisthread = pthread_self(); LOGI("pthread_self() = %X", thisthread); cocos2d::FileUtilsAndroid::setassetmanager(app->activity->assetManager); if (!cocos2d::Director::getInstance()->getOpenGLView()) { cocos2d::EGLView *view = cocos2d::EGLView::getInstance(); view->setFrameSize(d.w, d.h); cocos_android_app_init(app); 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); cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent); cocos2d::Director::getInstance()->setGLDefaultValues(); } }
cocos2d::Application::getInstance()->run();
就调用了开发者的代码,游戏就运行起来了,在engine_draw_frame中,调用了cocos2d::Director::getInstance()->mainLoop();
static void engine_draw_frame(struct engine* engine) { LOG_RENDER_DEBUG("engine_draw_frame(...)"); pthread_t thisthread = pthread_self(); LOG_RENDER_DEBUG("pthread_self() = %X", thisthread); if (engine->display == NULL) { // No display. LOGW("engine_draw_frame : No display."); return; } dispatch_pending_runnables(); cocos2d::Director::getInstance()->mainLoop(); LOG_RENDER_DEBUG("engine_draw_frame : just called cocos‘ mainLoop()"); /* // Just fill the screen with a color. */ /* glClearColor(((float)engine->state.x)/engine->width, engine->state.angle, */ /* ((float)engine->state.y)/engine->height, 1); */ /* glClear(GL_COLOR_BUFFER_BIT); */ if (s_pfEditTextCallback && editboxText) { s_pfEditTextCallback(editboxText, s_ctx); free(editboxText); editboxText = NULL; } eglSwapBuffers(engine->display, engine->surface); }