Android的OpenGL ES的對象feeze我已成立了一個項目的OpenGL ES,併成功導入jBox2D庫。我做了一個測試項目,其中包括兩個正在倒下的測試項目。很棒!然而,當屏幕進入睡眠狀態或我鎖住屏幕,掉落物品停在我鎖定屏幕的位置,當我解鎖屏幕,他們甚至不會繼續下跌只是凍結和另外兩個矩形將在起始位置創建。所以它似乎重置了整個應用程序,但之前的對象以某種方式凍結在那裏。睡眠/鎖屏
下面是代碼:
package com.example.opengltest;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
GLSurfaceView surface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
surface = new GLSurfaceView(this);
surface.setRenderer(new SimpleRenderer());
setContentView(surface);
}
protected void onPause() {
super.onPause();
surface.onPause();
}
protected void onResume() {
super.onResume();
surface.onResume();
}
}
渲染:
package com.example.opengltest;
import java.util.HashSet;
import java.util.Set;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import android.opengl.GLSurfaceView.Renderer;
public class SimpleRenderer implements Renderer {
private static World world;
private static Set <Body> bodies = new HashSet<Body>();
private Rect r;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
world = new World(new Vec2(0.0f, -9.8f), false);
r = new Rect();
createObject(300, 500, 0.75f, 0.75f, BodyType.STATIC);
createObject(360, 1200, 0.75f, 0.75f, BodyType.DYNAMIC);
gl.glViewport(0, 0, 600, 1024);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, 600, 0, 1024, 1, -1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
private void createObject(int x, int y, float w, float h, BodyType bType) {
BodyDef boxDef = new BodyDef();
boxDef.position.set(new Vec2(x/30/2, y/30/2));
boxDef.type = bType;
PolygonShape shape = new PolygonShape();
shape.setAsBox(w, h);
Body box = world.createBody(boxDef);
FixtureDef boxFixture = new FixtureDef();
boxFixture.density = 1;
boxFixture.shape = shape;
box.createFixture(boxFixture);
bodies.add(box);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Vec2 vec;
for(Body body : bodies) {
vec = body.getPosition();
r.draw(gl, vec.x * 30, vec.y * 30, body.getAngle());
}
world.step(1/60f, 8, 3);
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
}
}
隨着矩形類我只是畫出矩形,所以我認爲這個問題是在此代碼。所以問題是我如何能夠以某種方式在屏幕鎖定中「保存」應用程序的狀態並防止凍結屏幕上的對象。所以基本上是在屏幕鎖定的狀態下繼續應用程序。
謝謝!