view src/com/aurellem/capture/RatchetTimer.java @ 73:877ae4b2993c tip

merge laptop changes.
author Robert McIntyre <rlm@mit.edu>
date Mon, 10 Mar 2014 18:58:08 -0400
parents ddb300c5335f
children
line wrap: on
line source
1 package com.aurellem.capture;
3 import com.jme3.system.Timer;
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 */
13 public class RatchetTimer extends Timer{
14 private long framerate;
15 private int ticks;
16 private long lastTime = 0;
18 public RatchetTimer(float framerate) {
19 this.framerate = (long) framerate;
20 this.ticks = 0;
21 }
23 /**
24 * return time in milliseconds
25 */
26 public long getTime() {
27 return ticks;
28 }
30 public long getResolution() {
31 return framerate;
32 }
34 public float getFrameRate() {
35 return framerate;
36 }
38 public float getTimePerFrame() {
39 return (float) (1.0f / framerate);
40 }
42 public void update() {
43 long time = System.currentTimeMillis();
44 long difference = time - lastTime;
45 lastTime = time;
46 if (difference < (1.0f / this.framerate) * 1000.0f) {
47 try {
48 Thread.sleep(difference);
49 } catch (InterruptedException ex) {
50 }
51 }
52 this.ticks++;
53 }
55 public void reset() {
56 this.ticks = 0;
57 }
58 }