📜  Java 从线程计算 fps - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:10.689000             🧑  作者: Mango

代码示例1
public int fps;
    private int frames;
    private long fpsTimer;
    public double deltaTime;
    private double lastDeltaTime;
    public boolean calculatePerformance;

    public Thread thread = new Thread() {
      public void run() {
          while(running) {

              // Update, Render, etc.

              if(calculatePerformance) calculatePerformance();
          }
      }
    };

    private final void calculatePerformance() {
        frames++; // Add 1 to frames each update.
        if(System.currentTimeMillis() - fpsTimer >= 1000L) { // If 1 second has passed.
            fpsTimer = System.currentTimeMillis(); // Update timer to current time.
            fps = frames; // Set fps to frames
            frames = 0; // Reset frames
        }
        deltaTime = (System.nanoTime() - lastDeltaTime) / 1_000_000_000.0D; // Set delta time from last delta time
        lastDeltaTime = System.nanoTime(); // Set last deltaTime to current delta Time
    }