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
use action;
use per_second;
use realtime;

pub struct Looper {
    fps: f32,
}

impl Looper {
    pub fn new(fps: f32) -> Self {
        Looper { fps: fps }
    }

    pub fn run<R, U>(&self, mut render: R, mut update: U)
    where
        R: FnMut(i32) -> action::Action,
        U: FnMut(i32) -> action::Action,
    {
        let mut realtime = realtime::Realtime::new(self.fps);
        let mut rps = per_second::PerSecond::new();
        let mut ups = per_second::PerSecond::new();

        loop {
            rps.tick();

            match render(rps.get_fps()) {
                action::Action::Stop => break,
                action::Action::Continue => (),
            };

            for acc in realtime.tick() {
                ups.tick();
                match update(ups.get_fps()) {
                    action::Action::Stop => break,
                    action::Action::Continue => (),
                }
                realtime.acc(acc);
            }
        }
    }
}