mercredi 1 juillet 2015

fixed amount of renders per second with a fixed logic update per second

I read the gaffer article on fix your tilmestep and also dewitters about fixed frame render rate.

I agree with both parts, I think choosing a desired frame rate and locking it locking the frame rate at 24, 30, 60 or even 120 at the beginning of the project and then working within those limitations would be ideal.

I also think that making those same decisions at the beginning for the logic loop as well.

So let's say you want to target having an update loop that runs 1/24 and a rendering loop that runs at a constant 1/30.

I was trying to combine both articles to create a game loop that would work and I hope that I can get some feedback. Here's my code below:

  double logicTime = 0.0;
  const logicDt = 1/120.0;

  double currLogicTime = 0.0; //get time in seconds
  double LogicAccum = 0.0;

  int ftps = 30;
  int fpsSkipTicks = 1000/ftps;
  int maxFrameSkips = 5;

  double nextFtps = 0.0 //get time in seconds
  int loops;
  float interpolation;

  while(game_running)
  {
    double newLogicTime = 0.0; //get time in seconds
    double frameLogicTime = newLogicTime - currTime;
    currLogicTime = newLogicTime;

    LocgicAccum += frameLogicTime;
    while(LocgicAccum >= logicDt)
    {
      //update core game logic here with fixed steps
      LogicAccum -= logicDt;
      logicTime += logicDt;
    }
    //can also update none essential things here such as background animations, particles, etc.

    loops = 0;
    while( /* get time in milliseconds */ > nextFtps && loops < maxFrameSkips) {

            nextFtps += fpsSkipTicks;
            loops++;
        }

        interpolation = float( /* get time in milliseconds */ + fpsSkipTicks - nextFtps ) / float( SKIP_TICKS );
        //render here with interpolation so that I can multiply the objects position based on the interpolation value
        render( interpolation );

  }

As you can see there are two distinct loops inside the main loop. One updates logic at a fixed rate and another updates the renderer also at a fixed time.

Aucun commentaire:

Enregistrer un commentaire