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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Types for counting and recording time in a simulation.
//!
//! `clock` is the primary module for time tracking in `game_time`. It
//! provides two primary types: `GameClock`, a "clock" for tracking frames
//! and time progression within the simulation and `GameTime`, a specific
//! point in time within the simulation.
use std::thread;
use std::time;

use chrono;
use float_duration::{FloatDuration, TimePoint};
use step::TimeStep;

use framerate::FrameCount;

/// A specific point of time in a simulation.
///
/// `GameTime` knows both the wall time and game time of the simulation at a
/// fixed time. A `GameTime` object is usually created by calling
/// [`tick`](./struct.GameClock.html#method.tick) on a [`GameClock`](./struct.GameClock.html)
/// object.
#[derive(Debug, Clone)]
pub struct GameTime {
    frame_start_time: chrono::DateTime<chrono::Local>,
    total_wall_time: FloatDuration,
    total_game_time: FloatDuration,
    elapsed_game_time: FloatDuration,
    elapsed_wall_time: FloatDuration,
    frame_number: u64,
}

/// Time-tracking for use in real-time simulations.
///
/// `GameClock` provides time tracking for simulations. It
/// tracks total time the simulation has run for as well as
/// the elapsed time between individual frames.
///
/// In addition to tracking wall time, it tracks "game time"
/// which is the time used by the simulation itself. This game time is updated
/// each frame and
/// can be coupled directly to wall time [`VariableStep`](../step/struct.VariableStep.html)
/// or can be updated by a fixed amount (see the [`step`](../step/index.html) module for more
/// options).
///
/// `GameClock` uses [`GameTime`](./struct.GameTime.html) objects
/// to report the time at each frame to the program. The [`tick`](./fn.tick.html)
/// tells `GameClock` when a new frame is started, and returns
/// the `GameTime` object for that frame. This object can then be passed
/// to the rest of the simulation independently of `GameClock`.
#[derive(Debug, Clone)]
pub struct GameClock {
    last_frame_time: GameTime,
    start_wall_time: chrono::DateTime<chrono::Local>,
    total_game_time: time::Duration,
    current_frame: u64,
    clock_multiplier: f64,
}

/// A [`GameClock`](./struct.GameClock.html) builder,
/// allowing for customization of the initial time and parameters.
///
/// `GameClockBuilder` offers fine control over the initial state of a `GameClock`. For
/// most cases, using [`GameClock::new()`](./struct.GameClock.html#method.new) is good enough.
/// However, it can be useful to have more control in some situations, especially testing.
#[derive(Debug, Clone)]
pub struct GameClockBuilder {
    start_game_time: time::Duration,
    start_wall_time: chrono::DateTime<chrono::Local>,
    start_frame: u64,
    clock_multiplier: f64,
}

impl GameClock {
    /// Construct a new `GameClock` object, initialized to start at
    /// zero game time and a wall time of `chrono::Local::now()`.
    pub fn new() -> GameClock {
        let now = chrono::Local::now();
        let start_game_time = GameTime {
            frame_start_time: now,
            total_wall_time: FloatDuration::zero(),
            total_game_time: FloatDuration::zero(),
            elapsed_game_time: FloatDuration::zero(),
            elapsed_wall_time: FloatDuration::zero(),
            frame_number: 0,
        };

        GameClock {
            last_frame_time: start_game_time,
            start_wall_time: now,
            total_game_time: time::Duration::new(0, 0),
            current_frame: 0,
            clock_multiplier: 1.0,
        }
    }

