Hi and welcome to our next blog post about the more slightly technical side of game development. In today’s post, we’ll cover almost the whole game because we’ll be looking at the game loop: what it is and how it looks.
Game loop is the main part of a game program, where the game spend most of it's time. It is a basic logic which manage the whole run of a game. Of course there are step which are taken before and after the game loop but these are not important for this article. The basic architecture of a game loop may have many forms, but the the most simple and often sufficient looks like this:
That’s it. But don’t forget, under each of these parts there may be thousands or even millions of lines of source code. So lets look at these parts in more detail.
At the beginning of a game loop it’s necessary to handle all the inputs which may affect the state of the game. Typically this input comes from the player’s devices like a keyboard, mouse or controller. But in a multiplayer game, input may come from the server as well. In VR games input may be derived from the position or movement of the VR controller. There are also other sources for input. This is the simplest function of a game loop but no less important.
The second part is simulation of the game world. Each pass of a game loop moves game time forward and its task, at this point, is to compute how the game world is altered during gameplay. Typically, this means: go over the game’s objects, compute their mutual interaction ( i.e. a bullet hitting a wall) and also compute interactions with handled inputs from the previous part of game loop. ( i.e. player press key to move forward) After the computation the game stores this new world state and goes to the third part.
The last part is rendering. In this part, we render the current state of the world to output devices. In modern games, this part of the process is the most performance consuming. But in games focusing on advanced simulation, the simulation part may be more demanding.
The biggest disadvantage of the mentioned model is that it doesn’t use modern hardware in an optimal way. That’s to say, the usage of multiple processor threads. The most straightforward multi-thread model is to compute inputs and simulation on one thread and render on another simultaneously, or in parallel. But there’s a problem. If we simultaneously change the world state in the simulation phase of game loop and render the world state in the rendering phase, then we will often render parts of the world before computing the next state. So the game loop process will get ahead of itself, and we don’t want that.
One of the solutions would be to wait until the simulation thread is finished before moving on to the render thread but that would bring us back to the original model and with a useless thread. The better solution is to compute only the simulation in the first iteration, and to store the results. Then in the second iteration, we can compute both simulation and rendering concurrently, because the rendering phase uses data computed in the previous iteration and does not collide with the running simulation thread. The only disadvantage is that the rendering is one iteration delayed, but player’s wont notice in most situations.
Even in this little problem you can see that, in general, the parallelization in computer games is complex and difficult because in games you typically have many dependent computations that are not easy to parallelize.
Of course, even the mentioned parallel model is very primitive and modern games use very complex game loops consisting of many threads with different degrees of dependency to one another. Or even multiple parallel game loops where each one computes its own part of simulation or rendering.
And that is, in short, a basic explanation of the function of a game loop: the process running in the background of every game, including ours. We hope you enjoyed the post and that you learned something new. Share your thoughts with us and others on our forum, Twitter, Facebook or Reddit. See you soon!Discuss this blog post on our forum or reddit.