2

I've read some stuff about game loops, but I can't seem to find out what the objectively best is, though I feel like there should be such a thing as a best game loop.

I'm going to semi-answer my own question here by going over some examples. The game loop I'm looking for should have the most effective way of updating the gamestate and calculating, while listening for input. When things get language specific, assume I'm using Java.

So, the most basic game loop seems to be this:

while(running){
    update();
    render();
}

However, this will make the program work differently on different computers, something not often desired. So to fix this I usually see one of the following two solutions:

//Fix the timestep
double step = 1/60;
double time;

while(running){
    update(step);
    render();
    try {
        Thread.sleep(Math.max(0,step + (time - (time = System.currentTimeMillis()) / 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Or

//Vary the timestep
double time;

while(running){
    update(-time + (time = System.currentTimeMillis()) / 1000));
    render();
}

I tend to go with the second option, but then we are faced with an issue: we are rendering a lot more often than needed. So what I've done is kept this loop, but only render if timeSinceLastUpdate >= 1/60.

However, optimally we would rather have the rendering and the updating process completely freed from each other, so I've thought about multithreading, where I have one thread that gets an optimal amount of calculations per second (determined by how many the PC can handle, at how many calculations per second calculations are accurate, and others), and one thread that's locked to a certain amount of frames per second that displays the game based on the last gamestate. But this creates another issue: the data that describes a gamestate is under constant modification. So I save the last gamestate, construct another, and then make that the saved gamestate when it's done and construct another again, and so forth. However, this means I'm constantly creating new data rather than changing existing data, which may be more work for the computer.

So what's the most effective kind of gameloop? I'm mainly interested in whether computing a completely new gamestate rather than changing the gamestate will make a big difference

Lara
  • 2,594
  • 4
  • 24
  • 36
  • 1
    There is no perfect answer. You should do what you believe is the simplest and that is more likely to work. Note: the GFX card will be work in parallel anyway. If you need to use more threads, I suggest using one core thread to co-ordinate them. – Peter Lawrey Jan 25 '15 at 18:25
  • questions like these always fail to consider that "best" can mean very different, even diametrically opposed things (ie fastest for single vs multicore cpu vs least memory usage vs easiest to write/maintain etc). If you really want to know: measure different implementations. – CodeSmile Jan 25 '15 at 18:58
  • One thing: **never** printStackTrace() from an exception. – usr-local-ΕΨΗΕΛΩΝ Jan 25 '15 at 19:05
  • I have tried few gameloops in Java, my answer shows best one I've found, it gives a smooth animation steps without shuttering. http://stackoverflow.com/questions/26073621/java-simple-fps-animationhow-can-i-work-with/26073828#26073828 But I have not run it with extensive GPU or multicore environments so results may vary. – Whome Jan 25 '15 at 20:47

0 Answers0