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
//! Types for computing game time from wall time.
use float_duration::FloatDuration;
use framerate::FrameCount;

/// Compute elapsed game time for a frame.
pub trait TimeStep {
    /// Compute the time step for the next frame.
    fn time_step(&self, wall_time: &FloatDuration) -> FloatDuration;
}

/// A time step based on elapsed wall time.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct VariableStep {}
/// A fixed time step derived from a set frame rate.
#[derive(Debug)]
pub struct FixedStep<'a, C: 'a + FrameCount + ?Sized> {
    counter: &'a C,
}
/// A specific, constant time step.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct ConstantStep {
    step: FloatDuration,
}

impl VariableStep {
    /// Construct a new `VariableStep` object.
    pub fn new() -> VariableStep {
        VariableStep {}
    }
}
impl<'a, C> FixedStep<'a, C>
where
    C: 'a + FrameCount + ?Sized,
{
    /// Construct a new `FixedStep` object based on the set frame rate in `counter`.
    pub fn new(counter: &'a C) -> FixedStep<'a, C> {
        FixedStep { counter: counter }
    }
}
impl ConstantStep {
    /// Construct a new `ConstantStep` object with a set time step `step`.
    pub fn new(step: FloatDuration) -> ConstantStep {
        ConstantStep { step }
    }
    /// Construct a new `ConstantStep` that does not advance game time.
    pub fn null_step() -> ConstantStep {
        ConstantStep::new(FloatDuration::zero())
    }
}

impl TimeStep for VariableStep {
    fn time_step(&self, wall_time: &FloatDuration) -> FloatDuration {
        *wall_time
    }
}
impl<'a, C> TimeStep for FixedStep<'a, C>
where
    C: 'a + FrameCount + ?Sized,
{
    fn time_step(&self, _: &FloatDuration) -> FloatDuration {
        self.counter.target_time_per_frame()
    }
}
impl TimeStep for ConstantStep {
    fn time_step(&self, _: &FloatDuration) -> FloatDuration {
        self.step
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clock::GameClock;
    use framerate::{counter, sample};

    #[test]
    fn test_fixed_step() {
        let mut clock = GameClock::new();
        let mut count =
            counter::FrameCounter::new(20.0, sample::RunningAverageSampler::with_max_samples(20));

        for _ in 0..20 {
            let time = {
                let step = FixedStep::new(&count);
                clock.tick(&step)
            };
            count.tick(&time);
            assert_eq!(time.elapsed_game_time(), FloatDuration::seconds(1.0 / 20.0));
        }
        let time = clock.last_frame_time();
        assert_eq!(time.total_game_time(), FloatDuration::seconds(1.0));
    }

    #[test]
    fn test_null_step() {
        let mut clock = GameClock::new();
        let step = ConstantStep::null_step();

        for _ in 0..1000 {
            let time = clock.tick(&step);
            assert_eq!(time.elapsed_game_time(), FloatDuration::zero());
        }

        assert_eq!(
            clock.last_frame_time().total_game_time(),
            FloatDuration::zero()
        );

    }
}