好的,所以我正在制作这个小程序'并希望能够计算FPS。我有一个想法,如果我挂钩一个被称为每帧的函数,我可以计算FPS?
这里完全失败了,现在再次看一下这段代码,我觉得我觉得这样做有多么愚蠢:
int FPS = 0;
void myHook()
{
if(FPS<60) FPS++;
else FPS = 0;
}
显然这是一次愚蠢的尝试,虽然不确定为什么我甚至在逻辑上认为它可能起作用......
但是,是否可以通过挂钩每个帧调用的函数来计算FPS?
我坐下来想着可能的方法来做到这一点,但我无法想出任何东西。任何信息或任何内容都会有所帮助,感谢阅读:)
答案 0 :(得分:1)
你可以调用你的钩子函数进行fps计算,但在能够这样做之前你应该:
每次执行重绘时通过递增计数器来跟踪帧
跟踪自上次更新以来经过的时间(获取挂钩功能的当前时间)
计算以下内容
frames / time
使用高分辨率计时器。使用合理的更新率(1/4秒等)。
答案 1 :(得分:1)
这应该可以解决问题:
int fps = 0;
int lastKnownFps = 0;
void myHook(){ //CALL THIS FUNCTION EVERY TIME A FRAME IS RENDERED
fps++;
}
void fpsUpdater(){ //CALL THIS FUNCTION EVERY SECOND
lastKnownFps = fps;
fps = 0;
}
int getFps(){ //CALL THIS FUNCTION TO GET FPS
return lastKnownFps;
}
答案 2 :(得分:1)
您可以找到多块帧之间的时差。此时间的倒数将为您提供帧速率。你需要实现一个finction getTime_ms(),它以ms为单位返回当前时间。
unsigned int prevTime_ms = 0;
unsigned char firstFrame = 1;
int FPS = 0;
void myHook()
{
unsigned int timeDiff_ms = 0;
unsigned int currTime_ms = getTime_ms(); //Get the current time.
/* You need at least two frames to find the time difference. */
if(0 == firstFrame)
{
//Find the time difference with respect to previous time.
if(currTime_ms >= prevTime_ms)
{
timeDiff_ms = currTime_ms-prevTime_ms;
}
else
{
/* Clock wraparound. */
timeDiff_ms = ((unsigned int) -1) - prevTime_ms;
timeDiff_ms += (currTime_ms + 1);
}
//1 Frame:timeDiff_ms::FPS:1000ms. Find FPS.
if(0 < timeDiff_ms) //timeDiff_ms should never be zero. But additional check.
FPS = 1000/timeDiff_ms;
}
else
{
firstFrame = 0;
}
//Save current time for next calculation.
prevTime_ms = currTime_ms;
}