Function game_loop::game_loop

source ·
pub fn game_loop<G, U, R>(
    game: G,
    updates_per_second: u32,
    max_frame_time: f64,
    update: U,
    render: R
) -> GameLoop<G, Time, ()>
where U: FnMut(&mut GameLoop<G, Time, ()>), R: FnMut(&mut GameLoop<G, Time, ()>),
Examples found in repository?
examples/game_of_life.rs (lines 13-23)
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
fn main() {
    let mut game = GameOfLife::new(12, 12);

    // Make some of the cells alive.
    game.set(5, 5);
    game.set(5, 6);
    game.set(5, 7);
    game.set(6, 6);

    // Run the game loop with 2 updates per second.
    let g = game_loop(game, 2, 1.0, |g| {
        g.game.update();
    }, |g| {
        // Pass the blending factor (even though this example doesn't use it).
        g.game.render(g.blending_factor());

        // Exit after 10 seconds.
        if g.running_time() > 10.0 {
            g.exit();
        }
    });

    // Use the 'g' variable to query the game loop after it finishes.
    println!("Exiting after {} seconds", g.running_time());
    println!("");
    println!("Last frame time: {}", g.last_frame_time());
    println!("Number of updates: {}", g.number_of_updates());
    println!("Number of renders: {}", g.number_of_renders());
}