我希望我的 GL 渲染器设置为“DIRTY”并使用线程来控制 FPS,但由于某种原因,requestRender
调用似乎被一起跳过了。我有一种感觉,这可能与线程没有获得正确的上下文有关。有人知道将GLSurfaceView
上下文传递给其他线程的好习惯吗?
这是我的线程:
public class GameLoop extends Thread {
private GLSurfaceView myGLSurfaceView;
private final static int maxFPS = 30;
private final static int maxFrameSkips = 5;
private final static int framePeriod = 1000 / maxFPS;
public static boolean running;
public final static String TAG = "input";
public GameLoop(){
myGLSurfaceView = MyLaunchActivity.getMyGLSurfaceView();
}
@Override
public void run() {
running = MyLaunchActivity.getRunning();
long beginTime;
long timeDiff;
int sleepTime;
int framesSkipped;
sleepTime = 0;
while (running) {
running = MyLaunchActivity.getRunning();
//Log.d(TAG, "gameRunning");
beginTime = System.currentTimeMillis();
framesSkipped = 0;
GameLogic.update();
myGLSurfaceView.requestRender();
//Log.d(TAG, "rendered");
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(framePeriod - timeDiff);
if (sleepTime > 0) {
try{
Thread.sleep(sleepTime);
//Log.d(TAG, "sleeping" + String.valueOf(sleepTime));
}catch(InterruptedException e){}
}
while(sleepTime < 0 && framesSkipped < maxFrameSkips){
GameLogic.update();
sleepTime += framePeriod;
framesSkipped++;
Log.d(TAG, "Frames Skipped");
}
}
}
}
这是我的表面视图的构造函数...
public MyGLSurfaceView(Context context, AttributeSet attrs){
super(context, attrs);
myGLSurfaceRenderer = new MyGLSurfaceRenderer();
setRenderer(myGLSurfaceRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
Log.d(TAG, "GLSurfaceViewed");
}