pub fn sleep(dur: f64)Expand description
Put the thread to sleep for dur seconds
Examples found in repository?
examples/messages.rs (line 27)
14fn main() {
15 let app = app::App::default();
16 let mut wind = Window::default().with_size(400, 300);
17 let mut frame = Frame::default().size_of(&wind).with_label("0");
18
19 let mut val = 0;
20
21 wind.show();
22
23 let (s, r) = app::channel::<Message>();
24
25 std::thread::spawn(move || {
26 loop {
27 app::sleep(1.);
28 s.send(Message::Increment(2));
29 }
30 });
31
32 while app.wait() {
33 if let Some(Message::Increment(step)) = r.recv() {
34 inc_frame(&mut frame, &mut val, step)
35 }
36 }
37}More examples
examples/system_fonts.rs (line 47)
28fn main() {
29 let app = app::App::default().load_system_fonts();
30 // To load a font by path, check the App::load_font() method
31 let fonts = app::fonts();
32 // println!("{:?}", fonts);
33 let mut wind = window::Window::default().with_size(400, 300);
34 let mut frame = frame::Frame::default().size_of(&wind);
35 frame.set_label_size(30);
36 wind.set_color(enums::Color::White);
37 wind.end();
38 wind.show();
39 println!("The system has {} fonts!\nStarting slideshow!", fonts.len());
40 let mut i = 0;
41 while app.wait() {
42 if i == fonts.len() {
43 i = 0;
44 }
45 frame.set_label(&format!("[{}]", fonts[i]));
46 frame.set_label_font(enums::Font::by_index(i));
47 app::sleep(0.5);
48 i += 1;
49 }
50}examples/fb.rs (line 37)
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 let app = app::App::default();
16 let mut win = Window::default()
17 .with_size(WIDTH, HEIGHT)
18 .with_label("Framebuffer");
19 let mut frame = frame::Frame::default_fill();
20 win.end();
21 win.make_resizable(true);
22 win.show();
23
24 let mut framebuf: Vec<u8> = vec![0; (WIDTH * HEIGHT * 4) as usize];
25 let mut world = World::new();
26
27 unsafe {
28 utils::blit_rgba_nocopy(&mut frame, &framebuf);
29 }
30
31 app::add_idle(move |_| {
32 world.update();
33 world.draw(&mut framebuf);
34 // utils::blit_rgba(&mut frame, &framebuf).unwrap(); // A safe variant of draw_rgba_nocopy
35 win.redraw();
36 // sleeps are necessary when calling redraw in the event loop
37 app::sleep(0.016);
38 });
39
40 app.run()?;
41 Ok(())
42}examples/pong.rs (line 107)
34fn main() {
35 let app = app::App::default();
36 let mut wind = window::Window::default()
37 .with_size(800, 600)
38 .with_label("Pong!");
39 wind.set_center_screen();
40 let mut ball = Ball::new(40, 40);
41 ball.wid.set_color(Color::White);
42 wind.set_color(Color::Black);
43 wind.end();
44 wind.show();
45
46 let paddle_pos = Rc::from(RefCell::from(320)); // paddle's starting x position
47
48 // This is called whenever the window is drawn and redrawn (in the event loop)
49 wind.draw({
50 let paddle_pos = paddle_pos.clone();
51 move |_| {
52 draw::set_draw_color(Color::White);
53 draw::draw_rectf(*paddle_pos.borrow(), 540, 160, 20);
54 }
55 });
56
57 wind.handle({
58 let paddle_pos = paddle_pos.clone();
59 move |_, ev| {
60 match ev {
61 // we handle focus to be able to accept KeyDown events
62 Event::Focus => true,
63 Event::KeyDown => {
64 let key = app::event_key();
65 match key {
66 Key::Left | KEY_A => *paddle_pos.borrow_mut() -= 30,
67 Key::Right | KEY_D => *paddle_pos.borrow_mut() += 30,
68 _ => return false,
69 }
70 true
71 }
72 Event::Move => {
73 // Mouse's x position relative to the paddle's center
74 *paddle_pos.borrow_mut() = app::event_coords().0 - 80;
75 true
76 }
77 _ => false,
78 }
79 }
80 });
81
82 app::add_idle(move |_| {
83 ball.pos.0 += 10 * ball.dir.0 as i32; // The increment in x position
84 ball.pos.1 += 10 * ball.dir.1 as i32; // The increment in y position
85 if ball.pos.1 == 540 - 40
86 && (ball.pos.0 > *paddle_pos.borrow() - 40 && ball.pos.0 < *paddle_pos.borrow() + 160)
87 {
88 ball.dir.1 = Direction::Negative; // Reversal of motion when hitting the paddle
89 }
90 if ball.pos.1 == 0 {
91 ball.dir.1 = Direction::Positive; // Reversal of motion when hitting the top border
92 }
93 if ball.pos.0 == 800 - 40 {
94 ball.dir.0 = Direction::Negative; // Reversal of motion when hitting the right border
95 }
96 if ball.pos.0 == 0 {
97 ball.dir.0 = Direction::Positive; // Reversal of motion when hitting the left border
98 }
99 if ball.pos.1 > 600 {
100 // Resetting the ball position after it bypasses the paddle
101 ball.pos = (0, 0);
102 ball.dir = (Direction::Positive, Direction::Positive);
103 }
104 ball.wid.resize(ball.pos.0, ball.pos.1, 40, 40); // Moves the ball
105 wind.redraw();
106 // sleeps are necessary when calling redraw in the event loop
107 app::sleep(0.016);
108 });
109 app.run().unwrap();
110}