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

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

    If you don't want to rely on Thread.sleep, use the following

    double frameStartTime = (double) System.nanoTime()/1000000;
    // start time in milliseconds
    // using System.currentTimeMillis() is a bad idea
    // call this when you first start to draw
    
    int frameRate = 30;
    double frameInterval = (double) 1000/frame_rate;
    // 1s is 1000ms, ms is millisecond
    // 30 frame per seconds means one frame is 1s/30 = 1000ms/30
    
    public void onDrawFrame(GL10 gl)
    {
      double endTime = (double) System.nanoTime()/1000000;
      double elapsedTime = endTime - frameStartTime;
    
      if (elapsed >= frameInterval)
      {
        // call GLES20.glClear(...) here
    
        UpdateGame(elapsedTime);
        RenderGame(gl);
    
        frameStartTime += frameInterval;
      }
    }
    

提交回复
热议问题