Android - turning GLSurfaceView.Renderer into sleep mode (as in Thread.sleep (20)) outside the OnDrawFrame method

I want to control the rendering speed of my GLSurfaceView.Renderer. I implemented a thread in a class that extends GLSurfaceView and periodically puts it in a sleep (true) loop that did not interfere with rendering. There is a good answer here that suggests putting GL Thread to sleep using Thread.sleep in the Renderer.onDrawFrame () method. I would like to handle it from outside the Renderer class. How can this be done when an explicit call requires passing a GL10 object? Thanks.

+2
source share
1 answer

Do not extend GLSurfaceView. If you have not already done so, save the renderer as a variable in your activity class:

public class MyActivity extends Activity { protected GLSurfaceView mGLView; protected GraphicsRenderer graphicsRenderer; // keep reference to renderer protected void initGraphics() { graphicsRenderer = new GraphicsRenderer(this); mGLView = (GLSurfaceView) findViewById(R.id.graphics_glsurfaceview1); mGLView.setEGLConfigChooser(true); mGLView.setRenderer(graphicsRenderer); graphicsRenderer.setFrameRate(30); } } 

Then you can create a method in your renderer to control the frame rate:

 public class GraphicsRenderer implements GLSurfaceView.Renderer { private long framesPerSecond; private long frameInterval; // the time it should take 1 frame to render private final long millisecondsInSecond = 1000; public void setFrameRate(long fps){ framesPerSecond = fps; frameInterval= millisecondsInSeconds / framesPerSecond; } public void onDrawFrame(GL10 gl) { // get the time at the start of the frame long time = System.currentTimeMillis(); // drawing code goes here // get the time taken to render the frame long time2 = System.currentTimeMillis() - time; // if time elapsed is less than the frame interval if(time2 < frameInterval){ try { // sleep the thread for the remaining time until the interval has elapsed Thread.sleep(frameInterval - time2); } catch (InterruptedException e) { // Thread error e.printStackTrace(); } } else { // framerate is slower than desired } } } 
+3
source

All Articles