comparison src/com/aurellem/capture/RatchetTimer.java @ 63:df4a2d12ea13

added new timer for debugging purposes
author Robert McIntyre <rlm@mit.edu>
date Sun, 15 Jan 2012 04:10:37 -0700
parents
children 155c70b7e6de
comparison
equal deleted inserted replaced
62:f5e52169f056 63:df4a2d12ea13
1 package com.aurellem.capture;
2
3 import com.jme3.system.Timer;
4
5 /**
6 * RatchetTimer is the same as IsoTimer, except that it will ensure
7 * that the simulation does not proceed any faster than it would had
8 * NanoTimer been used.
9 *
10 * @author normenhansen, Robert McIntyre
11 */
12
13 public class RatchetTimer extends Timer{
14 private float framerate;
15 private int ticks;
16 private long lastTime = 0;
17
18 public RatchetTimer(float framerate) {
19 this.framerate = framerate;
20 this.ticks = 0;
21 }
22
23 public long getTime() {
24 return (long) (this.ticks * (1.0f / this.framerate) * 1000f);
25 }
26
27 public long getResolution() {
28 return 1000000000L;
29 }
30
31 public float getFrameRate() {
32 return this.framerate;
33 }
34
35 public float getTimePerFrame() {
36 return (float) (1.0f / this.framerate);
37 }
38
39 public void update() {
40 long time = System.currentTimeMillis();
41 long difference = time - lastTime;
42 lastTime = time;
43 if (difference < (1.0f / this.framerate) * 1000.0f) {
44 try {
45 Thread.sleep(difference);
46 } catch (InterruptedException ex) {
47 }
48 }
49 this.ticks++;
50 }
51
52 public void reset() {
53 this.ticks = 0;
54 }
55 }
56