Function fltk::app::sleep

source ·
pub fn sleep(dur: f64)
Expand description

Put the thread to sleep for dur seconds

Examples found in repository?
examples/messages.rs (line 26)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() {
    let app = app::App::default();
    let mut wind = Window::default().with_size(400, 300);
    let mut frame = Frame::default().size_of(&wind).with_label("0");

    let mut val = 0;

    wind.show();

    let (s, r) = app::channel::<Message>();

    std::thread::spawn(move || loop {
        app::sleep(1.);
        s.send(Message::Increment(2));
    });

    while app.wait() {
        if let Some(Message::Increment(step)) = r.recv() {
            inc_frame(&mut frame, &mut val, step)
        }
    }
}
More examples
Hide additional examples
examples/system_fonts.rs (line 47)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    let app = app::App::default().load_system_fonts();
    // To load a font by path, check the App::load_font() method
    let fonts = app::fonts();
    // println!("{:?}", fonts);
    let mut wind = window::Window::default().with_size(400, 300);
    let mut frame = frame::Frame::default().size_of(&wind);
    frame.set_label_size(30);
    wind.set_color(enums::Color::White);
    wind.end();
    wind.show();
    println!("The system has {} fonts!\nStarting slideshow!", fonts.len());
    let mut i = 0;
    while app.wait() {
        if i == fonts.len() {
            i = 0;
        }
        frame.set_label(&format!("[{}]", fonts[i]));
        frame.set_label_font(enums::Font::by_index(i));
        app::sleep(0.5);
        i += 1;
    }
}
examples/fb.rs (line 37)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app = app::App::default();
    let mut win = Window::default()
        .with_size(WIDTH, HEIGHT)
        .with_label("Framebuffer");
    let mut frame = frame::Frame::default().size_of(&win);
    win.end();
    win.make_resizable(true);
    win.show();

    let mut framebuf: Vec<u8> = vec![0; (WIDTH * HEIGHT * 4) as usize];
    let mut world = World::new();

    unsafe {
        draw::draw_rgba_nocopy(&mut frame, &framebuf);
    }

    app::add_idle3(move |_| {
        world.update();
        world.draw(&mut framebuf);
        // draw::draw_rgba(&mut frame, &framebuf).unwrap(); // A safe variant of draw_rgba_nocopy
        win.redraw();
        // sleeps are necessary when calling redraw in the event loop
        app::sleep(0.016);
    });

    app.run()?;
    Ok(())
}
examples/pong.rs (line 80)
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
fn main() {
    let app = app::App::default();
    let mut wind = window::Window::default()
        .with_size(800, 600)
        .center_screen()
        .with_label("Pong!");
    let mut ball = Ball {
        wid: valuator::FillDial::new(0, 0, 40, 40, ""),
        pos: (0, 0),
        dir: (Direction::Positive, Direction::Positive),
    };
    ball.wid.set_color(enums::Color::White);
    wind.set_color(enums::Color::Black);
    wind.end();
    wind.show();

    let paddle_pos = Rc::from(RefCell::from(320)); // paddle's starting x position
    let paddle_c = paddle_pos.clone();

    // This is called whenever the window is drawn and redrawn (in the event loop)
    wind.draw(move |_| {
        draw::set_draw_color(enums::Color::White);
        draw::draw_rectf(*paddle_c.borrow(), 540, 160, 20);
    });

    let paddle_c = paddle_pos.clone();
    wind.handle(move |_, ev| {
        match ev {
            enums::Event::Move => {
                // Mouse's x position relative to the paddle's center
                *paddle_pos.borrow_mut() = app::event_coords().0 - 80;
                true
            }
            _ => false,
        }
    });

    app::add_idle3(move |_| {
        ball.pos.0 += 10 * ball.dir.0 as i32; // The increment in x position
        ball.pos.1 += 10 * ball.dir.1 as i32; // The increment in y position
        if ball.pos.1 == 540 - 40
            && (ball.pos.0 > *paddle_c.borrow() - 40 && ball.pos.0 < *paddle_c.borrow() + 160)
        {
            ball.dir.1 = Direction::Negative; // Reversal of motion when hitting the paddle
        }
        if ball.pos.1 == 0 {
            ball.dir.1 = Direction::Positive; // Reversal of motion when hitting the top border
        }
        if ball.pos.0 == 800 - 40 {
            ball.dir.0 = Direction::Negative; // Reversal of motion when hitting the right border
        }
        if ball.pos.0 == 0 {
            ball.dir.0 = Direction::Positive; // Reversal of motion when hitting the left border
        }
        if ball.pos.1 > 600 {
            // Resetting the ball position after it bypasses the paddle
            ball.pos = (0, 0);
            ball.dir = (Direction::Positive, Direction::Positive);
        }
        ball.wid.resize(ball.pos.0, ball.pos.1, 40, 40); // Moves the ball
        wind.redraw();
        // sleeps are necessary when calling redraw in the event loop
        app::sleep(0.016);
    });
    app.run().unwrap();
}