Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Open GL renderer

For some reason, I need to restart a GLSurfaceView.Renderer so I want some time in my app to call setRenderer(newRenderer) a second time, which Android doesn't like and throws a IllegalStateException saying "setRenderer has already been called"...

Now, I know this is because the renderer is attached to the GLSurfaceView and I need to unbind this renderer to the surface view, so I can call again setRenderer.

Anyone has a solution for this ?

P.S.: Code looks like this :

render = new Renderer(this);

setContentView(R.layout.main);
graphicView = (GLSurfaceView) this.findViewById(R.id.graphicView);

//DO STUFF

graphicView.setRenderer(render);

//DO STUFF

Renderer newRender = new Renderer();
graphicView.setRenderer(newRender); <= ...and Android hates this line sooo much

Thanks !

like image 493
turbodoom Avatar asked Feb 03 '12 15:02

turbodoom


2 Answers

Found the answer here, this "LemonDev" do saved my life :

One can stop/destroy an OpenGL surface view and set the new GLSurfaceView a new renderer by calling the OnPause() and postDelayed the destruction with a Handler and a Runnable, like this :

final Renderer newRender = new GRenderer(this);
final LinearLayout ll = (LinearLayout) this.findViewById(R.id.main);
graphicView.onPause();

Handler handler = new Handler();
class RefreshRunnable implements Runnable{

    public RefreshRunnable(){

    }

    public void run(){
        ll.removeView(findViewById(R.id.graphicView));

        GLSurfaceView surfaceview = new GLSurfaceView(getApplication());
        surfaceview.setId(R.id.graphicView);
        surfaceview.setRenderer(newRender);

        ll.addView(surfaceview);

        graphicView = (GLSurfaceView) findViewById(R.id.graphicView);
    }
};

RefreshRunnable r = new RefreshRunnable(this);
handler.postDelayed(r, 500);

This will create a new renderer and pause the GLSurfaceView. The application then will "destroy" some internal stuffs that you can't touch with the SDK... After this, you can destroy the GLSurfaceView, create another one and set the new renderer to it.

My problem now is that the textures are not refreshed yet but, still, this is great !

like image 188
turbodoom Avatar answered Oct 14 '22 12:10

turbodoom


Do you really need to call setRenderer twice?

The android documentation mentions :

This method should be called once and only once in the life-cycle of a GLSurfaceView.

Instead, I guess you want to pause the rendering?

Use setRenderMode(RENDERMODE_WHEN_DIRTY); to pause the renderer

like image 39
rockeye Avatar answered Oct 14 '22 10:10

rockeye