requestRender 似乎没有渲染(opengl-es android)
我有一个游戏循环在线程中运行,我的问题是因
mGLSurfaceView.requestRender();
空指针异常而崩溃。我的 onSurfaceCreated 方法中有一个日志标记,它证明表面已创建(至少我认为),所以这不是问题。
我认为问题在于 requestRender 需要非静态引用。当我尝试调用对另一个方法的非静态引用时,它在该行崩溃了。所以我只能假设线程对非静态不太友好。我也无法对 requestRender 进行静态引用(这将是一个明显的解决方案)。
这是我的游戏循环线程
import android.util.Log;
public class GameLoop extends Thread {
private MyGLSurfaceView 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";
@Override
public void run() {
running = LaunchActivity.getRunning();
long beginTime;
long timeDiff;
int sleepTime;
int framesSkipped;
sleepTime = 0;
while (running) {
running = LaunchActivity.getRunning();
Log.d(TAG, "gameRunning");
beginTime = System.currentTimeMillis();
framesSkipped = 0;
// notice the static reference here
GameLogic.update();
Log.d(TAG, "updated");
// not static here
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");
}
}
}
}
这是我的表面创建方法(我认为从长远来看这可能是问题的一部分)
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
Log.d(TAG, "SurfaceCreated");
}
哦,顺便说一句,我正在使用
glOrthof
投影
StackTrace
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): FATAL EXCEPTION: Thread-11
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): java.lang.NullPointerException
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): at basicmelon.games.androidgamething.GameLoop.run(GameLoop.java:33)
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): at java.lang.Thread.run(Thread.java:1096)
Log TAG
10-05 20:28:23.077: DEBUG/input(18923): GLSurfaceViewed
10-05 20:28:23.116: DEBUG/input(18923): Created
10-05 20:28:23.116: DEBUG/input(18923): Resumed
10-05 20:28:23.124: DEBUG/input(18923): gameRunning
10-05 20:28:23.132: DEBUG/input(18923): Updated
10-05 20:28:23.210: DEBUG/input(18923): Paused
I have a gameloop running in a thread, my problem is that the
mGLSurfaceView.requestRender();
crashes with a null pointer exception. I have a log tag in my onSurfaceCreated method which proves that the surface is created (at least I think) so that's not the problem.
I'm thinking that the problem is that requestRender needs a non-static reference. When I tried calling a non-static reference to another method it crashed on that line instead. So I can only assume that threads aren't too friendly with non-statics. I also can't make a static reference to requestRender (which would be an obvious solution).
Here is my game loop thread
import android.util.Log;
public class GameLoop extends Thread {
private MyGLSurfaceView 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";
@Override
public void run() {
running = LaunchActivity.getRunning();
long beginTime;
long timeDiff;
int sleepTime;
int framesSkipped;
sleepTime = 0;
while (running) {
running = LaunchActivity.getRunning();
Log.d(TAG, "gameRunning");
beginTime = System.currentTimeMillis();
framesSkipped = 0;
// notice the static reference here
GameLogic.update();
Log.d(TAG, "updated");
// not static here
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");
}
}
}
}
And here is my surface created method (which I though could be part of the problem at a long shot)
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
Log.d(TAG, "SurfaceCreated");
}
Oh and by the way i'm using
glOrthof
projection
StackTrace
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): FATAL EXCEPTION: Thread-11
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): java.lang.NullPointerException
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): at basicmelon.games.androidgamething.GameLoop.run(GameLoop.java:33)
10-05 20:26:44.694: ERROR/AndroidRuntime(18276): at java.lang.Thread.run(Thread.java:1096)
Log TAG
10-05 20:28:23.077: DEBUG/input(18923): GLSurfaceViewed
10-05 20:28:23.116: DEBUG/input(18923): Created
10-05 20:28:23.116: DEBUG/input(18923): Resumed
10-05 20:28:23.124: DEBUG/input(18923): gameRunning
10-05 20:28:23.132: DEBUG/input(18923): Updated
10-05 20:28:23.210: DEBUG/input(18923): Paused
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会创建 MyGLSurfaceView 对象。您必须将此行放在 while 循环之前的某个位置:
You never create the MyGLSurfaceView object. You have to put this line somewhere prior to the while-loop: