zoukankan      html  css  js  c++  java
  • The Game Loop

    1 #define MAXIMUM_FRAME_RATE 120
    2 #define MINIMUM_FRAME_RATE 30
    3 #define UPDATE_INTERVAL (1.0 / MAXIMUM_FRAME_RATE)
    4 #define MAX_CYCLES_PER_FRAME (MAXIMUM_FRAME_RATE / MINIMUM_FRAME_RATE)
    5 - (void)gameLoop {
    6   static double lastFrameTime = 0.0f;
    7   static double cyclesLeftOver = 0.0f;
    8   double currentTime;
    9   double updateIterations;

    10   // Apple advises to use CACurrentMediaTime() as CFAbsoluteTimeGetCurrent() is
    11   // synced with the mobile network time and so could change causing hiccups.
    12   currentTime = CACurrentMediaTime();
    13   updateIterations = ((currentTime - lastFrameTime) + cyclesLeftOver);

    14   if(updateIterations > (MAX_CYCLES_PER_FRAME * UPDATE_INTERVAL))
    15     updateIterations = (MAX_CYCLES_PER_FRAME * UPDATE_INTERVAL);

    16   while (updateIterations >= UPDATE_INTERVAL) {
    17     updateIterations -= UPDATE_INTERVAL;

    18     // Update the game logic passing in the fixed update interval as the delta
    19     [sharedGameController updateCurrentSceneWithDelta:UPDATE_INTERVAL];
    20   }

    21   cyclesLeftOver = updateIterations;
    22   lastFrameTime = currentTime;

    23   // Render the scene
    24   [self drawView:nil];
    25 }

    游戏性能可以分为三个层次,

    第一,当每次GameLoop间隔小于等于UPDATE_INTERVAL时,保证所有的帧都被绘制,达到了最佳游戏性能。

    第二,当每次GameLoop间隔置于UPDATE_INTERVAL和MAX_RECYCLE*UPDATE_INTERVAL之间时,数据每更新几次(最多MAX_RECYCLE次),才绘制一帧,导致帧减少,游戏画面下降,俗称“卡”。此种情况下,不影响游戏时间轴,即不影响速度。

    第三,当每次GameLoop间隔大于MAX_RECYLE*UPDATE_INTERVAL时(机器性能实在太差),此种情况下,游戏数据计算缓慢,不能及时更新,会导致游戏时间轴变慢,画面比卡更卡。这就在有些老机器上运行《魔兽争霸》时常常会看到的,烂机器上运行的慢,人物一秒钟才走一步的情况。

  • 相关阅读:
    js"发送验证码"倒计时效果!
    input:button按钮文字换行
    最新jQuery引用google地址外部文件(jquery 1.2.6至jquery1.7.2)
    overflow:hidden ie6,7失效
    ZeroClipboard支持IE,firefox,Chrome复制到剪贴板(转)
    js取url参数
    弹出层高度不限垂直居中 兼容ie ff chrome
    jQuery 2.0将不再支持IE 6/7/8
    CSS: IE中的BUG之marginbottom失效
    inputSuggest邮箱提示自动补全js插件
  • 原文地址:https://www.cnblogs.com/tekkaman/p/2172590.html
Copyright © 2011-2022 走看看