Antkeeper  0.0.1
frame-scheduler.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_UTILITY_FRAME_SCHEDULER_HPP
21 #define ANTKEEPER_UTILITY_FRAME_SCHEDULER_HPP
22 
23 #include <chrono>
24 #include <functional>
25 #include <type_traits>
26 
33 {
34 public:
36  using clock_type = std::conditional
37  <
38  std::chrono::high_resolution_clock::is_steady,
39  std::chrono::high_resolution_clock, std::chrono::steady_clock
40  >::type;
41 
43  using duration_type = clock_type::duration;
44 
46  using time_point_type = clock_type::time_point;
47 
54 
63 
65  frame_scheduler() noexcept;
66 
72  void tick();
73 
77  void refresh() noexcept;
78 
82  void reset() noexcept;
83 
89  inline void set_fixed_update_interval(duration_type interval) noexcept
90  {
91  m_fixed_update_interval = interval;
92  }
93 
99  inline void set_min_frame_duration(duration_type duration) noexcept
100  {
101  m_min_frame_duration = duration;
102  }
103 
109  inline void set_max_frame_duration(duration_type duration) noexcept
110  {
111  m_max_frame_duration = duration;
112  }
113 
120  inline void set_fixed_update_callback(fixed_update_callback_type&& callback) noexcept
121  {
122  m_fixed_update_callback = callback;
123  }
124  inline void set_fixed_update_callback(const fixed_update_callback_type& callback) noexcept
125  {
126  m_fixed_update_callback = callback;
127  }
129 
137  {
138  m_variable_update_callback = callback;
139  }
140  inline void set_variable_update_callback(const variable_update_callback_type& callback) noexcept
141  {
142  m_variable_update_callback = callback;
143  }
145 
147  [[nodiscard]] inline duration_type get_fixed_update_time() const noexcept
148  {
149  return m_fixed_update_time;
150  }
151 
153  [[nodiscard]] inline duration_type get_accumulated_time() const noexcept
154  {
155  return m_accumulated_time;
156  }
157 
159  [[nodiscard]] inline duration_type get_frame_duration() const noexcept
160  {
161  return m_frame_duration;
162  }
163 
165  [[nodiscard]] inline duration_type get_min_frame_duration() const noexcept
166  {
167  return m_min_frame_duration;
168  }
169 
171  [[nodiscard]] inline duration_type get_max_frame_duration() const noexcept
172  {
173  return m_max_frame_duration;
174  }
175 
177  [[nodiscard]] inline duration_type get_fixed_update_interval() const noexcept
178  {
179  return m_fixed_update_interval;
180  }
181 
183  [[nodiscard]] inline const fixed_update_callback_type& get_fixed_update_callback() const noexcept
184  {
185  return m_fixed_update_callback;
186  }
187 
189  [[nodiscard]] inline const variable_update_callback_type& get_variable_update_callback() const noexcept
190  {
191  return m_variable_update_callback;
192  }
193 
194 private:
195  duration_type m_fixed_update_time{duration_type::zero()};
196  duration_type m_accumulated_time{duration_type::zero()};
197 
198  time_point_type m_frame_start_time;
199  time_point_type m_frame_end_time;
200  duration_type m_frame_duration{duration_type::zero()};
201 
202  duration_type m_min_frame_duration{duration_type::zero()};
203  duration_type m_max_frame_duration{duration_type::max()};
204 
205  duration_type m_fixed_update_interval{duration_type::zero()};
206 
207  fixed_update_callback_type m_fixed_update_callback;
208  variable_update_callback_type m_variable_update_callback;
209 };
210 
211 #endif // ANTKEEPER_UTILITY_FRAME_SCHEDULER_HPP
Schedules fixed- and variable-rate updates.
clock_type::time_point time_point_type
Time point type matches the clock's time point type.
void reset() noexcept
Resets the elapsed fixed-rate update time (t), accumulated time (at), and frame timer.
std::conditional< std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock >::type clock_type
Clock type is std::chrono::high_resolution_clock if steady, std::chrono::steady_clock otherwise.
duration_type get_max_frame_duration() const noexcept
Returns the maximum frame duration.
duration_type get_accumulated_time() const noexcept
Returns the accumulated time (at).
void set_min_frame_duration(duration_type duration) noexcept
Sets the minimum frame duration.
void set_max_frame_duration(duration_type duration) noexcept
Sets the maximum accumulated frame duration.
duration_type get_min_frame_duration() const noexcept
Returns the minimum frame duration.
duration_type get_fixed_update_time() const noexcept
Returns the elapsed fixed-rate update time (t).
const variable_update_callback_type & get_variable_update_callback() const noexcept
Returns the variable-rate update callback.
std::function< void(duration_type, duration_type)> fixed_update_callback_type
Fixed-rate update callback function type.
void refresh() noexcept
Resets the accumulated time (at) and frame timer, but not the elapsed fixed-rate update time.
void set_fixed_update_interval(duration_type interval) noexcept
Sets the interval (dt) at which fixed-rate updates are scheduled.
frame_scheduler() noexcept
Constructs a frame scheduler and starts its frame timer.
void tick()
Performs any scheduled fixed-rate updates followed by a single variable-rate update.
const fixed_update_callback_type & get_fixed_update_callback() const noexcept
Returns the fixed-rate update callback.
void set_fixed_update_callback(fixed_update_callback_type &&callback) noexcept
Sets the fixed-rate update callback.
duration_type get_fixed_update_interval() const noexcept
Returns the fixed-rate update interval (dt).
void set_fixed_update_callback(const fixed_update_callback_type &callback) noexcept
Sets the fixed-rate update callback.
duration_type get_frame_duration() const noexcept
Returns the duration of the previous frame.
clock_type::duration duration_type
Duration type matches the clock's duration type.
void set_variable_update_callback(variable_update_callback_type &&callback) noexcept
Sets the variable-rate update callback.
std::function< void(duration_type, duration_type, duration_type)> variable_update_callback_type
Variable-rate callback function type.
void set_variable_update_callback(const variable_update_callback_type &callback) noexcept
Sets the variable-rate update callback.
constexpr vector< T, N > max(const vector< T, N > &x, const vector< T, N > &y)
Returns a vector containing the maximum elements of two vectors.
Definition: vector.hpp:1328