// Run Method
boolean isRunning = false;
// Variables for the FPS and UPS counter
private int ticks = 0;
private int frames = 0;
private int FPS = 0;
private int UPS = 0;
public double delta = 0;
// Used in the "run" method to limit the frame rate to the UPS
private boolean limitFrameRate = true;
private boolean shouldRender;
public synchronized void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
long lastTimer = System.currentTimeMillis();
delta = 0D;
while (isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
// If you want to limit frame rate, shouldRender = false
shouldRender = false;
// If the time between ticks = 1, then various things (shouldRender = true, keeps FPS locked at UPS)
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
if (!limitFrameRate && ticks > 0)
shouldRender = true;
// If you should render, render!
if (shouldRender) {
frames++;
render();
}
// Reset stuff every second for the new "FPS" and "UPS"
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
FPS = frames;
UPS = ticks;
frames = 0;
ticks = 0;
}
}
stop();
}