在android平台,刷帧的调用过程是这样的:
GL线程调用onDrawFrame函数,然后…
Cocos2dxRenderer.java:
@Override
public void onDrawFrame(final GL10 gl) {
/*
* No need to use algorithm in default(60 FPS) situation,
* since onDrawFrame() was called by system 60 times per second by default.
*/
if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {
Cocos2dxRenderer.nativeRender();
} else {
final long now = System.nanoTime();
final long remain = mLastTickInNanoSeconds + Cocos2dxRenderer.sAnimationInterval - now;
if (remain > 0) {
try {
Thread.sleep(remain / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}
/*
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
mLastTickInNanoSeconds = System.nanoTime();
Cocos2dxRenderer.nativeRender();
}
}
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env) {
cocos2d::CCDirector::sharedDirector()->mainLoop();
}
暂停GL线程的方法:
Cocos2dxGLSurfaceView.java
@Override
public void onPause() {
// super.onPause();
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleOnPause();
}
});
}
游戏进入后台的时候,在没有调用GLThread.onPause( )的情况下,GL线程没有调用onDrawFrame方法不刷帧了,这是什么原因?
这样会导致CCDirector:sharedDirector():getScheduler():scheduleScriptFunc( ) 设置的时间调度都不能执行。
有什么办法控制GL线程后台时继续运行吗?
事实上官方提供的GLSurfaceView是没有调用onPause方法的,那么GL线程是如何停止的?
通过反射把log打开:
public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private final static String TAG = “GLSurfaceView”;
private final static boolean LOG_ATTACH_DETACH = false;
private final static boolean LOG_THREADS = false;
private final static boolean LOG_PAUSE_RESUME = false;
private final static boolean LOG_SURFACE = false;
private final static boolean LOG_RENDERER = false;
private final static boolean LOG_RENDERER_DRAW_FRAME = false;
private final static boolean LOG_EGL = false;
然而并没有打印log( 运行的代码跟我下载的源码版本不一致的原因?)
问题1:没有调用GLThread.onPause方法GL线程是如何停止的?
问题2:能否手动控制GL线程的暂停与运行?
问题3:如果不能,但需要每秒执行一些操作,在lua上除了CCDirector:sharedDirector():getScheduler():scheduleScriptFunc( ) , 还有其他不依赖GL线程的方法吗?