-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlooper.cpp
More file actions
55 lines (44 loc) · 1.53 KB
/
Copy pathlooper.cpp
File metadata and controls
55 lines (44 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "core/looper.h"
#include <chrono>
namespace iris
{
Looper::Looper(
std::chrono::microseconds clock,
std::chrono::microseconds timestep,
LoopFunction fixed_timestep,
LoopFunction variable_timestep)
: clock_(clock)
, timestep_(timestep)
, fixed_timestep_(fixed_timestep)
, variable_timestep_(variable_timestep)
{
}
void Looper::run()
{
auto run = true;
auto start = std::chrono::steady_clock::now();
std::chrono::steady_clock::duration accumulator(0);
do
{
// calculate duration of last frame
const auto end = std::chrono::steady_clock::now();
const auto frame_time = end - start;
start = end;
// variable time step function produces time
accumulator += frame_time;
// fixed time step function consumed time
while (run && (accumulator >= timestep_))
{
run &= fixed_timestep_(clock_, timestep_);
accumulator -= timestep_;
clock_ += timestep_;
}
run &= variable_timestep_(clock_, std::chrono::duration_cast<std::chrono::microseconds>(frame_time));
} while (run);
}
}