-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEngineRuntime.cpp
More file actions
100 lines (76 loc) · 1.99 KB
/
Copy pathEngineRuntime.cpp
File metadata and controls
100 lines (76 loc) · 1.99 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "engine/EngineRuntime.h"
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;
}
if (config.baseSeed == 0) {
config.baseSeed = 1;
}
return config;
}
} // namespace
EngineRuntime::EngineRuntime(EngineConfig config)
: config_(normalizeConfig(config)),
frameClock_(config_) {
}
void EngineRuntime::initialize() {
frameClock_.reset();
stats_ = {};
stats_.state = EngineState::Ready;
++runIndex_;
}
void EngineRuntime::play() {
if (stats_.state == EngineState::Stopped) {
initialize();
}
stats_.state = EngineState::Running;
}
void EngineRuntime::pause() {
if (stats_.state == EngineState::Running) {
stats_.state = EngineState::Paused;
}
}
void EngineRuntime::stop() {
world_.shutdown();
frameClock_.reset();
stats_ = {};
stats_.state = EngineState::Stopped;
}
void EngineRuntime::stepFrame(double deltaSeconds) {
if (stats_.state == EngineState::Stopped) {
initialize();
}
frameClock_.beginFrame(deltaSeconds);
++stats_.frameIndex;
stats_.fixedStepsThisFrame = 0;
while (frameClock_.shouldRunFixedStep()) {
frameClock_.consumeFixedStep();
++stats_.fixedStepIndex;
++stats_.fixedStepsThisFrame;
}
stats_.alpha = frameClock_.alpha();
}
EngineWorld& EngineRuntime::world() noexcept {
return world_;
}
const EngineWorld& EngineRuntime::world() const noexcept {
return world_;
}
const EngineConfig& EngineRuntime::config() const noexcept {
return config_;
}
const EngineStats& EngineRuntime::stats() const noexcept {
return stats_;
}
EngineState EngineRuntime::state() const noexcept {
return stats_.state;
}
std::uint64_t EngineRuntime::runIndex() const noexcept {
return runIndex_;
}
} // namespace safecrowd::engine