cocos2d-x源码分析-----入口分析(android)

本文详细分析了cocos2d-x3.0Beta版本中Android部分的源码,包括nativeActivity的初始化过程、应用启动流程以及如何在engine_draw_frame中调用开发者代码实现游戏运行。重点阐述了从cocos_android_app_init到Director::getInstance()->run()的完整游戏启动流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本: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);
}

转载于:https://www.cnblogs.com/lihaibo19891007/p/3558771.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值