官术网_书友最值得收藏!

Fixed time-step

In some cases, the code for time management that we've written doesn't really apply correctly. Let's say we only want to call certain methods at a fixed rate of 60 times per second. It could be a physics system that requires updating only a certain amount of times, or it can be useful if the game is grid-based. Whatever the case is, when an update rate is really important, a fixed time-step is your friend. Unlike the variable time-step, where the next update and draw happens as soon as the previous one is done, the fixed time-step approach will ensure that certain game logic is only happening at a provided rate. It's fairly simple to implement a fixed time-step. First, we must make sure that instead of overwriting the elapsed time value of the previous iteration, we add to it like so:

void Game::RestartClock(){
    m_elapsed += m_clock.restart();
}

The basic expression for calculating the amount of time for an individual update throughout a 1 second interval is illustrated here:

Let's say we want our game to update 60 times a second. To find the frame time, we would divide 1 by 60 and check if the elapsed time has exceeded that value, as shown here:

float frametime = 1.0f / 60.0f;

if(m_elapsed.asSeconds() >= frametime){
    // Do something 60 times a second.
    ...
    m_elapsed -= sf::seconds(frametime); // Subtracting.
}

Notice the subtraction at the end. This is how we reset the cycle and keep the simulation running at a constant speed. Depending on your application, you might want to put it to sleep in between updates in order to relieve the CPU. Aside from that detail, these are the bare bones of the fixed time-step. This is the exact technique that will be used in the game that we will finish building in the next chapter.

主站蜘蛛池模板: 绩溪县| 斗六市| 开封市| 玉环县| 滦平县| 扎鲁特旗| 霍林郭勒市| 子长县| 普格县| 沂水县| 临桂县| 嘉黎县| 襄城县| 汝阳县| 肥西县| 红原县| 湟源县| 大竹县| 兴宁市| 老河口市| 合肥市| 石阡县| 炎陵县| 三门峡市| 宜兴市| 陇南市| 长兴县| 泸定县| 昭苏县| 科尔| 通城县| 图木舒克市| 原平市| 临夏市| 太和县| 密山市| 万全县| 枣强县| 勃利县| 津南区| 昌黎县|