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;
}
}