0

我在我的游戏中添加了一个 fps 计数器,在我尝试添加它之前它运行良好,但现在它是一个白框,在控制台中它以惊人的速度打印 0 fps:

private void start() {
    if (running) {
        return;
    }
    running = true;

    thread = new Thread(this);
    thread.start();
    System.out.println("Error Free!");
}

public void stop() {
    if (!running) {
        return;
    }
    running = false;
    try {
        thread.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

public void run() {
    int frames = 0;
    double UnproccesedSeconds = 0;
    long PreviousTime = System.nanoTime();
    double SecondsPerTick = 1 / 60.0;
    int TickCount = 0;
    boolean Ticked = false;

    while (running) {
        long CurrentTime = System.nanoTime();
        long PassedTime = CurrentTime - PreviousTime;
        PreviousTime = CurrentTime;
        UnproccesedSeconds += PassedTime / 1000000000.0;

        while (UnproccesedSeconds < SecondsPerTick) {
            tick();
            UnproccesedSeconds -= SecondsPerTick;
            Ticked = true;
            TickCount++;
            if (TickCount % 60 == 0) {
                System.out.println(frames + "fps");
                PreviousTime += 1000;
                frames = 0;
            }
        }

        if (Ticked) {
            render();
            frames++;
        }
        render();
        frames++;
    }
}

private void tick() {
}

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Screen.render();

    for (int i = 0; i < WIDTH * LENGTH; i++) {
        PIXELS[i] = Screen.PIXELS[i];
    }

}

我查了一下,我找不到问题所在;如果有人能回复我,那就太棒了!

4

2 回答 2

1

线程的优先级可能会干扰您的游戏输出管道。也就是说,计数器消耗了太多的运行时间,以至于引擎管道无法再跟上,这可能就是 FPS 报告为 0 的原因。

您可以尝试降低线程的优先级(当您创建它时)和/或在计算循环中的某个位置放置一个 Thread.yield。这确实意味着 FPS 计算将被分流到后面,但会允许游戏引擎正常运行(至少比以前更快)

于 2012-07-14T05:49:23.407 回答
0

如果有人在这里迷路了,那是错的=)

这条线

while (UnproccesedSeconds < SecondsPerTick) {}

需要是

while (UnproccesedSeconds > SecondsPerTick) {}
于 2012-10-07T21:38:50.563 回答