-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFrameClock.cpp
More file actions
65 lines (48 loc) · 1.61 KB
/
FrameClock.cpp
File metadata and controls
65 lines (48 loc) · 1.61 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
56
57
58
59
60
61
62
63
64
65
#include "engine/FrameClock.h"
#include <algorithm>
#include <cmath>
namespace safecrowd::engine {
namespace {
EngineConfig normalizeConfig(EngineConfig config) {
if (config.fixedDeltaTime <= 0.0) {
config.fixedDeltaTime = 1.0 / 60.0;
}
if (config.maxCatchUpSteps == 0) {
config.maxCatchUpSteps = 1;
}
return config;
}
} // namespace
FrameClock::FrameClock(const EngineConfig& config)
: config_(normalizeConfig(config)) {
}
void FrameClock::reset() {
accumulatedSeconds_ = 0.0;
pendingFixedSteps_ = 0;
}
void FrameClock::beginFrame(double deltaSeconds) {
const double safeDeltaSeconds = std::max(0.0, deltaSeconds);
const double maxAccumulatedSeconds =
config_.fixedDeltaTime * static_cast<double>(config_.maxCatchUpSteps);
accumulatedSeconds_ = std::min(accumulatedSeconds_ + safeDeltaSeconds, maxAccumulatedSeconds);
const double rawFixedSteps = std::floor(accumulatedSeconds_ / config_.fixedDeltaTime);
pendingFixedSteps_ = static_cast<std::uint32_t>(rawFixedSteps);
}
bool FrameClock::shouldRunFixedStep() const {
return pendingFixedSteps_ > 0;
}
void FrameClock::consumeFixedStep() {
if (!shouldRunFixedStep()) {
return;
}
--pendingFixedSteps_;
accumulatedSeconds_ = std::max(0.0, accumulatedSeconds_ - config_.fixedDeltaTime);
}
double FrameClock::alpha() const {
const double alphaValue = accumulatedSeconds_ / config_.fixedDeltaTime;
return std::clamp(alphaValue, 0.0, 1.0);
}
std::uint32_t FrameClock::pendingFixedSteps() const noexcept {
return pendingFixedSteps_;
}
} // namespace safecrowd::engine