Unravel Engine C++ Reference
Loading...
Searching...
No Matches
simulation.cpp
Go to the documentation of this file.
1#include "simulation.h"
2#include <algorithm>
3#include <thread>
4#include <limits>
5
6namespace unravel
7{
8using namespace std::chrono_literals;
9
17
18void simulation::run_one_frame(bool is_active)
19{
20 // perform waiting loop if maximum fps set
21 auto max_fps = max_fps_;
22 if(!is_active && max_fps > 0)
23 {
24 max_fps = std::min(max_inactive_fps_, max_fps);
25 }
26
27 duration_t elapsed = clock_t::now() - last_frame_timepoint_;
28 if(max_fps > 0)
29 {
30 duration_t target_duration = 1000ms / max_fps;
31
32 for(;;)
33 {
34 elapsed = clock_t::now() - last_frame_timepoint_;
35 if(elapsed >= target_duration)
36 {
37 break;
38 }
39
40 if(elapsed < duration_t(0))
41 {
42 break;
43 }
44 duration_t sleep_time = (target_duration - elapsed);
45 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(sleep_time);
46
47 if(sleep_time > std::chrono::microseconds(1000))
48 {
49 if(ms.count() > 0)
50 {
51 sleep_time /= ms.count();
52 }
53
54 std::this_thread::sleep_for(sleep_time);
55 }
56 }
57 }
58
59 if(elapsed < duration_t(0))
60 {
62 }
63 last_frame_timepoint_ = clock_t::now();
64
65 // if fps lower than minimum, clamp eplased time
66 if(min_fps_ > 0)
67 {
68 duration_t target_duration = 1000ms / min_fps_;
69 if(elapsed > target_duration)
70 {
71 elapsed = target_duration;
72 }
73 }
74
75 // perform time step smoothing
76 if(smoothing_step_ > 0)
77 {
78 timestep_ = duration_t::zero();
81 {
82 auto begin = previous_timesteps_.begin();
83 previous_timesteps_.erase(begin, begin + int(previous_timesteps_.size() - smoothing_step_));
84 for(auto step : previous_timesteps_)
85 {
86 timestep_ += step;
87 }
88 timestep_ /= static_cast<duration_t::rep>(previous_timesteps_.size());
89 }
90 else
91 {
93 }
94 }
95 else
96 {
98 }
99
100 ++frame_;
101}
102
103auto simulation::get_frame() const -> uint64_t
104{
105 return frame_;
106}
107
108void simulation::set_min_fps(uint32_t fps)
109{
110 min_fps_ = std::max<uint32_t>(fps, 0);
111}
112
113void simulation::set_max_fps(uint32_t fps)
114{
115 max_fps_ = std::max<uint32_t>(fps, 0);
116}
117
119{
120 max_inactive_fps_ = std::max<uint32_t>(fps, 0);
121}
122
124{
125 smoothing_step_ = step;
126}
127
129{
130 return clock_t::now() - launch_timepoint_;
131}
132
133auto simulation::get_fps() const -> float
134{
135 auto dt = std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(get_delta_time()).count();
136 return (std::abs(dt) < std::numeric_limits<float>::epsilon()) ? 0 : 1000.0f / dt;
137}
138
140{
141 auto dt = std::chrono::duration_cast<delta_t>(timestep_) * time_scale_;
142 return dt;
143}
144
145void simulation::set_time_scale(float time_scale)
146{
147 time_scale_ = time_scale;
148}
149
150auto simulation::get_time_scale() const -> float
151{
152 return time_scale_;
153}
154} // namespace unravel
float elapsed
std::vector< duration_t > previous_timesteps_
previous time steps for smoothing in seconds
Definition simulation.h:119
auto get_delta_time() const -> delta_t
Returns the delta time in seconds.
auto get_time_scale() const -> float
auto get_fps() const -> float
Returns frames per second.
void set_time_scale(float time_scale=1.0f)
void set_time_smoothing_step(uint32_t step)
Set how many frames to average for timestep smoothing.
timepoint_t last_frame_timepoint_
frame update timer
Definition simulation.h:127
duration_t timestep_
next frame time step in seconds
Definition simulation.h:121
void set_max_inactive_fps(uint32_t fps)
Set maximum frames per second when the application does not have input focus.
uint32_t smoothing_step_
how many frames to average for the smoothed time step
Definition simulation.h:125
void run_one_frame(bool is_active)
Perform on frame computations with specified fps.
auto get_frame() const -> uint64_t
auto get_time_since_launch() const -> duration_t
Returns duration since launch.
std::chrono::duration< float > delta_t
Definition simulation.h:24
uint32_t max_inactive_fps_
Definition simulation.h:117
uint64_t frame_
current frame
Definition simulation.h:123
void set_max_fps(uint32_t fps)
Set maximum frames per second. The engine will sleep if fps is higher than this.
void set_min_fps(uint32_t fps)
Set minimum frames per second. If fps goes lower than this, time will appear to slow.
timepoint_t launch_timepoint_
time point when we launched
Definition simulation.h:129
clock_t::duration duration_t
Definition simulation.h:23
uint32_t min_fps_
minimum/maximum frames per second
Definition simulation.h:113