rlm@63: package com.aurellem.capture; rlm@63: rlm@63: import com.jme3.system.Timer; rlm@63: rlm@63: /** rlm@63: * RatchetTimer is the same as IsoTimer, except that it will ensure rlm@63: * that the simulation does not proceed any faster than it would had rlm@63: * NanoTimer been used. rlm@63: * rlm@63: * @author normenhansen, Robert McIntyre rlm@63: */ rlm@63: rlm@63: public class RatchetTimer extends Timer{ rlm@69: private long framerate; rlm@63: private int ticks; rlm@63: private long lastTime = 0; rlm@63: rlm@63: public RatchetTimer(float framerate) { rlm@69: this.framerate = (long) framerate; rlm@63: this.ticks = 0; rlm@63: } rlm@64: rlm@64: /** rlm@64: * return time in milliseconds rlm@64: */ rlm@63: public long getTime() { rlm@69: return ticks; rlm@63: } rlm@63: rlm@63: public long getResolution() { rlm@69: return framerate; rlm@63: } rlm@63: rlm@63: public float getFrameRate() { rlm@69: return framerate; rlm@63: } rlm@63: rlm@63: public float getTimePerFrame() { rlm@69: return (float) (1.0f / framerate); rlm@63: } rlm@63: rlm@63: public void update() { rlm@63: long time = System.currentTimeMillis(); rlm@63: long difference = time - lastTime; rlm@63: lastTime = time; rlm@63: if (difference < (1.0f / this.framerate) * 1000.0f) { rlm@63: try { rlm@63: Thread.sleep(difference); rlm@63: } catch (InterruptedException ex) { rlm@63: } rlm@63: } rlm@63: this.ticks++; rlm@63: } rlm@63: rlm@63: public void reset() { rlm@63: this.ticks = 0; rlm@63: } rlm@63: } rlm@63: