gpt4 book ai didi

c++ - 调节后检索/调试当前 FPS?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:33:34 24 4
gpt4 key购买 nike

我希望我的游戏能够以固定的 FPS 在我家附近的多台计算机上运行。我有一个简单的问题:如何调试当前的 fps

我是这样调节我的 fps 的:

SDL_Init(SDL_INIT_EVERYTHING);
const unsigned FPS = 20;
Uint32 start = SDL_GetTicks(); // How much time (milliseconds) has passed after SDL_Init was called.

while (true)
{
start = SDL_GetTicks();

if (1000 / FPS > SDL_GetTicks() - start)
SDL_Delay(1000 / FPS - (SDL_GetTicks() - start)); // Delays program in milliseconds

我认为这应该可以调节我的帧率。问题是,我如何获得当前的 fps?

我试过了

std::stringstream fps;
fps << 1000 / FPS - (SDL_GetTicks() - start);
SDL_WM_SetCaption(fps.str().c_str(), NULL); // Set the window caption

fps << start / 1000; // and vice versa

但他们都没有给我想要的东西。

最佳答案

试一试这个 block :

while(true)
{
// render
// logic
// etc...

// calculate (milli-)seconds per frame and frames per second
static unsigned int frames = 0;
static Uint32 start = SDL_GetTicks();
Uint32 current = SDL_GetTicks();
Uint32 delta = (current - start);
frames++;
if( delta > 1000 )
{
float seconds = delta / 1000.0f;
float spf = seconds / frames;
float fps = frames / seconds;
cout << "Milliseconds per frame: " << spf * 1000 << endl;
cout << "Frames per second: " << fps << endl;
cout << endl;

frames = 0;
start = current;
}
}

请注意,uint/uint 将产生一个 uint,而 uint/float 是一个 float

关于c++ - 调节后检索/调试当前 FPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8861037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com