diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/com/aurellem/capture/RatchetTimer.java	Sun Jan 15 04:10:37 2012 -0700
     1.3 @@ -0,0 +1,56 @@
     1.4 +package com.aurellem.capture;
     1.5 +
     1.6 +import com.jme3.system.Timer;
     1.7 +
     1.8 +/**
     1.9 + * RatchetTimer is the same as IsoTimer, except that it will ensure
    1.10 + * that the simulation does not proceed any faster than it would had
    1.11 + * NanoTimer been used.
    1.12 + * 
    1.13 + * @author normenhansen, Robert McIntyre
    1.14 + */
    1.15 +
    1.16 +public class RatchetTimer extends Timer{
    1.17 +	private float framerate;
    1.18 +    private int ticks;
    1.19 +    private long lastTime = 0;
    1.20 +
    1.21 +    public RatchetTimer(float framerate) {
    1.22 +        this.framerate = framerate;
    1.23 +        this.ticks = 0;
    1.24 +    }
    1.25 +
    1.26 +    public long getTime() {
    1.27 +        return (long) (this.ticks * (1.0f / this.framerate) * 1000f);
    1.28 +    }
    1.29 +
    1.30 +    public long getResolution() {
    1.31 +        return 1000000000L;
    1.32 +    }
    1.33 +
    1.34 +    public float getFrameRate() {
    1.35 +        return this.framerate;
    1.36 +    }
    1.37 +
    1.38 +    public float getTimePerFrame() {
    1.39 +        return (float) (1.0f / this.framerate);
    1.40 +    }
    1.41 +
    1.42 +    public void update() {
    1.43 +        long time = System.currentTimeMillis();
    1.44 +        long difference = time - lastTime;
    1.45 +        lastTime = time;
    1.46 +        if (difference < (1.0f / this.framerate) * 1000.0f) {
    1.47 +            try {
    1.48 +                Thread.sleep(difference);
    1.49 +            } catch (InterruptedException ex) {
    1.50 +            }
    1.51 +        }
    1.52 +        this.ticks++;
    1.53 +    }
    1.54 +
    1.55 +    public void reset() {
    1.56 +        this.ticks = 0;
    1.57 +    }
    1.58 +}
    1.59 +