游戏开发:如何限制FPS?

发布于 2024-09-07 02:27:15 字数 201 浏览 5 评论 0原文

我正在编写一个游戏,我看到 FPS 算法无法正常工作(当他必须计算更多时,他会睡更长的时间......)所以,问题很简单:如何计算具有正确 FPS 的睡眠时间?

我知道以微秒为单位更新游戏一帧需要多长时间,当然还有我想要达到的 FPS。

我正在疯狂地寻找一个简单的例子,但我找不到......

代码可能是Java、C++或伪代码......

I'm writing a game, and I saw the FPS algorithm doesn't work correctly (when he have to calculate more, he sleeps longer...) So, the question is very simple: how to calculate the sleeptime for having correct FPS?

I know how long it took to update the game one frame in microseconds and of course the FPS I want to reach.

I'm searching crazy for a simple example, but I can't find one....

The code may be in Java, C++ or pseudo....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

送你一个梦 2024-09-14 02:27:15

渲染一帧应花费的时间为 1/FPS 秒(如果您的目标是 10 FPS,则应在每一帧上花费 1/10 = 0.1 秒)。因此,如果渲染需要 X 秒,您应该“休眠”1/FPS - X 秒。

例如,将其转换为毫秒,您会得到

ms_to_sleep = 1000 / FPS - elapsed_ms;

如果由于某种原因花费了超过 1/FPS 来渲染帧,您将获得负睡眠时间,在这种情况下,您显然只是跳过睡眠。

The time you should spend on rendering one frame is 1/FPS seconds (if you're aiming for, say 10 FPS, you should spend 1/10 = 0.1 seconds on each frame). So if it took X seconds to render, you should "sleep" for 1/FPS - X seconds.

Translating that to for instance milliseconds, you get

ms_to_sleep = 1000 / FPS - elapsed_ms;

If it, for some reason took more than 1/FPS to render the frame, you'll get a negative sleep time in which case you obviously just skip the sleeping.

嘿看小鸭子会跑 2024-09-14 02:27:15

每帧的微秒数为1000000/frames_per_second。如果您知道您已经花费了 elapsed_microseconds 进行计算,那么您需要睡眠的时间为:

(1000000 / frames_per_second) - elapsed_microseconds

The number of microseconds per frame is 1000000 / frames_per_second. If you know that you've spent elapsed_microseconds calculating, then the time that you need to sleep is:

(1000000 / frames_per_second) - elapsed_microseconds
零度℉ 2024-09-14 02:27:15

尝试...

static const int NUM_FPS_SAMPLES = 64;
float fpsSamples[NUM_FPS_SAMPLES]
int currentSample = 0;

float CalcFPS(int dt)
{
    fpsSamples[currentSample % NUM_FPS_SAMPLES] = 1.0f / dt;
    float fps = 0;
    for (int i = 0; i < NUM_FPS_SAMPLES; i++)
        fps += fpsSamples[i];
    fps /= NUM_FPS_SAMPLES;
    return fps;
}

...按照 http://www.gamedev .net/community/forums/topic.asp?topic_id=510019

Try...

static const int NUM_FPS_SAMPLES = 64;
float fpsSamples[NUM_FPS_SAMPLES]
int currentSample = 0;

float CalcFPS(int dt)
{
    fpsSamples[currentSample % NUM_FPS_SAMPLES] = 1.0f / dt;
    float fps = 0;
    for (int i = 0; i < NUM_FPS_SAMPLES; i++)
        fps += fpsSamples[i];
    fps /= NUM_FPS_SAMPLES;
    return fps;
}

... as per http://www.gamedev.net/community/forums/topic.asp?topic_id=510019

毁我热情 2024-09-14 02:27:15

请查看这篇有关不同 FPS 处理方法的文章。

更新:使用网络档案中的此链接,因为原始网站已消失:https://web.archive.org/web/20161202094710/http://dewitters.koonsolo.com/gameloop.html

Take a look at this article about different FPS handling methods.

UPDATE: use this link from Web Archive as original website disappeared: https://web.archive.org/web/20161202094710/http://dewitters.koonsolo.com/gameloop.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文