image_bench/
image_bench.rs

1// SPDX-License-Identifier: MIT
2
3use orbclient::{Color, EventOption, Renderer, Window};
4
5const TIMES: usize = 10;
6
7macro_rules! time {
8    ($msg:tt, $block: block) => ({
9        let _time_instant = ::std::time::Instant::now();
10        $block
11        let _time_duration = _time_instant.elapsed();
12        let _time_fractional = _time_duration.as_secs() as f64
13                             + (_time_duration.subsec_nanos() as f64)/1000000000.0;
14        println!(
15            "{}: {} ms",
16            $msg,
17            _time_fractional * 1000.0
18        );
19    });
20}
21
22fn main() {
23    //let (width, height) = orbclient::get_display_size().unwrap();
24
25    let mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27    window.set(Color::rgb(255, 255, 255));
28
29    //create image data : a green square
30    let data = vec![Color::rgba(100, 200, 10, 20); 412500];
31    let data2 = vec![Color::rgba(200, 100, 10, 20); 412500];
32    let data3 = vec![Color::rgba(10, 100, 100, 20); 412500];
33    let data4 = vec![Color::rgba(10, 100, 200, 20); 480000];
34
35    //draw image benchmarking
36    println!("Benchmarking implementations to draw an image on window:");
37
38    time!("image_legacy", {
39        for _i in 0..TIMES {
40            window.image_legacy(15, 15, 750, 550, &data[..]);
41        }
42    });
43
44    time!("image_fast", {
45        for _i in 0..TIMES {
46            window.image_fast(20, 20, 750, 550, &data2[..]);
47        }
48    });
49
50    time!("image_opaque", {
51        for _i in 0..TIMES {
52            window.image_opaque(50, 50, 750, 550, &data3[..]);
53        }
54    });
55
56    time!("image_over", {
57        for _i in 0..TIMES {
58            window.image_over(50, &data4[..360000]);
59        }
60    });
61
62    println!("------------------------------------------------");
63
64    window.sync();
65
66    'events: loop {
67        for event in window.events() {
68            match event.to_option() {
69                EventOption::Quit(_quit_event) => break 'events,
70                EventOption::Mouse(evt) => println!(
71                    "At position {:?} pixel color is : {:?}",
72                    (evt.x, evt.y),
73                    window.getpixel(evt.x, evt.y)
74                ),
75                event_option => println!("{:?}", event_option),
76            }
77        }
78    }
79}