如果您只有一個活動和一個GLSurfaceView,則可以通過操作呈示器對象來切換呈現的內容。
public class MyRenderer implements Renderer {
Vector<String> modelsToLoad;
HashMap<String, Model> models;
String[] modelsToDraw;
Context context;
@Override
public void onDrawFrame(GL10 gl) {
// load models ahead of time
while(modelsToLoad.size()>0){
String modelFilename = modelsToLoad.remove(0);
models.put(modelFilename, new Model(modelFilename,context,gl));
}
// keep drawing current models
for(int i = 0;i<modelsToDraw.length;i++){
models.get(modelsToDraw[i]).draw(gl);
}
}
// queue models to be loaded when onDraw is called
public void loadModel(String filename){
modelsToLoad.add(filename);
}
// switch to in-game scene
public void drawGame(){
modelsToDraw = new String[]{"tank.mdl", "soldier.mdl"};
}
// switch to menu scene
public void drawMenuBackground(){
modelsToDraw = new String[]{"bouncingBall.mdl", "gun.mdl"};
}
}
然後在的onCreate:
MyRenderer myRenderer;
public void onCreate(Bundle bundle){
super.onCreate(bundle);
// set layout which has everything in it
setContentView(R.layout.main);
myRenderer = new Renderer(this);
// load menu models
myRenderer.loadModel("bouncingBall.mdl");
myRenderer.loadModel("gun.mdl");
// set up the glsurfaceview
GLSurfaceView mGLView = findViewById(R.id.glsurfaceview1);
mGLView.setRenderer(myRenderer);
// set the renderer to draw menu background objects
myRenderer.drawMenuBackground();
// set the new game button to start the game
ImageButton newGameButton = findViewById(R.id.new_game_button1);
newGameButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// make menu invisible
findViewById(R.id.menu_linearLayout1).setVisibility(View.GONE);
// tell renderer to render game scene
myRenderer.drawGame();
}
});
// make the menu visible
findViewById(R.id.menu_linearLayout1).setVisibility(View.VISIBLE);
// finally we have some time whilst the user decides on their menu option
// use it to load game models in anticipation of the user clicking new game
myRenderer.loadModel("tank.mdl");
myRenderer.loadModel("soldier.mdl");
}
因此,而不是兩個渲染物體或多個GLSurfaceViews胡鬧了,你不是有一個單一的渲染對象,你只需告訴它渲染和時間。您可以對其進行管理,以便僅在需要或預期需要時加載模型和紋理。如果您決定在多個地方使用相同的模型,它也會使事情變得更加簡單。如果你想在你的菜單中放置一個模型,這個模型也可以在遊戲中使用,那麼你可以只加載一次並重復使用多次!