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

后端 未结 6 903
自闭症患者
自闭症患者 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:15

    When using GLSurfaceView, you perform the drawing in your Renderer's onDrawFrame which is handled in a separate thread by the GLSurfaceView. Simply make sure that each call to onDrawFrame takes (1000/[frames]) milliseconds, in your case something like 33ms.

    To do this: (in your onDrawFrame)

    1. Measure the current time before your start drawing using System.currentTimeMillis (Let's call it startTime)
    2. Perform the drawing
    3. Measure time again (Let's call it endTime)
    4. deltaT = endTime - starTime
    5. if deltaT < 33, sleep (33-deltaT)

    That's it.

提交回复
热议问题