Mercurial > jmeCapture
annotate 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 |
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@69 | 14 private long 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@69 | 19 this.framerate = (long) framerate; |
rlm@63 | 20 this.ticks = 0; |
rlm@63 | 21 } |
rlm@64 | 22 |
rlm@64 | 23 /** |
rlm@64 | 24 * return time in milliseconds |
rlm@64 | 25 */ |
rlm@63 | 26 public long getTime() { |
rlm@69 | 27 return ticks; |
rlm@63 | 28 } |
rlm@63 | 29 |
rlm@63 | 30 public long getResolution() { |
rlm@69 | 31 return framerate; |
rlm@63 | 32 } |
rlm@63 | 33 |
rlm@63 | 34 public float getFrameRate() { |
rlm@69 | 35 return framerate; |
rlm@63 | 36 } |
rlm@63 | 37 |
rlm@63 | 38 public float getTimePerFrame() { |
rlm@69 | 39 return (float) (1.0f / framerate); |
rlm@63 | 40 } |
rlm@63 | 41 |
rlm@63 | 42 public void update() { |
rlm@63 | 43 long time = System.currentTimeMillis(); |
rlm@63 | 44 long difference = time - lastTime; |
rlm@63 | 45 lastTime = time; |
rlm@63 | 46 if (difference < (1.0f / this.framerate) * 1000.0f) { |
rlm@63 | 47 try { |
rlm@63 | 48 Thread.sleep(difference); |
rlm@63 | 49 } catch (InterruptedException ex) { |
rlm@63 | 50 } |
rlm@63 | 51 } |
rlm@63 | 52 this.ticks++; |
rlm@63 | 53 } |
rlm@63 | 54 |
rlm@63 | 55 public void reset() { |
rlm@63 | 56 this.ticks = 0; |
rlm@63 | 57 } |
rlm@63 | 58 } |
rlm@63 | 59 |