我試圖讓我的遊戲保持在60fps,但我從我的代碼中得到奇怪的結果,如「2-8000 fps」爲什麼這不是在60?從幀率代碼得到奇怪的結果
public static void main(String[] args) {
joglplat m = new joglplat();
while(true){
long startTime = System.nanoTime()/1000000;
try
{
// 123456: 6 zeros => 16ms
long nsToSleep = 16000000 - (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/16000000, (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();
}
m.controls();
m.update();
m.repaint();
System.out.println("framerate: " + (System.nanoTime()/1000000 - startTime));
}
}
從哪裏收集幀計數:
在一個(未經測試/未編譯的,所以可能有一些錯誤,但應該讓你在正確的方向開始)所有樣式?如何? – William 2009-04-20 19:23:07