Unravel Engine C++ Reference
Loading...
Searching...
No Matches
simulation.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5#include <vector>
6
7namespace unravel
8{
9
10//-----------------------------------------------------------------------------
11// Main Class Declarations
12//-----------------------------------------------------------------------------
13//-----------------------------------------------------------------------------
14// Name : simulation (Class)
18//-----------------------------------------------------------------------------
20{
21 using clock_t = std::chrono::steady_clock;
22 using timepoint_t = clock_t::time_point;
23 using duration_t = clock_t::duration;
24 using delta_t = std::chrono::duration<float>;
25 simulation();
26
27 //-----------------------------------------------------------------------------
28 // Name : run_one_frame ()
32 //-----------------------------------------------------------------------------
33 void run_one_frame(bool is_active);
34
38 void reset_delta_clock();
39
40 //-----------------------------------------------------------------------------
41 // Name : get_frame ()
47 //-----------------------------------------------------------------------------
48 auto get_frame() const -> uint64_t;
49
50 //-----------------------------------------------------------------------------
51 // Name : set_min_fps ()
58 //-----------------------------------------------------------------------------
59 void set_min_fps(uint32_t fps);
60
61 //-----------------------------------------------------------------------------
62 // Name : set_max_fps ()
67 //-----------------------------------------------------------------------------
68 void set_max_fps(uint32_t fps);
69
70 //-----------------------------------------------------------------------------
71 // Name : set_max_inactive_fps ()
76 //-----------------------------------------------------------------------------
77 void set_max_inactive_fps(uint32_t fps);
78
79 //-----------------------------------------------------------------------------
80 // Name : set_time_smoothing_step ()
84 //-----------------------------------------------------------------------------
85 void set_time_smoothing_step(uint32_t step);
86
87 //-----------------------------------------------------------------------------
88 // Name : get_time_since_launch ()
92 //-----------------------------------------------------------------------------
93 auto get_time_since_launch() const -> duration_t;
94
95 //-----------------------------------------------------------------------------
96 // Name : get_fps ()
100 //-----------------------------------------------------------------------------
101 auto get_fps() const -> float;
102
103 //-----------------------------------------------------------------------------
104 // Name : get_delta_time ()
108 //-----------------------------------------------------------------------------
109 auto get_delta_time() const -> delta_t;
110
111
112 void set_time_scale(float time_scale = 1.0f);
113 auto get_time_scale() const -> float;
114
115 //-----------------------------------------------------------------------------
116 // Name : get_max_fps ()
120 //-----------------------------------------------------------------------------
121 auto get_max_fps() const -> uint32_t;
122
123 //-----------------------------------------------------------------------------
124 // Name : get_last_sleep_duration ()
130 //-----------------------------------------------------------------------------
131 auto get_last_sleep_duration() const -> duration_t;
132
133protected:
134 float time_scale_{1.0f};
136 uint32_t min_fps_ = 0;
138 uint32_t max_fps_ = 0;
140 uint32_t max_inactive_fps_ = 20;
142 std::vector<duration_t> previous_timesteps_;
144 duration_t timestep_ = duration_t::zero();
146 duration_t last_sleep_duration_ = duration_t::zero();
148 uint64_t frame_ = 0;
150 uint32_t smoothing_step_ = 11;
155};
156} // namespace unravel
Class responsible for timers.
Definition simulation.h:20
std::vector< duration_t > previous_timesteps_
previous time steps for smoothing in seconds
Definition simulation.h:142
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:152
duration_t last_sleep_duration_
time spent sleeping inside the last run_one_frame call
Definition simulation.h:146
duration_t timestep_
next frame time step in seconds
Definition simulation.h:144
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:150
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
std::chrono::steady_clock clock_t
Definition simulation.h:21
uint32_t max_inactive_fps_
Definition simulation.h:140
auto get_last_sleep_duration() const -> duration_t
Returns how long run_one_frame spent sleeping during the last call. Useful for stats overlays so they...
uint64_t frame_
current frame
Definition simulation.h:148
clock_t::time_point timepoint_t
Definition simulation.h:22
void set_max_fps(uint32_t fps)
Set maximum frames per second. The engine will sleep if fps is higher than this.
auto get_max_fps() const -> uint32_t
Returns the configured software FPS cap (0 means uncapped).
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:154
clock_t::duration duration_t
Definition simulation.h:23
uint32_t min_fps_
minimum/maximum frames per second
Definition simulation.h:136