package com.betato.gamedisplay;

public class GameLoop {
	
	private int targetFps = 60;
	private int nanoFps = 1000000000 / targetFps;
	private int targetUps = 60;
	private int nanoUps = 1000000000 / targetUps;
	
	public boolean running = true;
	
	public GameLoop(int targetFps, int targetUps) {
		this.targetFps = targetFps;
		this.targetUps = targetUps;
		nanoFps = 1000000000 / targetFps;
		nanoUps = 1000000000 / targetUps;
	}
	
	// Runs the GameLoop
	public void run() {
		long startTime = System.nanoTime();
		long deltaFps = 0;
		long deltaUps = 0;
		long deltaDisplay = 0;
		int framecount = 0;
		int updatecount = 0;

		while (running) {
			// Get current time
			long currentTime = System.nanoTime();
			// Get time since last loop
			deltaFps += currentTime - startTime;
			deltaUps += currentTime - startTime;
			deltaDisplay += currentTime - startTime;

			// Set start time of this loop for use in next cycle
			startTime = currentTime;

			// Render if target time has been reached
			if (deltaFps >= nanoFps) {
				// Render
				
				framecount++;
				deltaFps = 0;
			}

			// Update if target time has been reached
			if (deltaUps >= nanoUps) {
				// Update
				
				updatecount++;
				deltaUps = 0;
			}

			// Update fps and ups if one second has passed
			if (deltaDisplay >= 1000000000) {
				framecount = 0;
				updatecount = 0;
				deltaDisplay = 0;
			}
		}
	}
}

@@ -51,17 +51,18 @@
 Loop %7B%0A%09
+%09
 %0A
-
 %09private
@@ -466,20 +466,32 @@
 oid run(
+Game game
 ) %7B%0A
+%09%09%0A
 %09%09long s
@@ -632,17 +632,37 @@
 nt = 0;%0A
+%09%09%0A%09%09game.init();%0A%09%09
 %0A
-
 %09%09while 
@@ -1067,24 +1067,38 @@
  Render%0A%09%09%09%09
+game.render();
 %0A%09%09%09%09frameco
@@ -1221,16 +1221,30 @@
 ate%0A%09%09%09%09
+game.update();
 %0A%09%09%09%09upd
@@ -1329,16 +1329,16 @@
  passed%0A
-
 %09%09%09if (d
@@ -1362,24 +1362,69 @@
 00000000) %7B%0A
+%09%09%09%09game.updateFps(framecount, updatecount);%0A
 %09%09%09%09framecou

Implement Game interface methods in GameLoop