Switch between activities in opengl
i have 2 activities in my game, one for the menu and the other is the game. When i click on new game, the game activity start and works fine, but when i press the back button, my main menu does not show anything, just a black screen. The Draw method in CMainMenu is still called by the renderer, if i put some log in it i can see it with logcat. What am i doing wrong?
Here is the code:
Game activity class, the menu activity class is the same, only CGame replaced with 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();
}
开发者_StackOverflow
@Override
protected void onPause()
{
super.onPause();
glSurfaceView.onPause();
}
}
The GLSurfaceView class:
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;
}
}
My renderer:
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);
}
}
I start the new activity with this:
Intent myIntent = new Intent(activity, CGameActivity.class);
activity.startActivity(myIntent);
精彩评论