How do I cap my framerate at 60 fps in Java?

后端 未结 10 1727
离开以前
离开以前 2021-02-07 08:01

I am writting a simple game, and I want to cap my framerate at 60 fps without making the loop eat my cpu. How would I do this?

相关标签:
10条回答
  • 2021-02-07 08:54

    The simple answer is to set a java.util.Timer to fire every 17 ms and do your work in the timer event.

    0 讨论(0)
  • 2021-02-07 08:54

    Here's how I did it in C++... I'm sure you can adapt it.

    void capFrameRate(double fps) {
        static double start = 0, diff, wait;
        wait = 1 / fps;
        diff = glfwGetTime() - start;
        if (diff < wait) {
            glfwSleep(wait - diff);
        }
        start = glfwGetTime();
    }
    

    Just call it with capFrameRate(60) once per loop. It will sleep, so it doesn't waste precious cycles. glfwGetTime() returns the time since the program started in seconds... I'm sure you can find an equivalent in Java somewhere.

    0 讨论(0)
  • 2021-02-07 08:54

    What I have done is to just continue to loop through, and keep track of when I last did an animation. If it has been at least 17 ms then go through the animation sequence.

    This way I could check for any user inputs, and turn musical notes on/off as needed.

    But, this was in a program to help teach music to children and my application was hogging up the computer in that it was fullscreen, so it didn't play well with others.

    0 讨论(0)
  • 2021-02-07 08:59

    I took the Game Loop Article that @cherouvim posted, and I took the "Best" strategy and attempted to rewrite it for a java Runnable, seems to be working for me

    double interpolation = 0;
    final int TICKS_PER_SECOND = 25;
    final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    final int MAX_FRAMESKIP = 5;
    
    @Override
    public void run() {
        double next_game_tick = System.currentTimeMillis();
        int loops;
    
        while (true) {
            loops = 0;
            while (System.currentTimeMillis() > next_game_tick
                    && loops < MAX_FRAMESKIP) {
    
                update_game();
    
                next_game_tick += SKIP_TICKS;
                loops++;
            }
    
            interpolation = (System.currentTimeMillis() + SKIP_TICKS - next_game_tick
                    / (double) SKIP_TICKS);
            display_game(interpolation);
        }
    }
    
    0 讨论(0)
提交回复
热议问题