view 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
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 public long getTime() {
24 return (long) (this.ticks * (1.0f / this.framerate) * 1000f);
25 }
27 public long getResolution() {
28 return 1000000000L;
29 }
31 public float getFrameRate() {
32 return this.framerate;
33 }
35 public float getTimePerFrame() {
36 return (float) (1.0f / this.framerate);
37 }
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 }
52 public void reset() {
53 this.ticks = 0;
54 }
55 }