aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/olacraft/game/Timer.java
blob: 1c0aaee6d2b79dcd0d6052a4477f9b60b6341070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ru.olamedia.olacraft.game;

/**
 * For frame-rate independent movement
 * 
 * @author Oskar Veerhoek
 */
public class Timer {
	private long lastTime; // nanoseconds
	private double elapsedTime;
	private float fps;
	private int fpsCounter = 0;
	private long lastFPS;
	private float avgSeconds = 3;

	/**
	 * @return the fps
	 */
	public float getFps() {
		return fps;
	}

	/**
	 * @return the avgFps
	 */
	public float getAvgFps() {
		return avgFps;
	}

	private float avgFps;

	/**
	 * Creates a timer.
	 */
	public Timer() {
		fps = 0;
	}

	/**
	 * Initializes the timer. Call this just before entering the game loop.
	 */
	public void initialize() {
		lastTime = System.nanoTime();
	}

	/**
	 * @return the elapsed time since the the next to last update call
	 */
	public double getElapsedTime() {
		return elapsedTime;
	}

	/**
	 * Updates the timer. Call this once every iteration of the game loop.
	 * 
	 * @return the elapsed time in milliseconds
	 */
	public double update() {
		if (lastTime == 0) {
			lastTime = System.nanoTime();
			return 0;
		} else {
			long elapsedTime = System.nanoTime() - lastTime;
			updateFps(elapsedTime);
			lastTime = System.nanoTime();
			this.elapsedTime = elapsedTime / (double) 1000000;
			return this.elapsedTime;
		}
	}

	public void updateFps(long elapsedTime) {
		if (elapsedTime > 0) {
			float ms = (float) (elapsedTime / 1000000);
			if (ms > 0) {
				fps = (float) (1000 / ms);
			}
		}
		fpsCounter++;
		if (lastFPS == 0) {
			lastFPS = System.nanoTime();
		} else {
			double elapsedFPS = (System.nanoTime() - lastFPS) / (double) 1000000;
			if (elapsedFPS > 1000 * avgSeconds) {
				avgFps = fpsCounter / avgSeconds;
				fpsCounter = 0;
				lastFPS = System.nanoTime();
			}
		}

		// if (elapsedTime > 0) {
		// fps = (float) (1000 / (elapsedTime / 1000000));
		// if (avgFps == 0) {
		// avgFps = fps;
		// } else {
		// avgFps = avgFps + (fps - avgFps) / 1000;
		// }
		// }
	}
}