Frame Time Manager

Made this a while back, use it however.

timer = new FrameTimer();

running = true;
while(running){
	update(timer.stepUpdate());
	render();
	timer.stepFrame();
}

public class FrameTimer {
	private long completeFrames  = 0;   // How many frames have been successfully processed
	
	private long currentFrameTime = 0;  // Time at the frame we're processing now
	private long lastFrameTime    = 0;  // Time at the last frame
	private float deltaTime        = 1; // Time between the two
	
	private long lastFPS;   // Time at last FPS
	private int frames;     // The frames processed in this second
	private int currentFPS; // Frames per second, updated every time a second has passed
	
	public FrameTimer() {
		lastFPS = System.currentTimeMillis();
		lastFrameTime = System.currentTimeMillis();
	}
	
	public float stepUpdate(){
		lastFrameTime = currentFrameTime;
		currentFrameTime = System.currentTimeMillis();
		deltaTime = (float) (currentFrameTime - lastFrameTime);
		
		if(System.currentTimeMillis() - lastFPS > 1000){
			currentFPS = frames;
			frames = 0;
			lastFPS += 1000;
		}
		
		frames++;
		
		return deltaTime;
	}
	
	// Long because calculating 9 quintillion frames is kinda realistic...
	// Damn kids and their 2^63-1 frames
	
	public long getFrames(){
		return completeFrames;
	}
	
	public int getConstFPS(){
		return currentFPS;
	}
	
	public void stepFrame(){
		completeFrames++;
	}
	
	public float getDelta(){
		return deltaTime;
	}
}

did you try a compare when using System.nanoTime() ?

[icode]System.nanoTime() * 1.0e-6[/icode] ms unit instead of [icode]System.currentTimeMillis()[/icode], should be more precise.

o/

Well, they’re long integers (They have to be), so decimals won’t help that much. Thanks for the feedback!

aah yes, everything long/int would become double/float. just curious, why do they have to be not-float ?

thanks for sharing! code is really easy to understand :slight_smile:

I did this a while ago for monitoring graphical apps (esp. method calls in paint())

Code is old and kinda derpy, don’t laugh.