0
我试图让我的游戏保持在60fps,但是我得到了像我的代码一样的奇怪结果,例如“2-8000 fps”为什么这不是在60?从java中的帧率代码获得奇怪的结果
public void run(boolean fullscreen) {
this.fullscreen = fullscreen;
try {
long lastFrame = 0;
long frames = 0;
init();
while (!done) {
frames++;
long startTime = System.nanoTime()/1000000;
try
{
System.out.println("framerate: " + ((System.nanoTime()/1000000 - startTime)/frames));
// 123456: 6 zeros => 16ms
long nsToSleep = 17000000 - (System.nanoTime() - lastFrame);
System.out.println("ns: " + nsToSleep);
lastFrame = System.nanoTime();
if(nsToSleep > 0)
{
System.out.println("ns2: " + (nsToSleep/1000));
System.out.println("ns3: " + (nsToSleep%1000));
Thread.sleep(nsToSleep/17000000, (int)(nsToSleep % 1000));
}
else
{
Thread.yield(); // Only necessary if you want to guarantee that
// the thread yields the CPU between every frame
}
}
catch(Exception e){
e.printStackTrace();
}
mainloop();
render();
Display.update();
}
cleanup();
}
catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
你确定startTime应该在循环中重新计算吗? – diciu 2009-12-25 08:22:38