All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sim.display.RateAdjuster Maven / Gradle / Ivy

Go to download

MASON is a fast discrete-event multiagent simulation library core in Java, designed to be the foundation for large custom-purpose Java simulations, and also to provide more than enough functionality for many lightweight simulation needs. MASON contains both a model library and an optional suite of visualization tools in 2D and 3D.

The newest version!
/*
  Copyright 2006 by Sean Luke and George Mason University
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
*/

package sim.display;
import sim.engine.*;

/** A simple class which maintains a frame rate fixed to no more than a given number of ticks per second.
    Typically this is used in games or visual demonstrations, where it is best to add it not to the Schedule
    but to the GUIState's minischedule.  For example, you could do this when start() and load(...) are called:
        
    

myGUIState.scheduleRepeatingImmediatelyAfter(new RateAdjuster(60)); // I want 60 frames per second maximum */ public class RateAdjuster implements Steppable { private static final long serialVersionUID = 1; long initialTime; long totalTics; boolean started = false; double rate; static final int MAX_POLL_ITERATIONS = 3; // so we don't go into a Zeno's paradox public RateAdjuster(double targetRate) { rate = targetRate; } public void step(SimState state) { if (!started) { initialTime = System.currentTimeMillis(); started = true; } else { long currentTime = System.currentTimeMillis(); long time = currentTime - initialTime; totalTics++; long expectedTime = (long)(totalTics / rate * 1000); int count = 0; while (time < expectedTime && count++ < MAX_POLL_ITERATIONS ) // too fast, need to slow down { try { Thread.currentThread().sleep(expectedTime - time); } catch (InterruptedException e) { } currentTime = System.currentTimeMillis(); time = currentTime -initialTime; } // At this point time >= expectedTime. So reset the clock. initialTime = currentTime; totalTics = 0; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy