Antkeeper  0.0.1
Public Types | Public Member Functions | List of all members
frame_scheduler Class Reference

Schedules fixed- and variable-rate updates. More...

#include <frame-scheduler.hpp>

Public Types

using clock_type = std::conditional< std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock >::type
 Clock type is std::chrono::high_resolution_clock if steady, std::chrono::steady_clock otherwise. More...
 
using duration_type = clock_type::duration
 Duration type matches the clock's duration type. More...
 
using time_point_type = clock_type::time_point
 Time point type matches the clock's time point type. More...
 
using fixed_update_callback_type = std::function< void(duration_type, duration_type)>
 Fixed-rate update callback function type. More...
 
using variable_update_callback_type = std::function< void(duration_type, duration_type, duration_type)>
 Variable-rate callback function type. More...
 

Public Member Functions

 frame_scheduler () noexcept
 Constructs a frame scheduler and starts its frame timer. More...
 
void tick ()
 Performs any scheduled fixed-rate updates followed by a single variable-rate update. More...
 
void refresh () noexcept
 Resets the accumulated time (at) and frame timer, but not the elapsed fixed-rate update time. More...
 
void reset () noexcept
 Resets the elapsed fixed-rate update time (t), accumulated time (at), and frame timer. More...
 
void set_fixed_update_interval (duration_type interval) noexcept
 Sets the interval (dt) at which fixed-rate updates are scheduled. More...
 
void set_min_frame_duration (duration_type duration) noexcept
 Sets the minimum frame duration. More...
 
void set_max_frame_duration (duration_type duration) noexcept
 Sets the maximum accumulated frame duration. More...
 
duration_type get_fixed_update_time () const noexcept
 Returns the elapsed fixed-rate update time (t). More...
 
duration_type get_accumulated_time () const noexcept
 Returns the accumulated time (at). More...
 
duration_type get_frame_duration () const noexcept
 Returns the duration of the previous frame. More...
 
duration_type get_min_frame_duration () const noexcept
 Returns the minimum frame duration. More...
 
duration_type get_max_frame_duration () const noexcept
 Returns the maximum frame duration. More...
 
duration_type get_fixed_update_interval () const noexcept
 Returns the fixed-rate update interval (dt). More...
 
const fixed_update_callback_typeget_fixed_update_callback () const noexcept
 Returns the fixed-rate update callback. More...
 
const variable_update_callback_typeget_variable_update_callback () const noexcept
 Returns the variable-rate update callback. More...
 
void set_fixed_update_callback (fixed_update_callback_type &&callback) noexcept
 Sets the fixed-rate update callback. More...
 
void set_fixed_update_callback (const fixed_update_callback_type &callback) noexcept
 Sets the fixed-rate update callback. More...
 
void set_variable_update_callback (variable_update_callback_type &&callback) noexcept
 Sets the variable-rate update callback. More...
 
void set_variable_update_callback (const variable_update_callback_type &callback) noexcept
 Sets the variable-rate update callback. More...
 

Detailed Description

Schedules fixed- and variable-rate updates.

See also
Fiedler, G. (2004). Fix your timestep. Gaffer On Games.

Definition at line 32 of file frame-scheduler.hpp.

Member Typedef Documentation

◆ clock_type

using frame_scheduler::clock_type = std::conditional < std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock >::type

Clock type is std::chrono::high_resolution_clock if steady, std::chrono::steady_clock otherwise.

Definition at line 36 of file frame-scheduler.hpp.

◆ duration_type

using frame_scheduler::duration_type = clock_type::duration

Duration type matches the clock's duration type.

Definition at line 43 of file frame-scheduler.hpp.

◆ fixed_update_callback_type

Fixed-rate update callback function type.

The first parameter is the elapsed time (t) and the second parameter is the fixed-rate update interval (dt).

Definition at line 53 of file frame-scheduler.hpp.

◆ time_point_type

using frame_scheduler::time_point_type = clock_type::time_point

Time point type matches the clock's time point type.

Definition at line 46 of file frame-scheduler.hpp.

◆ variable_update_callback_type

Variable-rate callback function type.

The first parameter is the elapsed time (t), the second parameter is the fixed-rate update interval (dt), and the third parameter is the accumulated time since the previous fixed-rate update (at).

Note
The subframe interpolation factor (alpha) can be calculated as at / dt.

Definition at line 62 of file frame-scheduler.hpp.

Constructor & Destructor Documentation

◆ frame_scheduler()

frame_scheduler::frame_scheduler ( )
noexcept

Constructs a frame scheduler and starts its frame timer.

Definition at line 24 of file frame-scheduler.cpp.

Member Function Documentation

◆ get_accumulated_time()

duration_type frame_scheduler::get_accumulated_time ( ) const
inlinenoexcept

