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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use context::Context;
use std::time;
use std::thread;
#[derive(Debug, Clone)]
struct LogBuffer<T> where T: Clone {
head: usize,
size: usize,
contents: Vec<T>,
}
impl<T> LogBuffer<T>
where T: Clone + Copy
{
fn new(size: usize, init_val: T) -> LogBuffer<T> {
let mut v = Vec::with_capacity(size);
v.resize(size, init_val);
LogBuffer {
head: 0,
size: size,
contents: v,
}
}
fn push(&mut self, item: T) {
self.head = (self.head + 1) % self.size;
self.contents[self.head] = item;
}
fn contents(&self) -> &[T] {
&self.contents
}
fn latest(&self) -> T {
self.contents[self.head]
}
}
#[derive(Debug)]
pub struct TimeContext {
init_instant: time::Instant,
last_instant: time::Instant,
frame_durations: LogBuffer<time::Duration>,
residual_update_dt: time::Duration,
}
const TIME_LOG_FRAMES: usize = 200;
impl TimeContext {
pub fn new() -> TimeContext {
TimeContext {
init_instant: time::Instant::now(),
last_instant: time::Instant::now(),
frame_durations: LogBuffer::new(TIME_LOG_FRAMES, time::Duration::new(0, 0)),
residual_update_dt: time::Duration::from_secs(0),
}
}
pub fn tick(&mut self) {
let now = time::Instant::now();
let time_since_last = now - self.last_instant;
self.frame_durations.push(time_since_last);
self.last_instant = now;
}
}
impl Default for TimeContext {
fn default() -> Self {
Self::new()
}
}
pub fn get_delta(ctx: &Context) -> time::Duration {
let tc = &ctx.timer_context;
tc.frame_durations.latest()
}
pub fn get_average_delta(ctx: &Context) -> time::Duration {
let tc = &ctx.timer_context;
let init = time::Duration::new(0, 0);
let sum = tc.frame_durations
.contents()
.iter()
.fold(init, |d1, d2| d1 + *d2);
sum / (TIME_LOG_FRAMES as u32)
}
pub fn duration_to_f64(d: time::Duration) -> f64 {
let seconds = d.as_secs() as f64;
let nanos = d.subsec_nanos() as f64;
seconds + (nanos * 1e-9)
}
pub fn f64_to_duration(t: f64) -> time::Duration {
debug_assert!(t >= 0.0, "f64_to_duration passed a negative number!");
let seconds = t.trunc();
let nanos = t.fract() * 1e9;
time::Duration::new(seconds as u64, nanos as u32)
}
fn fps_as_duration(fps: u64) -> time::Duration {
let target_dt_seconds = 1.0 / (fps as f64);
f64_to_duration(target_dt_seconds)
}
pub fn get_fps(ctx: &Context) -> f64 {
let duration_per_frame = get_average_delta(ctx);
let seconds_per_frame = duration_to_f64(duration_per_frame);
1.0 / seconds_per_frame
}
pub fn get_time_since_start(ctx: &Context) -> time::Duration {
let tc = &ctx.timer_context;
time::Instant::now() - tc.init_instant
}
pub fn check_update_time(ctx: &mut Context, desired_update_rate: u64) -> bool {
let dt = get_delta(ctx);
let timedata = &mut ctx.timer_context;
let target_dt = fps_as_duration(desired_update_rate);
timedata.residual_update_dt += dt;
if timedata.residual_update_dt > target_dt {
timedata.residual_update_dt -= target_dt;
true
} else {
false
}
}
pub fn sleep_until_next_frame(ctx: &Context, desired_fps: u32) {
assert!(desired_fps > 0);
let tc = &ctx.timer_context;
let fps_delay = 1.0 / (desired_fps as f64);
let nanos_per_frame = fps_delay * 1e9;
let duration_per_frame = time::Duration::new(0, nanos_per_frame as u32);
let now = time::Instant::now();
let time_spent_this_frame = now - tc.last_instant;
if time_spent_this_frame >= duration_per_frame {
()
} else {
let duration_to_sleep = duration_per_frame - time_spent_this_frame;
thread::sleep(duration_to_sleep);
}
}
pub fn sleep(duration: time::Duration) {
thread::sleep(duration);
}