    /// Return the current frame number.
    ///
    /// The frame number starts at `0` for "before the first frame"
    /// and increases by 1 every time `tick` is called.
    pub fn current_frame_number(&self) -> u64 {
        self.current_frame
    }
    /// Return the wall time when the `GameClock` was created.
    pub fn start_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.start_wall_time
    }
    /// Return the wall time at the start of the current frame.
    pub fn frame_start_time(&self) -> chrono::DateTime<chrono::Local> {
        self.last_frame_time().frame_start_time()
    }
    /// Return the total elapsed wall time at the start of the current frame.
    ///
    /// This is equivalent to the value returned by
    /// `last_frame_time().total_wall_time()`
    pub fn total_wall_time(&self) -> FloatDuration {
        self.last_frame_time().total_wall_time()
    }

    /// Return the amount of wall time elapsed since the start of the current frame.
    pub fn frame_elapsed_time(&self) -> FloatDuration {
        chrono::Local::now()
            .float_duration_since(self.last_frame_time().frame_start_time())
            .unwrap()
    }
    /// Return the [`GameTime`](./struct.GameTime.html) for the current frame.
    pub fn last_frame_time(&self) -> &GameTime {
        &self.last_frame_time
    }
    /// Return the rate at which game time is increasing.
    pub fn clock_multiplier(&self) -> f64 {
        self.clock_multiplier
    }
    /// Set the rate at which game time is increasing.
    pub fn set_clock_multiplier(&mut self, val: f64) -> &mut GameClock {
        self.clock_multiplier = val;
        self
    }

    /// Mark the start of a new frame, updating time statistics.
    ///
    /// The `GameTime` for the new frame is returned. This gives the time
    /// statistics for the entirety of the current frame. It is cached and
    /// can be later obtained by calling `last_frame_time`.
    ///
    /// `time_step` is a [`TimeStep`](../step/trait.TimeStep.html) reference used to
    /// compute the elapsed game time for the frame..
    ///
    /// The wall time for the start of the frame is computed via `chrono::Local::now()` at
    /// the start of the function. In order to override this to use a different clock
    /// or for debugging purposes, see
    /// [`tick_with_wall_time`](./struct.GameClock.html#methods.tick_with_wall_time).
    pub fn tick<T>(&mut self, time_step: &T) -> GameTime
    where
        T: TimeStep + ?Sized,
    {
        let frame_start = chrono::Local::now();
        self.tick_with_wall_time(time_step, frame_start)
    }

    /// Mark the start of a new frame with a specified wall time, updating time statistics.
    ///
    /// This function is like `tick` but allows for the start time for the
    /// frame to be specified.
    pub fn tick_with_wall_time<T>(
        &mut self,
        time_step: &T,
        frame_start: chrono::DateTime<chrono::Local>,
    ) -> GameTime
    where
        T: TimeStep + ?Sized,
    {
        self.current_frame += 1;

        let elapsed_wall_time = frame_start
            .float_duration_since(self.frame_start_time())
            .unwrap();

        let elapsed_game_time = time_step.time_step(&elapsed_wall_time) * self.clock_multiplier;
        let total_game_time = self.total_game_time + elapsed_game_time.to_std().unwrap();

        self.total_game_time = total_game_time;

        let time = GameTime {
            frame_start_time: frame_start,
            total_wall_time: FloatDuration::from(
                frame_start.signed_duration_since(self.start_wall_time),
            ),
            total_game_time: FloatDuration::from(total_game_time),
            elapsed_game_time,
            elapsed_wall_time,
            frame_number: self.current_frame,
        };

        self.last_frame_time = time.clone();

        time
    }

    /// Put the current thread to sleep if necessary in order to maintain the target frame rate.
    ///
    /// If the current frame has taken more time than the target frame rate allows, then the
    /// thread will not sleep. Otherwise it will sleep for
    /// `counter.target_time_per_frame() - self.frame_elapsed_time()`
    ///
    /// This method relies on the passed function `f` to actually perform the sleep.
    /// `f` receives the amount of sleep time requested and it is up to itself to
    /// sleep for that amount of time. If you don't care how the sleep is performed,
    /// use the [`sleep_remaining`](./struct.GameClock.html#method.sleep_remaining)
    /// method instead.
    pub fn sleep_remaining_via<C, F>(&mut self, counter: &C, f: F)
    where
        C: FrameCount + ?Sized,
        F: FnOnce(FloatDuration),
    {
        let remaining_time = counter.target_time_per_frame() -
            self.last_frame_time.elapsed_time_since_frame_start();
        if !remaining_time.is_negative() {
            f(remaining_time)
        }
    }

    /// Put the current thread to sleep if necessary in order to maintain the target frame rate.
    ///
    /// If the current frame has taken more time than the target frame rate allows, then the
    /// thread will not sleep. Otherwise it will sleep for
    /// `counter.target_time_per_frame() - self.frame_elapsed_time()`
    ///
    /// This method uses [`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html)
    /// to put the thread to sleep. If a different sleep function is desired, use
    /// the [`sleep_remaining_via`](./struct.GameClock.html#method.sleep_remaining_via)
    /// method instead.
    pub fn sleep_remaining<C>(&mut self, counter: &C)
    where
        C: FrameCount + ?Sized,
    {
        self.sleep_remaining_via(counter, |rem| thread::sleep(rem.to_std().unwrap()))
    }
}

