在opengl中切换活动

时间:2011-03-03 23:30:44

标签: android

我的游戏中有2个活动,一个用于菜单,另一个用于游戏。当我点击新游戏时,游戏活动开始并正常工作,但是当我按下后退按钮时,我的主菜单没有显示任何内容,只是黑屏。 CMainMenu中的Draw方法仍然由渲染器调用,如果我在其中放入一些日志,我可以用logcat看到它。我做错了什么?

以下是代码:

游戏活动类,菜单活动类是相同的,只有CGame替换为CMenu

public class CGameActivity extends Activity {
private myGlSurfaceView glSurfaceView;
private CGame game;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // Initiate the Open GL view and
    // create an instance with this activity

    game=new CGame(this, this);
    glSurfaceView = new myGlSurfaceView(this, game);

    // set our renderer to be the main renderer with
    // the current activity context
    glSurfaceView.setRenderer(new GlRenderer(this,game));
    setContentView(glSurfaceView);
}

@Override
protected void onResume() 
{
    super.onResume();
    glSurfaceView.onResume();
}

@Override
protected void onPause() 
{
    super.onPause();
    glSurfaceView.onPause();
}

}

GLSurfaceView类:

public class myGlSurfaceView extends GLSurfaceView {

private CScene scene;

public myGlSurfaceView(Context context, CScene scene) {
    super(context);
    this.scene=scene;
}

@Override
public boolean  onTouchEvent  (MotionEvent  event){  
    scene.TapControl(event);
    return true;
}

}

我的渲染器:

public class GlRenderer implements Renderer {

private Context     context;
private CScene      scene;
long mLastTime;

/** Constructor to set the handed over context */
public GlRenderer(Context context, CScene scene) {
    this.context = context;
    this.scene=scene;
}

@Override
public void onDrawFrame(GL10 gl) {
    long now = System.currentTimeMillis();

    if (mLastTime > now) return;
    float dt = (float) ((now - mLastTime) / 1000.0);
    mLastTime = now;
    scene.Update(dt);
    scene.Draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       //Prevent A Divide By Zero By
        height = 1;                         //Making Height Equal One
    }
    gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
    gl.glLoadIdentity();                    //Reset The Projection Matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix

    GLU.gluOrtho2D(gl, 0, width, height, 0);

    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();                    //Reset The Modelview Matrix
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    scene.LoadTextures(gl);
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    gl.glEnable(GL10.GL_BLEND); 
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 
}

}

我用这个开始新的活动:

Intent myIntent = new Intent(activity, CGameActivity.class);

activity.startActivity(myIntent);

0 个答案:

没有答案