Search code examples
javaframe-rate

how to set a max fps in java


I was making a 2D game, and I realized that the FPS was around 500, but it kept dropping to 440 and that made horrible lag spikes so I wanted to set a max fps to stop the lag spikes, but I don't know how to do that. Game loop:

    public void run(){
    this.requestFocus();
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000/amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1){
            tick();
            delta--;
        }
        if(running)
            render();
    frames++;

    if(System.currentTimeMillis() - timer > 1000){
        timer += 1000;
        System.out.println("FPS: " + frames);
        frames = 0;
        }
    }
    stop();
}

Solution

  • Just use the same control mechanism you used to control the number of ticks. Try this, and put the max FPS you desire in the 'amountOfRenders' variable.

    public void run(){
        this.requestFocus();
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000/amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        int ticks = 0;
    
        long renderLastTime=System.nanoTime();
        double amtOfRenders = 200.0;//MAX FPS
        double renderNs=1000000000/amtOfRenders;
        double renderDelta = 0;
    
        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >= 1){
                tick();
                ticks++;
                delta--;
            }
    
            now = System.nanoTime();
            renderDelta += (now - renderLastTime) / renderNs;
            renderLastTime = now;
            while(running && renderDelta >= 1){
                render();
                frames++;
                renderDelta--;
            }
    
            if(System.currentTimeMillis() - timer > 1000){
                timer += 1000;
                System.out.println("FPS: " + frames);
                System.out.println("Ticks per second: " + ticks);
                frames = 0;
                ticks = 0;
            }
        }
        stop();
    }