Android Open Source - BulletsForever Tools F P S Counter






From Project

Back to project page BulletsForever.

License

The source code is released under:

GNU General Public License

If you think the Android project BulletsForever listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bulletsforever.bullets;
//  w  w  w.  ja  v a2s . c  o m
import android.os.SystemClock;

/**
 * This is for keeping track of the game's FPS
 * This should be instantiated by GameMain's onCreate()
 * Only a single instance should exist per GameMain instance
 */
public class ToolsFPSCounter {
  
  // Stuff
  private boolean fpsTotalStarted; // To account for inaccurate measurements during initial few frames
  private long fpsStartTime;
  private long fpsTotalTime;
  private int fpsFrameCount;
  private int fpsFrameCountTotal;
  private float fpsCalculated;
  private float fpsCalculatedTotal;
  private String fpsDisplayed;
  private int updateFrequency; // number of frames
  
  public ToolsFPSCounter(int updateFrequency) {
    this.updateFrequency = updateFrequency;
    this.fpsDisplayed = "";
  }
  
  // Call this on every frame
  public void nextFrame() {
    // Calculate FPS based on elapsed time every n frames
    fpsFrameCount++;
    if (fpsFrameCount >= updateFrequency) {
      // Recalculate everything
      long fpsCurrentTime = SystemClock.elapsedRealtime(); 
      fpsCalculated = ((float)(fpsFrameCount) / ((float)(fpsCurrentTime - fpsStartTime) / 1000f));
      if (fpsTotalStarted) {
        fpsFrameCountTotal += fpsFrameCount;
        fpsTotalTime += fpsCurrentTime - fpsStartTime;
        fpsCalculatedTotal = ((float)(fpsFrameCountTotal) / ((float)(fpsTotalTime) / 1000f));
      } else {
        if (fpsCalculated > 0 && fpsCalculated < 99) { // Sanity
          fpsTotalStarted = true;
        }
      }  
      fpsFrameCount = 0;
      fpsStartTime = SystemClock.elapsedRealtime();
      
      // Truncate displayed value to 2 decimal places
      fpsDisplayed = String.format("FPS %.2f/%.2f", fpsCalculated, fpsCalculatedTotal);
    }
  }
  
  // Returns last FPS value
  public String getDisplayedFPS() { return fpsDisplayed; }
  public float getFPS() { return fpsCalculated; }
  public float getTotalFPS() { return fpsCalculatedTotal; }
}




Java Source Code List

com.bulletsforever.bullets.AudioMusicPlayer.java
com.bulletsforever.bullets.AudioSoundPool.java
com.bulletsforever.bullets.DrawBitmapLoader.java
com.bulletsforever.bullets.DrawKeyHandler.java
com.bulletsforever.bullets.DrawObjectBackground.java
com.bulletsforever.bullets.DrawObjectBoss.java
com.bulletsforever.bullets.DrawObjectBullet.java
com.bulletsforever.bullets.DrawObjectDynamicArm.java
com.bulletsforever.bullets.DrawObjectDynamicBoss.java
com.bulletsforever.bullets.DrawObjectHUD.java
com.bulletsforever.bullets.DrawObjectPlayer.java
com.bulletsforever.bullets.DrawObject.java
com.bulletsforever.bullets.DrawRefreshHandler.java
com.bulletsforever.bullets.DrawTouchHandler.java
com.bulletsforever.bullets.DrawWorld.java
com.bulletsforever.bullets.GameMain.java
com.bulletsforever.bullets.GameScore.java
com.bulletsforever.bullets.MenuHome.java
com.bulletsforever.bullets.MenuSettings.java
com.bulletsforever.bullets.Settings.java
com.bulletsforever.bullets.ToolsFPSCounter.java
com.bulletsforever.bullets.ToolsRandomizer.java
com.bulletsforever.bullets.ToolsScoreboard.java
com.bulletsforever.bullets.ToolsTracker.java
com.bulletsforever.bullets.ToolsVibrator.java