view src/com/aurellem/capture/RatchetTimer.java @ 64:155c70b7e6de

removed cursing
author Robert McIntyre <rlm@mit.edu>
date Fri, 10 Feb 2012 04:07:05 -0700
parents df4a2d12ea13
children ddb300c5335f
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 float framerate;
15 private int ticks;
16 private long lastTime = 0;
18 public RatchetTimer(float framerate) {
19 this.framerate = framerate;
20 this.ticks = 0;
21 }
23 /**
24 * return time in milliseconds
25 */
26 public long getTime() {
27 return (long) (this.ticks * (1.0f / this.framerate) * 1000f);
28 }
30 public long getResolution() {
31 return 1000000000L;
32 }
34 public float getFrameRate() {
35 return this.framerate;
36 }
38 public float getTimePerFrame() {
39 return (float) (1.0f / this.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 }