问题情况是, 通过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回调,还是不行。
求大神指点下迷津。