2011-11-21 31 views
1

我正在开发Android游戏,它使用OpenGL 1.0。我创建了一个菜单,这是一个布局简单的活动,但我不喜欢它,所以我决定在OpenGL中创建它,但是我不知道如何切换到实际的游戏。我想在另一个GLSurfaceView中做,因为在一个中创建所有东西,然后我必须在开始时加载所有纹理,这可能会很慢。Android OpenGL更改setContentView或setRenderer?

我的问题是,有可能以某种方式更改setContentView或setRenderer? 该应用程序的基本功能就像这里:http://developer.android.com/resources/tutorials/opengl/opengl-es10.html#creating其中setContentView是我控制Touch和Key事件的地方,在那里我将setRenderer设置为GLSurfaceView。

回答

1

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