[java] Frame Rate control

Started by
4 comments, last by Abob 21 years, 5 months ago
In the java game programming tutorials I have been reading they have something like this
    
   public void run ()	{
	while (true)  {	
	    DrawFrame();
                repaint ();
                 try
		{	Thread.sleep (REFRESH_RATE);
		}
		catch (Exception exc)
		{}
	}
    }
  
This dos not seem right to me. I think that that would run at deferent speeds on deferent computers. Do I not understand Thread.sleep or is there a better way to do it? Thanks for any help [edited by - Abob on November 3, 2002 10:52:11 PM]
Arrg!!
Advertisement
Thread.Sleep( x ) does exactly that, it sleeps the program. It will stop the program from running excessively fast on fast computers but will not guarantee a constant framerate. If you can measure the framerate or speed of the computer it is running on then you could just adjust the amount of time the thread sleeps. This is only neccessary if timing is paramount though, usually using Thread.Sleep() will work well enough.

Hope that helps
______________________________"Crack a government encryption code on my laptop? Easy as really difficult pie." - Willow.------------------------------
Hi,

normally you should try get the highest frame rate possible. To keep your game in sync you should calculate the time which has passed between the frames. In this case for example: a object which travels from point A to B takes 2 sec, it always needs 2 sec for this distance independant if the system is fast or not. The different is that the fast system will draw more frames, than the slow system, thus the animation looks smoother.

If you control your game by framerate the playability depends on the speed of the system and this is not good.


ciao knudde
quote:Original post by Knudde
Hi,

normally you should try get the highest frame rate possible. To keep your game in sync you should calculate the time which has passed between the frames. In this case for example: a object which travels from point A to B takes 2 sec, it always needs 2 sec for this distance independant if the system is fast or not. The different is that the fast system will draw more frames, than the slow system, thus the animation looks smoother.

If you control your game by framerate the playability depends on the speed of the system and this is not good.


ciao knudde


Yes. I know about that in C++. The thing is that all the Java code I have seen dos not do that. So I was wondering if there was a reason (like no accurate timer).
Arrg!!
You hit the nail on the head. The java timer, in win os, has a 50 ms granularity... So if you need to be more accurate the 50 ms, try looking up snowmoon''s timer, it has been posted a hundred times in this forum. Otherwise I believe there is an example of one on the javagaming.org site..
Yeah, and if you lock the frame rate by using sleep or snowmoons timer then by updating graphics by the number of tick counts that have passed you are in essence doing the same thing that knudde said. The only problem would be if you set REFRESHRATE to something like 20 (50 fps) and your applet runs on a really crappy computer that can''t even do that.

Its IMPOSSIBLE to get smooth animation using System.currentTimeMillis() on win 98-ME. The two games on my site use thread.sleep(REFRESHRATE). But even if you use snowmoons timer it only runs at 300-500 ticks per second on my machine wich is way to wide of a range to get smooth animation with updating by passed time. (not sure if that made sense.) Using sleep and notify is the only way to go.

This topic is closed to new replies.

Advertisement