Collection of useful utilities for Java games and apps. A lot of interesting utilities that could maybe still find some use if you work with Java...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mightyutils/src/mightypork/utils/math/timing/TimerFps.java

103 lines
1.7 KiB

package mightypork.utils.math.timing;
/**
* Timer for interpolated timing
*
* @author Ondřej Hruška (MightyPork)
*/
public class TimerFps {
private long lastFrame = 0;
private long nextFrame = 0;
private long skipped = 0;
private long lastSkipped = 0;
private static final long SECOND = 1000000000; // a million nanoseconds
private final long FRAME; // a time of one frame in nanoseconds
/**
* New interpolated timer
*
* @param fps target FPS
*/
public TimerFps(long fps) {
FRAME = Math.round(SECOND / (double) fps);
lastFrame = System.nanoTime();
nextFrame = System.nanoTime() + FRAME;
}
/**
* Sync and calculate dropped frames etc.
*/
public void sync()
{
final long time = getTime();
if (time >= nextFrame) {
final long skippedNow = (long) Math.floor((time - nextFrame) / (double) FRAME) + 1;
skipped += skippedNow;
lastFrame = nextFrame + (1 - skippedNow) * FRAME;
nextFrame += skippedNow * FRAME;
}
}
/**
* Get nanotime
*
* @return nanotime
*/
public long getTime()
{
return System.nanoTime();
}
/**
* Get fraction of next frame
*
* @return fraction
*/
public double getFraction()
{
if (getSkipped() >= 1) {
return 1;
}
final long time = getTime();
if (time <= nextFrame) {
return (double) (time - lastFrame) / (double) FRAME;
}
return 1;
}
/**
* Get number of elapsed ticks
*
* @return ticks
*/
public int getSkipped()
{
final long change = skipped - lastSkipped;
lastSkipped = skipped;
return (int) change;
}
/**
* Clear timer and start counting new tick.
*/
public void startNewFrame()
{
final long time = getTime();
lastFrame = time;
nextFrame = time + FRAME;
lastSkipped = skipped;
}
}