本文介绍了如何使用Android的GLSurfaceView.RENDERMODE_CONTINUOUSLY时限制帧率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++游戏通过JNI在Android中运行。帧速率变化大约20-45fps由于场景复杂度。任何超过30fps的是愚蠢的游戏;它只是燃烧的电池。我想帧速率限制为每秒30帧。

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 burning battery. I'd like to limit the frame rate to 30 fps.

  • 我可以切换到RENDERMODE_WHEN_DIRTY,并使用定时器或的ScheduledThreadPoolExecutor到requestRender()。但是,这增加了额外的移动部件,可能会或可能不会持续,正常工作的整体混乱。
  • 在我试过注射Thread.sleep()方法时,事情很快运行,但是这似乎并没有在所有的工作对于小时间值。它可能只是支持事件到队列,无论如何,没有实际暂停。

有没有在API中一个capFramerate()方法隐藏?任何可靠的方式做到这一点?

Is there a "capFramerate()" method hiding in the API? Any reliable way to do this?

推荐答案

从马克的解决方案几乎是好的,但不完全正确。的问题是,交换本身需要花费相当长的时间(特别是如果所述的视频驱动程序是缓存指令)。因此,你必须考虑到这一点,否则你会比需要的较低的帧速率结束。所以,事情应该是:

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.

这篇关于如何使用Android的GLSurfaceView.RENDERMODE_CONTINUOUSLY时限制帧率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:37