gpt4 book ai didi

java - 将 GLSurfaceView 上下文传递给线程

转载 作者:行者123 更新时间:2023-12-01 15:41:41 24 4
gpt4 key购买 nike

我希望将我的 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");
}




}
}


}

这是我的表面 View 的构造函数......

 public MyGLSurfaceView(Context context, AttributeSet attrs){
super(context, attrs);
myGLSurfaceRenderer = new MyGLSurfaceRenderer();
setRenderer(myGLSurfaceRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
Log.d(TAG, "GLSurfaceViewed");

}

最佳答案

myGLSurfaceView = MyLaunchActivity.getMyGLSurfaceView();

有关您的代码的一些问题:

  • 如何设置上下文?
  • 您是否在初始化 MyLaunchActivity.getMyGLSurfaceView() 返回的 MyGLSurfaceView 后检查是否创建了 GameLoop 对象?

您可以做的第一件事是在 run() 方法中添加 Exception 的 catch 子句,以查看是否存在任何问题来解释为什么未调用该方法。

关于java - 将 GLSurfaceView 上下文传递给线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com