How to limit framerate when using Android's GLSurfaceView.RENDERMODE_CONTINUOUSLY?

后端 未结 6 906
自闭症患者
自闭症患者 2020-12-12 16:53

I have a C++ game running through JNI in Android. The frame rate varies from about 20-45fps due to scene complexity. Anything above 30fps is silly for the game; it\'s just b

6条回答
  •  温柔的废话
    2020-12-12 17:14

    The solution from Mark is almost good, but not entirely correct. The problem is that the swap itself takes a considerable amount of time (especially if the video driver is caching instructions). Therefore you have to take that into account or you'll end with a lower frame rate than desired. So the thing should be:

    somewhere at the start (like the constructor):

    startTime = System.currentTimeMillis();
    

    then in the render loop:

    public void onDrawFrame(GL10 gl)
    {    
        endTime = System.currentTimeMillis();
        dt = endTime - startTime;
        if (dt < 33)
            Thread.Sleep(33 - dt);
        startTime = System.currentTimeMillis();
    
        UpdateGame(dt);
        RenderGame(gl);
    }
    

    This way you will take into account the time it takes to swap the buffers and the time to draw the frame.

提交回复
热议问题