Help on understanding the game loop

Hi everyone,
I’ve been learning java for more than 2 months now, and i have managed to do some “advanced” things (simple text editor, simple calculator, simple paint application) but when it turns out to games i can almost do everything by my own except the game loop i always needs to check out some other people codes to “manage” the solution, any one can help please ?
thank you

The game loop is remarkably simple once you “get” it.


initialize();
int delta = 0;
while(true) {
    delta = calculateDelta();
    update(delta);
    render();
    sync();
}
destroy();

So this is the basic structure of a simple game. So now I’ll go through it step by step.
Before you enter into the game loop, you want to initialize everything. This means create your window, load your graphics, sounds, levels etc.
Then we enter into the loop which will continue to run until the user presses exit. NB, one “frame,” is one iteration of this loop. The first thing you do is calculate the delta.
Delta in sciences means “change in” and in this context we mean change in time, as in the difference between the time at the start of the previous frame and the start of this frame. The reason we use this is to make sure the game runs at the same rate no matter how long individual frames take to “do.” This will become clearer later on if you don’t understand.
Next we “update.” Call this what you want all it means is “do game logic.” In this part, you calculate new positions of objects that are moving, you calculate what has hit what (aka collisions detection), you calculate who’s dead and who killed them etc. This is where delta comes in. If you have a ball moving at 5 pixels per second, you need to know how many seconds have passed by to work out how many pixels it has actually moved. That’s basics right? Yet it always takes a while to sink in. The last thing you have to do here is see if the player wants to exit and break the loop if so.
Then we “render”. In this bit you take everything you calculated in update (who is where and how much blood is around) and draw it in your window so that the player(s) can actually see whats going on.This bit is really as complex as you need it to be and can be anything under the sun. (Sun as in Sun Microsystems)
The last bit of the loop is syncing. It is really necessary but makes sure your game doesn’t consume all the cpu time and battery power. Say you want your game to run at 60 frames per second. What happens if an iteration of the loop takes less time than 1 / 60 seconds? You need to wait a little which is all the sync does. If your using LWJGL, then there is a special little function that does this for you.
And after all this you tear down your program and release any resources you may have had.

P.S. There’s nothing more “advanced” than “simple” programs. (I kid, I know what you mean).

That is a terrible loop.

Indeed. How is your example going to help if it uses magical methods that don’t exist? Not to mention that you don’t even show how to use delta.

I’m sorry, I pressed enter by mistake (actually tab and enter?) and was editing it when you posted.

quew8 was more helpful than either of you, he gave the basic structure of a game loop and described it.

I’d suggest taking a look at Eli’s article on game loops to see the different kinds and some ways to implement them. I also wouldn’t use while(true), it’s probably best to have a boolean that you can change in order to exit the loop and then exit the game.

Fixed timestep loop (mostly taken from Eli’s article):

http://pastebin.java-gaming.org/bf3455c3455

EDIT: Accidentally left Display.isCloseRequested() in there. Replace it with a running boolean if you aren’t using LWJGL.

[icode]fps[/icode] is the frames per second for the last frame, in case you want an fps counter.
[icode]capped[/icode] if set to true, caps the framerate TARGET_FPS. Otherwise it maxes the framerate. It always updates at the rate of GAME_HERTZ per second (except when experiencing lag).

If you read the post before you, we were responding to the unedited version of quew8’s first post, which was incomplete.

So I was. My mistake.

i’ll take a look to all of them and keep you in touch in case i had a problem
thank you very much

thanx dude
that seems very clear and funny too ::slight_smile:
thank you