Display.sync() issue

Started by CodeBunny, March 18, 2011, 19:46:29

Previous topic - Next topic

CodeBunny

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:

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.

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?

avm1979

Are you on windows?

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

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.

CodeBunny

...that's a bad system bug.

Sweet, that fixed it.  :)

avm1979

This is windows, so it's called a feature :)  Glad this helped!