impl Default for GameClock {
    fn default() -> GameClock {
        GameClock::new()
    }
}

impl GameTime {
    /// The game time at the time of creation of this `GameTime` object.
    pub fn total_game_time(&self) -> FloatDuration {
        self.total_game_time
    }
    /// The wall time elapsed since the start of the simulation.
    pub fn total_wall_time(&self) -> FloatDuration {
        self.total_wall_time
    }
    /// The wall time at the time of creation of this `GameTime` object.
    pub fn frame_start_time(&self) -> chrono::DateTime<chrono::Local> {
        self.frame_start_time
    }
    /// The amount of game time that passed since the previous frame.
    pub fn elapsed_game_time(&self) -> FloatDuration {
        self.elapsed_game_time
    }
    /// The amount of wall time that passed since the previous frame.
    pub fn elapsed_wall_time(&self) -> FloatDuration {
        self.elapsed_wall_time
    }
    /// The amount of elapsed wall time since the start of the current frame.
    ///
    /// This value is computed from the current instant when called, based
    /// on the frame start time. This can be used for intra-frame profiling.
    pub fn elapsed_time_since_frame_start(&self) -> FloatDuration {
        chrono::Local::now()
            .float_duration_since(self.frame_start_time())
            .unwrap()
    }
    /// The index of the current frame.
    ///
    /// This value increases by 1 for each frame created, and represents the total
    /// number of frames executed in the simulation.
    pub fn frame_number(&self) -> u64 {
        self.frame_number
    }
    /// Return the instantaneous frame rate between the last and current frames.
    ///
    /// The "instantaneous" frame rate is computed from the last frame's elapsed
    /// time, and takes no previous frames into account.
    ///
    /// For a more stable frame rate, use a
    /// [`FrameCount`](../framerate/counter/trait.FrameCount.html) object.
    pub fn instantaneous_frame_rate(&self) -> f64 {
        1.0 / self.elapsed_game_time.as_seconds()
    }
}

impl GameClockBuilder {
    /// Construct a new `GameClockBuilder` with default values.
    ///
    /// Calling `build` on the returned object returns immediately gives the same
    /// result as `GameClock::new()`.
    pub fn new() -> GameClockBuilder {
        GameClockBuilder {
            start_game_time: time::Duration::new(0, 0),
            start_wall_time: chrono::Local::now(),
            start_frame: 0,
            clock_multiplier: 1.0,
        }
    }

    /// Set the initial game time when the game is started.
    ///
    /// Defaults to zero.
    pub fn start_game_time(&mut self, time: time::Duration) -> &mut GameClockBuilder {
        self.start_game_time = time;
        self
    }
    /// Set the initial wall time when the game is started.
    ///
    /// Defaults to `chrono::Local::now()`.
    pub fn start_wall_time(
        &mut self,
        time: chrono::DateTime<chrono::Local>,
    ) -> &mut GameClockBuilder {
        self.start_wall_time = time;
        self
    }
    /// Set the initial frame number.
    ///
    /// Defaults to `0`.
    pub fn start_frame(&mut self, frame_num: u64) -> &mut GameClockBuilder {
        self.start_frame = frame_num;
        self
    }
    /// Set the initial clock multiplier.
    ///
    /// Defaults to `1.0`.
    pub fn clock_multiplier(&mut self, multiplier: f64) -> &mut GameClockBuilder {
        self.clock_multiplier = multiplier;
        self
    }
    /// Construct a `GameClock` object with the set parameters.
    pub fn build(&self) -> GameClock {
        let start_game_time = GameTime {
            frame_start_time: self.start_wall_time,
            total_wall_time: FloatDuration::zero(),
            total_game_time: FloatDuration::from(self.start_game_time),
            elapsed_game_time: FloatDuration::zero(),
            elapsed_wall_time: FloatDuration::zero(),
            frame_number: self.start_frame,
        };

        GameClock {
            last_frame_time: start_game_time,
            start_wall_time: self.start_wall_time,
            total_game_time: self.start_game_time,
            current_frame: self.start_frame,
            clock_multiplier: self.clock_multiplier,
        }
    }
}

