Hello Guest

Display.sync() issue

  • 3 Replies
  • 6409 Views
Display.sync() issue
« on: March 18, 2011, 19:46:29 »
Previously, in my game loop, I used a while() loop in conjunction with System.nanotime() to regulate my fps, since sleeping the thread proved to be way too inexact. The code was as follows:

Code: [Select]
while(System.nanoTime() < frameTracker + frameInterval);
frameTracker = System.nanoTime();

I know this is a waste of system resources, but it provided very accurate loop control and everything ran silky smooth, so I left it for a bit. Then, I tried using Display.sync, which sounded like it was a much better alternative.

Code: [Select]
Display.sync(framesPerSecond);
Unfortunately, things are now choppy. It's not severe, but sporadically the game looks like it's running at 20-30 fps, even though it's running at a speed well over 60 fps. What's going wrong? Is it my driver? If this problem persists, I'm going to have to stick with the hard pause - I don't want to, but playability comes first.

Also, when I use Display.sync, it's not as accurate as I'd like it to be. I tried setting sync to 55 and 50, so see if the problem was my monitor refresh rate, and the problem continued but my received performance was 45 fps. Can sync only receive particular values?

Re: Display.sync() issue
« Reply #1 on: March 19, 2011, 00:44:52 »
Are you on windows?

If so, add this to the start of your program:

Code: [Select]
Thread timerAccuracyThread = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (Exception e) {
}
}
}});
timerAccuracyThread.setDaemon(true);
timerAccuracyThread.start();

The windows timer is forced to be accurate while there's a sleeping thread.  Possibly, calling nanoTime() frequently had the same effect.  Yeah this looks like voodoo, but it's a (sad) known fact...

Otherwise, it'll be very inaccurate, 10ms+ off at times.
« Last Edit: March 19, 2011, 00:47:12 by avm1979 »

Re: Display.sync() issue
« Reply #2 on: March 19, 2011, 02:20:18 »
...that's a bad system bug.

Sweet, that fixed it.  :)

Re: Display.sync() issue
« Reply #3 on: March 19, 2011, 02:27:16 »
This is windows, so it's called a feature :)  Glad this helped!