Constant frame rate.

I am currently calculating the time between frames in my game an using that to update animation etc, but it isnt smooth and i thought it ould be much simpler and smoother if the frame rate was constant as then i could update everything by just 1 tick. Would this be done by using vsync set at say 60hx to give 60fps, but then what happens if you cant do a complete loop every time, do you skip rendering the frame if you calculate that there isnt enought time left? Is this possible with j2d?

:slight_smile:

Why not just a Timer object? You can put your game loop and rendering inside the actionListener for the Timer. It keeps a very constant rate of time, I’ve never had any issues with it. Do you know how to use one?

but doesnt that mean all my code will be in the event handlign thread, wont that cause control problems? Anyway ive switched to lwjgl now, but id ont know how to do it in that either.
What i would like to know is why some games (eg aliean flux) use this method and the advantagesw, disadvantages it gave them.

Advantages:

  • Dead easy to code
  • Ultraslick feel

Disadvantages:

  • Won’t cope with slower machines so well (workaround: provide 50% framerate option)
  • Endless arguments on forums from people who disagree (workaround: ignore them and carry on making games)

Cas :slight_smile:

I agree, swing Timers are fine for this sort of thing. If you’re aiming for a framerate around 50 Hz, then the lack of GUI responsiveness will probably not be sensed by the user anyway.

Can i cahnge the refresh rate in windowed mode in lwjgl, i set the display mode but when enabling vsync it is still at the old rate? Also i ust noticed how slow Display.setTitle(…) seems to be, removing a call to it every frame (to display the fps)gave my frame rate a boost from 120 to 140 (now i only change the title when the rounded fps has changed).

no, you’re at the mercy of the OS refresh rate, you could consider using Display.sync

hmm, thats sounds a bit violent… but you shouldn’t be setting it each update anyway - perhaps once a second or less

Yeah, Display.sync(int fps) will give you what you want. It synchronizes you to the specified frames per second almost perfectly. In measured fps, my program is almost always between 59 and 61 when I use the sync method. You could go as low as 30 fps for most games and people wouldn’t even notice a difference.

Well, Display.sync(…) doesnt work on my system, neither does sync2() or sync3(), but i have now come up with a solution that lets me have a tick based game at any frame rate. It uses this class shown below:

eg. in mainloop:

int t = TickTimerInstnce.getNumIntervalsPassed();
for(int i=0; i<t; i++)
updateWorldOneTick();



import org.lwjgl.Sys;

/*
 *a class to help tick based animation with variable frame rate
 */
 
public class TickTimer{
	
	//length of each interval, or tick
	private float interval;
	//amount through interval at last check
	private float amount;
	//time at last check, in millis
	private float lastCheck;
	//the timer has been started
	private boolean started;
	
	public TickTimer(float interval){
		this.interval = interval;
	}
	
	
	/**
	 *starts the timer going, should be called before using the timer
	 */
	public void start(){
		lastCheck = FPSCounter.getTime();
		started = true;
	}
	
	
	public boolean isStarted(){
		return started;
	}
	
	
	/**
	 *returns the number of intervals that have passed since
	 *this method was last called, or since start() was called
	 */
	public int getNumIntervalsPassed() throws IllegalStateException{
		//started already?
		if(!started)
			throw new IllegalStateException("Timer must be started.");
		//
		float now = (Sys.getTime()*1000f) / (float)Sys.getTimerResolution();
		float delta = now - lastCheck;
		lastCheck = now;
		//
		float newAmount = amount+delta;
		int intervalCount = 0;
		//
		while(newAmount >= interval){
			newAmount -= interval;
			intervalCount++;
		}
		amount = newAmount;
		return intervalCount;
	}
}