GameEngine with loop

 public class GameContainer implements Runnable {

    public static int scale = 6;
public static int w = 100, h = (w * 16) / 9;
public static String title = "BitHomeEngine";

private float UPDATE_CAP = 1 / 60f;
private boolean running = false;

private Window window = null;
private Thread thread = null;

public void initialize() {
window = new Window();
thread = new Thread(this);
}

public void start() {
initialize();
running = true;
thread.run();
}

public void run() {
boolean render;
double firstTime = System.nanoTime() / 1000000000.0;
double passedTime;
double unprocessedTime = 0.0;
double lastTime;

while (running) {
render = false;
lastTime = System.nanoTime() / 1000000000.0;
passedTime = lastTime - firstTime;
firstTime = lastTime;
unprocessedTime += passedTime;

while (unprocessedTime > UPDATE_CAP) {
render = true;
// TODO update here
unprocessedTime -= UPDATE_CAP;

}

if (render) {
window.update();
// TODO render
}
}
}

public static void main(String[] args) {
new GameContainer().start();
}

}

Comments

Popular posts from this blog