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
use std::time::{Instant, Duration};
use std::thread;

/// Passed to `renderloop()` and `mainloop()` callbacks.
#[derive(Copy, Clone)]
pub struct LoopState {
    /// Time between frames in as Duration.
    pub delta       : Duration,
    /// Time between frames in seconds.
    pub delta_f32   : f32,
    /// Time since loop was started as Duration.
    pub elapsed     : Duration,
    /// Time since loop was started in seconds.
    pub elapsed_f32 : f32,
    /// Current frame id.
    pub frame_id    : u64,
    /// Current framerate.
    pub fps         : u32,
    /// Current state id for `mainloop()`'s `state_callback`.
    pub state_id    : u32,
}

/// Main loop helper function. Provides callbacks for game state changes and rendering. Calls
/// state_callback multiple times if the actual render_callback call interval exceeds the given interval.
/// Both callbacks receive a LoopState object containing frame delta and fps data.
pub fn mainloop<F, G>(interval: Duration, mut state_callback: F, mut render_callback: G) where F: FnMut(LoopState) -> bool, G: FnMut(LoopState) -> bool {

    let mut accumulator = Duration::new(0, 0);
    let mut previous_clock = Instant::now();
    let start_clock = previous_clock;

    let second = Duration::new(1, 0);
    let mut second_elapsed = Duration::new(0, 0);
    let mut frames_elapsed = 0;
    let mut fps = 0;
    let mut frame_id = 0;

    loop {

        let now = Instant::now();
        let delta = now - previous_clock;
        let elapsed = now - start_clock;

        let mut state_info = LoopState {
            delta       : delta,
            delta_f32   : delta.as_secs() as f32 + (delta.subsec_nanos() as f64 / 1000000000.0) as f32,
            elapsed     : elapsed,
            elapsed_f32 : elapsed.as_secs() as f32 + (elapsed.subsec_nanos() as f64 / 1000000000.0) as f32,
            frame_id    : frame_id,
            fps         : fps,
            state_id    : 0,
        };

        if render_callback(state_info) == false {
            break;
        }

        // determine thread sleep to maintain X FPS
        accumulator += delta;

        while accumulator >= interval {
            accumulator -= interval;
            if state_callback(state_info) == false {
                break;
            }
            state_info.state_id += 1;
        }

        // framerate print
        second_elapsed += now - previous_clock;
        frames_elapsed += 1;
        frame_id += 1;

        if second_elapsed >= second {
            fps = frames_elapsed;
            second_elapsed -= second;
            frames_elapsed = 0;
        }

        previous_clock = now;
        thread::sleep(interval - accumulator);
    }
}

/// Render loop helper function. Provides given callback with frame delta and fps data.
/// This is a more simple alternative to the main loop helper function.
pub fn renderloop<G>(mut render_callback: G) where G: FnMut(LoopState) -> bool {

    let mut previous_clock = Instant::now();
    let start_clock = previous_clock;

    let second = Duration::new(1, 0);
    let mut second_elapsed = Duration::new(0, 0);
    let mut frames_elapsed = 0;
    let mut fps = 0;
    let mut frame_id = 0;

    loop {

        let now = Instant::now();
        let delta = now - previous_clock;
        let elapsed = now - start_clock;

        let state_info = LoopState {
            delta       : delta,
            delta_f32   : delta.as_secs() as f32 + (delta.subsec_nanos() as f64 / 1000000000.0) as f32,
            elapsed     : elapsed,
            elapsed_f32 : elapsed.as_secs() as f32 + (elapsed.subsec_nanos() as f64 / 1000000000.0) as f32,
            frame_id    : frame_id,
            fps         : fps,
            state_id    : 0,
        };

        if render_callback(state_info) == false {
            break;
        }

        // framerate print
        second_elapsed += now - previous_clock;
        frames_elapsed += 1;
        frame_id += 1;

        if second_elapsed >= second {
            fps = frames_elapsed;
            second_elapsed -= second;
            frames_elapsed = 0;
        }

        previous_clock = now;
    }
}