Returns the accumulated time (at).

Definition at line 153 of file frame-scheduler.hpp.

◆ get_fixed_update_callback()

const fixed_update_callback_type& frame_scheduler::get_fixed_update_callback ( ) const
inlinenoexcept

Returns the fixed-rate update callback.

Definition at line 183 of file frame-scheduler.hpp.

◆ get_fixed_update_interval()

duration_type frame_scheduler::get_fixed_update_interval ( ) const
inlinenoexcept

Returns the fixed-rate update interval (dt).

Definition at line 177 of file frame-scheduler.hpp.

◆ get_fixed_update_time()

duration_type frame_scheduler::get_fixed_update_time ( ) const
inlinenoexcept

Returns the elapsed fixed-rate update time (t).

Definition at line 147 of file frame-scheduler.hpp.

◆ get_frame_duration()

duration_type frame_scheduler::get_frame_duration ( ) const
inlinenoexcept

Returns the duration of the previous frame.

Definition at line 159 of file frame-scheduler.hpp.

◆ get_max_frame_duration()

duration_type frame_scheduler::get_max_frame_duration ( ) const
inlinenoexcept

Returns the maximum frame duration.

Definition at line 171 of file frame-scheduler.hpp.

◆ get_min_frame_duration()

duration_type frame_scheduler::get_min_frame_duration ( ) const
inlinenoexcept

Returns the minimum frame duration.

Definition at line 165 of file frame-scheduler.hpp.

◆ get_variable_update_callback()

const variable_update_callback_type& frame_scheduler::get_variable_update_callback ( ) const
inlinenoexcept

Returns the variable-rate update callback.

Definition at line 189 of file frame-scheduler.hpp.

◆ refresh()

void frame_scheduler::refresh ( )
noexcept

Resets the accumulated time (at) and frame timer, but not the elapsed fixed-rate update time.

Definition at line 72 of file frame-scheduler.cpp.

◆ reset()

void frame_scheduler::reset ( )
noexcept

Resets the elapsed fixed-rate update time (t), accumulated time (at), and frame timer.

Definition at line 79 of file frame-scheduler.cpp.

◆ set_fixed_update_callback() [1/2]

void frame_scheduler::set_fixed_update_callback ( const fixed_update_callback_type callback)
inlinenoexcept

Sets the fixed-rate update callback.

Parameters
callbackFixed-rate update callback.

Definition at line 124 of file frame-scheduler.hpp.

◆ set_fixed_update_callback() [2/2]

void frame_scheduler::set_fixed_update_callback ( fixed_update_callback_type &&  callback)
inlinenoexcept

Sets the fixed-rate update callback.

Parameters
callbackFixed-rate update callback.

Definition at line 120 of file frame-scheduler.hpp.

◆ set_fixed_update_interval()

void frame_scheduler::set_fixed_update_interval ( duration_type  interval)
inlinenoexcept

Sets the interval (dt) at which fixed-rate updates are scheduled.

Parameters
intervalFixed-rate update interval.

Definition at line 89 of file frame-scheduler.hpp.

◆ set_max_frame_duration()

void frame_scheduler::set_max_frame_duration ( duration_type  duration)
inlinenoexcept

Sets the maximum accumulated frame duration.

Prevents a "spiral of death", in which updates are scheduled too many times per frame while trying to catch up to the target update rate.

Parameters
durationMaximum accumulated frame duration.

Definition at line 109 of file frame-scheduler.hpp.

◆ set_min_frame_duration()

void frame_scheduler::set_min_frame_duration ( duration_type  duration)
inlinenoexcept

Sets the minimum frame duration.

If a frame is quicker than the minimum frame duration, the CPU will be idled until the minimum frame duration is met.

Parameters
durationMinimum frame duration.

Definition at line 99 of file frame-scheduler.hpp.

◆ set_variable_update_callback() [1/2]

void frame_scheduler::set_variable_update_callback ( const variable_update_callback_type callback)
inlinenoexcept

Sets the variable-rate update callback.

Parameters
callbackVariable-rate update callback.

Definition at line 140 of file frame-scheduler.hpp.

◆ set_variable_update_callback() [2/2]

void frame_scheduler::set_variable_update_callback ( variable_update_callback_type &&  callback)
inlinenoexcept

Sets the variable-rate update callback.

Parameters
callbackVariable-rate update callback.

Definition at line 136 of file frame-scheduler.hpp.

◆ tick()

void frame_scheduler::tick ( )

Performs any scheduled fixed-rate updates followed by a single variable-rate update.

Warning
Both the fixed-rate and variable-rate update callbacks must be valid when calling tick().

Definition at line 29 of file frame-scheduler.cpp.


The documentation for this class was generated from the following files: