package code.kalter.longflight; /** * Ограничитель кадров * * @author KalterFive */ public class FPS { private static FPS instance; public static FPS getInstance(int max) { if (instance == null) { instance = new FPS(max); } return instance; } private long fpsTime; private long fpsDt; private int max; // кол-во кадров в секунду private FPS(int max) { this.max = max; this.fpsTime = System.currentTimeMillis(); this.fpsDt = 1; } // обновляет необходимые данные и возвращает текущий fps public long process() { long dt; if ((dt = System.currentTimeMillis() - fpsTime) != 0) { fpsDt = dt; } fpsTime = System.currentTimeMillis(); return 1000 / fpsDt; } // останавливает программу, пока скорость смены кадров больше заданной public void max() { long dt = System.currentTimeMillis() - fpsTime; while (dt < max) { dt = System.currentTimeMillis() - fpsTime; } } }