impl Default for GameClockBuilder {
    fn default() -> GameClockBuilder {
        GameClockBuilder::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Local;
    use step;

    #[test]
    fn test_clock_construct() {
        let clock = GameClock::new();
        assert_eq!(clock.clock_multiplier(), 1.0);
        assert_eq!(
            clock.last_frame_time().elapsed_game_time(),
            FloatDuration::zero()
        );

        let clock2 = GameClockBuilder::new()
            .clock_multiplier(5.0)
            .start_frame(100)
            .build();

        assert_eq!(clock2.current_frame_number(), 100);
        assert_eq!(clock2.clock_multiplier(), 5.0);
        assert_eq!(
            clock2.last_frame_time().elapsed_game_time(),
            FloatDuration::zero()
        );
        assert_eq!(
            clock2.last_frame_time().elapsed_wall_time(),
            FloatDuration::zero()
        );
        assert!(clock2.last_frame_time().total_wall_time() == FloatDuration::zero());

        let start_time = Local::today().and_hms(12, 0, 0);
        let clock3 = GameClockBuilder::default()
            .start_game_time(time::Duration::new(100, 0))
            .start_wall_time(start_time)
            .clone()
            .build();

        assert_eq!(clock3.current_frame_number(), 0);
        assert_eq!(
            clock3.last_frame_time().total_game_time(),
            FloatDuration::seconds(100.0)
        );
        assert_eq!(clock3.last_frame_time().frame_start_time(), start_time);

        assert_eq!(GameClock::default().current_frame_number(), 0);
        assert_eq!(
            GameClock::default().last_frame_time().total_game_time(),
            FloatDuration::zero()
        );
    }

    #[test]
    fn test_clock_tick() {
        let mut clock = GameClock::new();
        let time = clock.tick(&step::ConstantStep::new(FloatDuration::seconds(1.0)));
        assert_eq!(time.frame_number(), 1);
        assert_eq!(time.total_game_time(), FloatDuration::seconds(1.0));
        assert!(time.frame_start_time() > clock.start_wall_time());
        assert!(time.frame_start_time() < Local::now());
        assert_eq!(time.elapsed_game_time(), FloatDuration::seconds(1.0));
        assert!(time.elapsed_wall_time() < FloatDuration::seconds(1.0));

        let time2 = clock.tick(&step::VariableStep::new());
        assert_eq!(time2.frame_number(), 2);
        assert!(time2.total_wall_time() > time.total_wall_time());
        assert_eq!(time2.elapsed_game_time(), time2.elapsed_wall_time());
        assert!(time2.total_game_time() > time.total_game_time());
        assert!(time2.elapsed_time_since_frame_start() > FloatDuration::seconds(0.0));
        assert!(time2.elapsed_time_since_frame_start() < FloatDuration::seconds(0.01));
    }

    #[test]
    fn test_clock_tick_loop() {
        let dt = FloatDuration::milliseconds(50.0);
        let step = step::ConstantStep::new(dt);
        let mut clock = GameClock::default().clone();

        for x in 0..10 {
            let frame_time = clock.tick(&step);

            assert_eq!(frame_time.frame_number(), x + 1);
            assert_eq!(frame_time.elapsed_game_time(), dt);
            relative_eq!(
                frame_time.total_game_time(),
                dt * (x + 1) as f64,
                epsilon = 1e-8
            );
            assert!(frame_time.elapsed_time_since_frame_start() > FloatDuration::zero());
        }

        let frame_time = clock.last_frame_time();
        assert_eq!(frame_time.total_game_time(), FloatDuration::seconds(0.5));
        assert!(frame_time.elapsed_time_since_frame_start() < FloatDuration::milliseconds(1.0));
        assert!(frame_time.frame_start_time() > clock.start_wall_time());
    }

    #[test]
    fn test_wall_time() {
        let mut clock = GameClock::new();
        clock.set_clock_multiplier(2.0);
        assert_eq!(clock.clock_multiplier(), 2.0);

        let step = step::VariableStep::new();
        let start_time = clock.start_wall_time();
        let wall_dt = chrono::Duration::seconds(1);

        for x in 0..10 {
            let time = start_time + wall_dt * (x + 1);
            let frame_time = clock.tick_with_wall_time(&step, time);

            assert_eq!(
                frame_time.total_wall_time(),
                time.float_duration_since(clock.start_wall_time()).unwrap()
            );
            assert_eq!(
                2.0 * frame_time.elapsed_wall_time(),
                frame_time.elapsed_game_time()
            );
            assert_eq!(
                frame_time.elapsed_wall_time(),
                FloatDuration::from_chrono(wall_dt)
            );
            assert_eq!(frame_time.instantaneous_frame_rate(), 0.5);
        }
    }
}