Skip to content

Latest commit

 

History

History
73 lines (54 loc) · 2.06 KB

File metadata and controls

73 lines (54 loc) · 2.06 KB

Time, timing, timers

The most basic timing facility is the Timer. Its role is to keep track of the delta time between two updates.

using ddge::time;

auto check_time(Resource<Timer> timer) -> void
{
    auto delta = timer->delta();
    auto time_of_last_update = timer->current();
}

It can also be reset and updated.

using ddge::time;

auto check_time(Resource<Timer> timer) -> void
{
    auto last_time = timer->current();
    auto current_time = Timer::Clock::now();

    // delta gets set to `current_time - last_time`
    timer->update(current_time);

    // delta gets set to zero
    timer->reset(current_time);
}

Fixed timestamp

Some systems need to be updated at fixed intervals. This may be 2 or 60 times a second, or whatever custom time that fits best. One iteration of a game loop may take from a couple of microseconds to milliseconds, meaning that a fixed system update should be called 0 or more times per iteration. To measure how many times a system should get called at a given timepoint, it is recommended to use a FixedTimer resource.

FixedTimer is very similar to Timer, but it makes sure that you get the exact number of ticks that happened within the last update.

Integration with the scheduler

Because of how common fixed systems are, there is a dedicated scheduling primitive for scheduling fixed updates.

using namespace ddge::measurement::symbols;
namespace sch = ddge::scheduler;

using DisplayTimer = ddge::time::FixedTimer<std::chrono::microseconds, 16'667>;

constexpr auto render =
    sch::at_fixed_rate<DisplayTimer>(
        sch::start_as(clear_window)
            .then(draw)
            .then(display)
    );
Warning

Don’t nest multiple at_fixed_rate primitives, as there is no guarantee the nested fixed systems will fire the proper number of times.

Variable tick rate

Some situations require a variable tick rate to slow down or speed up gameplay. For these cases, VariableTimer comes in handy. It is just like a FixedTimer, but with an adjustable tick rate.