framecounter.java

来自「可在索爱K700C上运行的3D例程」· Java 代码 · 共 184 行

JAVA
184
字号
/**
 * 
 * COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004. 
 * 
 * The software is the copyrighted work of Sony Ericsson Mobile Communications AB. 
 * The use of the software is subject to the terms of the end-user license agreement which 
 * accompanies or is included with the software. The software is provided "as is" and Sony Ericsson 
 * specifically disclaim any warranty or condition whatsoever regarding merchantability or fitness for 
 * a specific purpose, title or non-infringement. No warranty of any kind is made in relation to the condition, 
 * suitability, availability, accuracy, reliability, merchantability and/or non-infringement of the software provided herein
 *
 */
package com.sonyericsson.javatest.mobile3d;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * 
 * Class that provides general purpose framerate counter for applications that use Graphics.
 * 
 *  The method update() is called on every frame redraw
 *  
 */
public class FrameCounter {

    private float averageFps = 0;

    private int fpsDisplay = 0;

    private int framesPerSecond = 0;

    private String labelFps = "FPS/Avg";

    private long lastTimeCheck;

    private boolean showLabelFlag = true;

    /**
     * Actually process and display results every n frame if set to 1, the result will be displayed on every frame
     */
    public int updateEveryFrame = 20;

    private int collectAverages = 100; // how many averages to collect

    private long currentTime;

    private long frameTimestamp;

    private int num = 0; //Number of items *actually* in the array

    private long savedTime;

    private int[] savedTimesArray = new int[collectAverages]; //Array containing all of the #s
    private String averageFpsString;
    /**
     * Calculate average FPS value for all stored FPS
     */
    public float calculateAverageFps() {
        int runTot = 0;
        for (int x = 0; x < num; x++) { //Go through the entire array
            if (savedTimesArray[x] != 0) {
                runTot = runTot + savedTimesArray[x]; //Increment the running total
            }
        }
        averageFps = (float) runTot / num; //calculate the average
        
        //make good-looking FPS string with two digits after point
        averageFpsString = Float.toString(averageFps);
        int pPos = averageFpsString.indexOf(".");
        averageFpsString = averageFpsString.substring(0, pPos + 2);
        
        return averageFps; //return the average
    }

    /**
     * Draw FPS counter on graphics
     * 
     * @param g
     *            Graphics instance
     */
    public void drawFps(Graphics g) {
        //		Specify a font face, style and size
        Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        g.setFont(font);
        g.setColor(0xFFFFFF);

        //something is wrong?
        if (framesPerSecond == 0) {
            return;
        }

        //		Position the text in the image
        if (showLabelFlag) {
            try {
                g.drawString(labelFps, g.getClipWidth() - 1, (g.getClipHeight()) - (font.getHeight()), Graphics.BOTTOM
                        | Graphics.RIGHT);
            } catch (Exception e) {
                System.err.println("drawstring error");
            }
        }
        //graw string in the image
        String message = String.valueOf(framesPerSecond) + "/" + averageFpsString;
        g.drawString(message, g.getClipWidth() - 1, g.getClipHeight() - 1, Graphics.BOTTOM | Graphics.RIGHT);
    }

    /**
     * @return Returns the averageFps.
     */
    public float getAverageFps() {
        return averageFps;
    }

    /**
     * Restore counting. substract sleep time from continious time
     *  
     */
    public void restore() {
        lastTimeCheck = System.currentTimeMillis() - savedTime;
    }

    /**
     * @param averageFps
     *            The averageFps to set.
     */
    public void setAverageFps(float averageFps) {
        this.averageFps = averageFps;
    }

    /**
     * Save current time to suspend the counter Used when application need to tmeporarily suspend counting.
     *  
     */
    public void suspend() {
        savedTime = System.currentTimeMillis() - lastTimeCheck;
    }

    /**
     * Main method that does actual calculation and update. Should be called every time the frame is drawn.
     *  
     */
    public void update() {

        long currTime = System.currentTimeMillis();
        framesPerSecond = (int) (1000 / (currTime - lastTimeCheck));

        lastTimeCheck = currTime;

        if (num > 0) {
            averageFps = calculateAverageFps();
        } else
            averageFps = 0;

        if (num >= collectAverages) {
            num = 0;
        }
        num++;
        savedTimesArray[num] = framesPerSecond;
    }
    /**
     * @return Returns the averageFpsString.
     */
    public String getAverageFpsString() {
        return averageFpsString;
    }
    /**
     * @param averageFpsString The averageFpsString to set.
     */
    public void setAverageFpsString(String averageFpsString) {
        this.averageFpsString = averageFpsString;
    }
    /**
     * @param showLabelFlag The showLabelFlag to set.
     */
    public void setShowLabelFlag(boolean showLabelFlag) {
        this.showLabelFlag = showLabelFlag;
    }
    /**
     * @return Returns the showLabelFlag.
     */
    public boolean isShowLabelFlag() {
        return showLabelFlag;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?