0

I'm trying to create a game loop in java which has a frame cap which can be set while the game is running. The problem im having is I have a render() function and a update() function. Just setting frame cap for both render() and update() means that the speed of the game logic will change when you change the frame cap. Any idea how to have a frame cap which can be set in game while not affecting the speed of the game logic (update())?

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Try to use a timer that periodically triggers events (frames), instead of a loop. This turns the code inside out. This is how swing of JavaFX normally works. – Joop Eggen Aug 18 '21 at 09:20
  • Yes it is graphical and im currently using swing. –  Aug 18 '21 at 10:27
  • Two separate loops in two separate threads, one for the update and one for the render. You can adjust the delay on the render loop. – Gilbert Le Blanc Aug 18 '21 at 10:32
  • uffff i have worked a lot with that in the past, i need to refresh my brain in order to show a full answer, in the meanwhile, you may take a look at my project (a single java file) https://github.com/vituchon/sleepy-screensaver/blob/master/main/Main.java that deals with time controlling in a way (there are many ways to deal with updating intervals). – Victor Aug 18 '21 at 15:54

1 Answers1

1

As stated in the comments: You can create two threads one of which is responsible for updating and the other is responsible for rendering.

Try to think of an architecture that suits your needs and your game. You can try something like:

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Runner extends JPanel {

    private static final long serialVersionUID = -5029528072437981456L;

    private JFrame window;
    
    private Renderer renderer;

    public Runner() {
        setPreferredSize(new Dimension(WindowData.WIDTH, WindowData.HEIGHT));
        setFocusable(true);
        requestFocus();
        
        window = new JFrame(WindowData.TITLE);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(this);
        window.setResizable(false);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        
        renderer = new Renderer(getGraphics());
        renderer.start();
    }
    
    public static void main(String[] args) {
        new Runner();
    }
}

import java.awt.Graphics;

public class Renderer implements Runnable {
    
    private Graphics context;

    private Thread thread;
    private boolean running;

    public Renderer(Graphics context) {
        this.context = context;

        thread = new Thread(this, "Renderer");
        running = false;
    }

    public void start() {
        if (running)
            return;
        running = true;
        thread.start();
    }

    public void render() {
        context.clearRect(0, 0, WindowData.WIDTH, WindowData.HEIGHT);
        context.fillRect(50, 50, 100, 100);
    }

    public void run() {
        while (running) {
            render();

            // ** DO YOUR TIME CONTROL HERE ** \\
        }
    }
}

This code will actually lead to serious performance issues because you are not in control over the rendering ( repaint() ) time.

But this is just a demo to show you how you can use different threads. Do your own architecture.

InSaNiTy
  • 150
  • 1
  • 9
  • Thanks for the response. I'm not using repaint() but rather my own renderer on top of swing using buffer strategys and other things so hopefully that shouldn't matter. –  Aug 18 '21 at 12:08
  • Yes, it does not matter at all. Using separate threads is a very common feature used in many AAA game engines. For example, unity does separate the physics processing from other things. And it does not matter if you are using canvas or JPanel for rendering. – InSaNiTy Aug 18 '21 at 12:26
  • Quick question, if my game is rendering at 144 frames a seconds how fast should it be updating? Also is should be updating the same speed at other render framerates. –  Aug 19 '21 at 07:26
  • There is no specific amount of updates you have to do. It is really dependent. Do your math, you want your game to be responsive? go on 60 updates per sec, or 144 like your frames. The updates are going to determine your game speed. – InSaNiTy Aug 19 '21 at 13:25