如果您只有一个活动和一个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胡闹了,你不是有一个单一的渲染对象,你只需告诉它渲染和时间。您可以对其进行管理,以便仅在需要或预期需要时加载模型和纹理。如果您决定在多个地方使用相同的模型,它也会使事情变得更加简单。如果你想在你的菜单中放置一个模型,这个模型也可以在游戏中使用,那么你可以只加载一次并重复使用多次!