cocos2d-x android 平台接sdk以后碰到的诡异问题

问题情况是, 通过jni调用android sdk进行登陆,回调之后,发现cocos2d-x主线程号变了,然后抛出一堆的 open gl error 0x0501 错误,百思不得其解,请大侠赐教。

1 在main函数里面打印了cocos2d-x 主线程号,log如下:
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit ==>> android ui thread is 8967912 curTime 1398584837

void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)

{
if (!CCDirector::sharedDirector()->getOpenGLView())
{
char pthreadID;
time_t curTime;
time(&curTime);
snprintf(pthreadID, sizeof(pthreadID), " android ui thread is %lu curTime %lu ", ( unsigned long int)pthread_self(), (unsigned long int)curTime);
DEBUG_LOG(pthreadID);

    CCEGLView *view = CCEGLView::sharedOpenGLView();
    view->setFrameSize(w, h);
    __android_log_print(ANDROID_LOG_DEBUG,"football","start run !!!!" );
    AppDelegate *pAppDelegate = new AppDelegate();
    CCApplication::sharedApplication()->run();
}
/*
else
{
    ccDrawInit();
    ccGLInvalidateStateCache();

    CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
    CCTextureCache::reloadAllTextures();
    CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);
    CCDirector::sharedDirector()->setGLDefaultValues(); 
}
*/

}

2 在cocos2d-x 主线程里面加了个定时器回调 onScheduleUpdate ,主要用于ui刷新。

初始化的时候打印了当前线程id,运行的log如下, 这里线程号是一致的:
MainScheduleExecutor main thread id 8967912

 代码如下:
 int MainScheduleExecutor::init(TaskProvider * p)
{

DEBUG_LOG("MainScheduleExecutor main thread id " << pthread_self());

m_attached=false;
m_provider=p;
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(MainScheduleExecutor::onScheduleUpdate), this, 0, false);
CCDirector::sharedDirector()->getScheduler()->pauseTarget(this);//明确开始时再开始.
return 0;
}

3 onScheduleUpdate回调函数里面也打印了log,用户输出当前线程号:

在调用sdk之前,log如下:
MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 8967912


调用sdk登陆回来以后,变成这样了:
MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 25912672

之后开始报错,导致游戏黑屏:
D/cocos2d-x debug info(19307): OpenGL error 0x0501 in proj_360.android/libs/cocos2dx/android/…/sprite_nodes/CCSprite.cpp draw 643

代码如下:

void MainScheduleExecutor::onScheduleUpdate(float delta)
{
DEBUG_LOG("onScheduleUpdate main thread id " << pthread_self());

//static int i=0;
TaskPtr t=m_provider->getTask();
while(t)
{
//DEBUG_LOG(“process a main task !”<<t<<" id:"<<++i);
t->handle();
m_provider->finishTask(t);
t=m_provider->getTask();
}

}

4 出现上述问题以后,各种度娘,均不能解决。
回调的线程不是cocos2d-x主线程,把它切回主线程运行,失败。
可能是android 加载opengl 比较慢,延时2s回调,还是不行。

求大神指点下迷津。

走过路过不要错过,顶起来先

你调用android SDK是怎么回来的。登录界面是activity【dialog】还是view。

用的是360 android sdk,走的是activity,这两者有什么区别吗?
登陆ok以后,sdk回调函数触发,通过jni调用c++,c++把回调任务丢到主线程任务列表运行。

在android那边cocos2dxActivity的onResume里加上this.mGLSurfaceView.requestFocus()试试?可能是从那边的sdk回来这边的主线程没能拿到焦点?

是应该走到onResume。
前一步我觉得应该是在回调的时候看下线程是不是主线程。回调应该只是取数据!

按照你的说法加了代码,运行结果还是一样。居然cocos2d-x的主线程号都变了,太诡异了。

我是主线程直接attack到jvm,然后调用jni的,不知道是不是这里的问题。

login之前的log:
16545 D/football(25967): MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 9106120
16546 D/football(25967): MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 9106120
16547 D/football(25967): MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 9106120
16548 D/football(25967): MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 9106120
16549 D/football(25967): MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 9106120

回调以后的log:
18498 D/football(25967): MainScheduleExecutor::onScheduleUpdate ==>> onScheduleUpdate main thread id 25353192
18500 D/NativeCrypto(25967): sslSelect READ fd=55 appData=0xa37ca0 timeout=20000 => 1
18501 D/NativeCrypto(25967): doing handshake ++
18502 D/football(25967): virtual int ReturnMainTask::handle ==>> handle code is 7828007949a443f836c9d3cafe180b1e394b13faf4b0ee575
18503 D/football(25967): void LoginCallBack ==>> delayLoginCallBack call back thread is 25353192 curTime 1398656749

@Override
protected void onResume() {
super.onResume();

    Cocos2dxHelper.onResume();
    this.mGLSurfaceView.onResume();
    this.mGLSurfaceView.requestFocus();
}

回调的线程应该是sdk控制的,不管这是个什么线程,在c++层切回cocos2d gl线程就好了,现在问题是gl线程变了。。。

模拟器上运行能不变么?

该问题已经解决。android登陆是竖屏的,从android切换回来,结果主线程号也变了,ui展现各种open gl错误。

修改如下:
android:configChanges=“orientation|screenSize|smallestScreenSize”>

群主,我问题和你差不多。请问能帮我解决不? 我qq 520041267

|screenSize

这个问题我也出现了,可是screenSize我已经添加了,还是解决不了