我是游戏开发的新手,我已经用SDL2写了几个程序。帧速率不是什么大问题,因为我从来没有打算发布这些程序,只是使用SDL_RENDER_PRESENTVSYNC将帧速率限制在60 (我的显示器的刷新率)。现在我想做一个可以发送给朋友的应用程序,我想知道我的帧率实现是否很好。
unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks();
double delta = 0;
while (running)
{
a = SDL_GetTicks();
delta += a - b;
if (delta > 1000/60.0)
{
std::cout << "fps: " << 1000 / delta << std::endl;
Update();
Render();
delta = 0;
}
b = SDL_GetTicks();
}我有一种很好的感觉,我把我的计算搞得一团糟,但我在网上找到的许多其他实现通常看起来都很冗长。如果可能的话,我希望在游戏循环中保持帧上限的实现,并且尽可能简单。感谢任何人的帮助!
发布于 2019-02-11 20:02:20
您应该将行b = SDL_GetTicks();放在if语句内的Update();语句之前。和delta = a - b而不是delta += a - b。您也可以使用b=a而不是调用SDL_GetTicks()函数。
while (running)
{
a = SDL_GetTicks();
delta = a - b;
if (delta > 1000/60.0)
{
std::cout << "fps: " << 1000 / delta << std::endl;
b = a;
Update();
Render();
}
}https://stackoverflow.com/questions/50361975
复制相似问题