How do you separate game logic from display?

后端 未结 8 2023
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 07:33

How can you make the display frames per second be independent from the game logic? That is so the game logic runs the same speed no matter how fast the video card can render

8条回答
  •  失恋的感觉
    2020-12-24 08:07

    There was an excellent article on flipcode about this back in the day. I would like to dig it up and present it for you.

    http://www.flipcode.com/archives/Main_Loop_with_Fixed_Time_Steps.shtml

    It's a nicely thought out loop for running a game:

    1. Single threaded
    2. At a fixed game clock
    3. With graphics as fast as possible using an interpolated clock

    Well, at least that's what I think it is. :-) Too bad the discussion that pursued after this posting is harder to find. Perhaps the wayback machine can help there.

    time0 = getTickCount();
    do
    {
      time1 = getTickCount();
      frameTime = 0;
      int numLoops = 0;
    
      while ((time1 - time0)  TICK_TIME && numLoops < MAX_LOOPS)
      {
        GameTickRun();
        time0 += TICK_TIME;
        frameTime += TICK_TIME;
        numLoops++;
    // Could this be a good idea? We're not doing it, anyway.
    //    time1 = getTickCount();
      }
      IndependentTickRun(frameTime);
    
      // If playing solo and game logic takes way too long, discard pending
    time.
      if (!bNetworkGame && (time1 - time0)  TICK_TIME)
        time0 = time1 - TICK_TIME;
    
      if (canRender)
      {
        // Account for numLoops overflow causing percent  1.
        float percentWithinTick = Min(1.f, float(time1 - time0)/TICK_TIME);
        GameDrawWithInterpolation(percentWithinTick);
      }
    }
    while (!bGameDone);
    

提交回复
热议问题