0
\$\begingroup\$

I'm learning about the game development. No i'm on the total basics - writting a proper game loop. As i've searched over the Web i'v found that simple but good working pattern using so called fixed time step:

double previous = getCurrentTime();
double lag = 0.0;
while (true)
{
  double current = getCurrentTime();
  double elapsed = current - previous;
  previous = current;
  lag += elapsed;

  processInput();

  while (lag >= MS_PER_UPDATE)
  {
    update();
    lag -= MS_PER_UPDATE;
  }

  render();
}

I roughly understand the logic behind this piece of code but i have the question regarding the game composition. What exactly the update function do and what the render function. What's their responsibility? Am i assuming right that in the super simple 2D game the update function can for e.g. calculate the distance in pixels the object (e.g. a Car) should move in the next frame and the render function in such a case just takes this calculated position and draws the Car on the screen? Correct me if i'm wrong and please provide some example/s.

Thx.

PS. Sorry for such a basic question but i'm really beginner.

\$\endgroup\$
2
  • \$\begingroup\$ Can Someone be so nice and deliver some simple implementation code examples to the update and render functions. Please... \$\endgroup\$
    – Derek81
    Commented Aug 23, 2020 at 16:01
  • \$\begingroup\$ The trouble is that these vary quite a lot depending on the content of your game. Pong and Breakout's update methods will move their balls and paddles and update the score, their render functions will draw those items. But Tetris doesn't have paddles or balls, so its update and render functions will look different, even though they serve the same role. Want to edit your question to describe what kind of content your game needs to update and draw? \$\endgroup\$
    – DMGregory
    Commented Aug 23, 2020 at 16:14

1 Answer 1

1
\$\begingroup\$

What this simple pattern is still missing is the game state. The game state is a data structure which contains all the information about the current state of the game. All the entities in the game, their type, their positions and every other information about them.

The purpose of the update-function is to update the game state. For example, if every entity in your game has a velocity, then the update-function would be responsible for changing the position of each entity by its velocity. It would then compare their new positions and see if there are any collisions. And when there are, it would do whatever is supposed to happen in case of a collision.

The purpose of the render-function is then to visualize the game state to the player by drawing it onto the screen.

\$\endgroup\$
1
  • \$\begingroup\$ Think it would be worth mentioning interpolation here for the rendering step, given that they're using a fixed time step? \$\endgroup\$
    – DMGregory
    Commented Aug 23, 2020 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .