在 Java 游戏中计算 FPS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20769767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:12:24  来源:igfitidea点击:

Calculate FPS in Java Game

javaframe-rate

提问by SirTrashyton

Yesterday I wrote a Thread addressing how my game loop ran (in java) and how it works.

昨天我写了一个线程来解决我的游戏循环如何运行(在 Java 中)以及它是如何工作的。

My game loop works completely, and I know why, but now I just wan't to know how to calculate FPS (Frames Per Second) and print it out every second.

我的游戏循环完全正常,我知道为什么,但现在我只是不知道如何计算 FPS(每秒帧数)并每秒打印出来。

I got a response yesterday about this, but he/she explained it in words and I couldn't understand it.

我昨天收到了关于这个的回复,但他/她用语言解释了,我无法理解。

If anyone could help me (with a code example? :D) that would be great.

如果有人可以帮助我(使用代码示例?:D)那就太好了。

Here is my game loop:

这是我的游戏循环:

        while (running) {

        start = System.nanoTime();

        update();
        draw();
        drawToScreen();

        elapsed = System.nanoTime() - start;

        wait = targetTime - elapsed / 1000000;
        if (wait < 0) {
            wait = 5;
        }

        try {
            Thread.sleep(wait);
        } catch (Exception e) {
            Game.logger.log("ERROR! Printing Stacktrace...");

            e.printStackTrace();
        }

    }

ALSO:

还:

In my JFrame when ever I call setName(string) it never works/updates on the Frame - Link me to a thread?

在我的 JFrame 中,当我调用 setName(string) 它永远不会在框架上工作/更新 - 将我链接到一个线程?

回答by Trojan

You can combine a simple counter and Timer.scheduleAtFixedRatefor this.

您可以为此组合一个简单的计数器和Timer.scheduleAtFixedRate

Disclaimer: I don't know if this is the best method; it's just easy.

免责声明:我不知道这是否是最好的方法;这很容易。

int totalFrameCount = 0;
TimerTask updateFPS = new TimerTask() {
    public void run() {
        // display current totalFrameCount - previous,
        // OR
        // display current totalFrameCount, then set
        totalFrameCount = 0;
    }
}

Timer t = new Timer();
t.scheduleAtFixedRate(updateFPS, 1000, 1000);

while (running) {
    // your code
    totalFrameCount++;
}

回答by Justin

The easiest way to do this is to keep a variable whatTheLastTimeWasstored and doing this where you want to check your frame rate:

最简单的方法是保存一个变量whatTheLastTimeWas并在您想要检查帧速率的地方执行此操作:

double fps = 1000000.0 / (lastTime - (lastTime = System.nanoTime())); //This way, lastTime is assigned and used at the same time.

Alternatively, you can make a FPS counter like so:

或者,您可以像这样制作 FPS 计数器:

class FPSCounter extends Thread{
    private long lastTime;
    private double fps; //could be int or long for integer values

    public void run(){
        while (true){//lazy me, add a condition for an finishable thread
            lastTime = System.nanoTime();
            try{
                Thread.sleep(1000); // longer than one frame
            }
            catch (InterruptedException e){

            }
            fps = 1000000000.0 / (System.nanoTime() - lastTime); //one second(nano) divided by amount of time it takes for one frame to finish
            lastTime = System.nanoTime();
        }
    }
    public double fps(){
        return fps;
    } 
}

Then in your game, have an instance of FPSCounterand call nameOfInstance.interrupt();when one frame is finished.

然后在你的游戏中,当一帧完成时有一个FPSCounter和调用的实例nameOfInstance.interrupt();