我帮助制作了我的 FPS 跟踪器,但我不明白为什么我必须previousTime += 1000;
在打印 fps 后添加。如果有人知道告诉我。另外,如果您知道他为什么添加了两次渲染,请解释一下。这是我的代码:
public void run()
{
int frames = 0;
double unprocessedSeconds = 0;
long previousTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int tickCount = 0;
boolean ticked = false;
while(running)
{
//check ticker code
long currentTime = System.nanoTime();
long passedTime = currentTime - previousTime;
previousTime = currentTime;
unprocessedSeconds = passedTime / 1000000000.0;
while(unprocessedSeconds > secondsPerTick)
{
tick();
unprocessedSeconds -= secondsPerTick;
ticked = true;
tickCount++;
if(tickCount % 60 == 0)
{
//System.out.println(frames + " fps");
previousTime += 1000;
fps = frames;
frames = 0;
}
}
if(ticked)
{
render();
frames++;
}
render();
frames++;
}