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