简体   繁体   中英

OpenGL ES 2.0 - How can multiple views use the same renderer?

Right now I have my ApplicationActivity , this activity is responsible for managing multiple views ( GLSurfaceViews ). Can / Should I have all the views set the renderer to a "global" renderer?

Code:

public class ApplicationActivity extends Activity 
{
    private static final String TAG = ApplicationActivity.class.getSimpleName();

    private final Stack<Screen> screens = new Stack<Screen>();
    private GlRenderer glRenderer;

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

            Log.d(TAG, "Main Activity Created");

        setupGraphics();

        ChangeScreen(new MainMenu(this, glRenderer)); //Creating a new Screen sets the renderer
    }

    private void setupGraphics()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,     WindowManager.LayoutParams.FLAG_FULLSCREEN);

        glRenderer = new GlRenderer(this);
    }


    public void Draw() //Is called by the glRenderer onDrawFrame() { mainActivity.Draw() } 
    {

    }
}

Its the same activity switching between GLSurfaceViews and by my knowledge I believe that the method setRenderer sets the view renderer and then starts the rendering thread (creating a new thread) but I don't want to recreate the thread every time I switch between views - may create potential problems.

So in the end I want a Renderer class just to keep graphics sepreate from business logic and such but, I don't know if using one Renderer is even possible, without setting the thread again?

You can only use Multiple Views with the same Renderer only if you properly switch out between them with GLSurfaceView.onPause() / .onResume();

My specific case:

@Override
protected void onPause() //Overrides onPause from Activity
{
    surfaceViews.peek().onPause();
    super.onPause();
}

So everytime the activity pauses I would have to pause the current View. And if the Activity resumes then resume the View also.

I also have a method called SetView which will either (pause and remove then change to another View) or (pause and then change to another View) this is accomplished using a Stack

public void SetView(View screen)
{   
    if (!screens.empty())
    {
        screens.peek().onPause();
        screens.pop();
    }

    screens.push(screen);
    setContentView(screens.peek());
}

Of course though because we are using Views instead of Activities now we must Override the onBackPressed() to go back to previous Views.

@Override
public void onBackPressed() 
{       
    if (screens.size() == 1)
        super.onBackPressed();
    else
    {
        screens.pop();
        setContentView(screens.peek());
        screens.peek().onResume();
    }
}

By doing new GLRenderer() you create new instance of your class. So there is no problem to have the same renderer used in different activities.

EDIT: I seem to misunderstand your question - if you want many GL surfaces visible at once, then no, it is not possible. But it got nothing to do with reusing renderer code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM