1use orbclient::{image::ImageRef, rect::Rect, Color, EventOption, Renderer, Window};
4
5const TIMES: usize = 100;
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 mut window = Window::new(10, 10, 800, 600, "IMAGE BENCHMARK").unwrap();
26
27 window.set(Color::rgb(255, 255, 255));
28
29 let data = vec![Color::rgba(100, 200, 10, 3); 412500];
31 let mut data2 = vec![Color::rgba(200, 100, 10, 3); 412500];
32 let mut data3 = vec![Color::rgba(10, 100, 100, 3); 412500];
33 let mut data4 = vec![Color::rgba(10, 100, 200, 3); 800 * 400];
34
35 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_over", {
45 for _i in 0..TIMES {
46 window.image_over(50, &data4[..]);
47 }
48 });
49
50 time!("image_fast", {
51 for _i in 0..TIMES {
52 window.image_fast(20, 20, 750, 550, &data2[..]);
53 }
54 });
55
56 time!("image_opaque", {
57 for _i in 0..TIMES {
58 window.image_opaque(30, 30, 750, 550, &data3[..]);
59 }
60 });
61
62 time!("image_roi_mut_blend", {
63 let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
64 for _i in 0..TIMES {
65 ImageRef::from_renderer(&mut window)
66 .roi_mut(&Rect::new(40, 40, 750, 550))
67 .blend(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
68 }
69 });
70
71 time!("image_roi_mut_blit_mask", {
72 let data2_roi = ImageRef::from_data(750, 550, &mut data2[..]);
73 for _i in 0..TIMES {
74 ImageRef::from_renderer(&mut window)
75 .roi_mut(&Rect::new(40, 40, 750, 550))
76 .blit_mask(&data2_roi.roi(&Rect::new(0, 0, 750, 550)));
77 }
78 });
79
80 time!("image_roi_mut_blit", {
81 let data3_roi = ImageRef::from_data(750, 550, &mut data3[..]);
82 for _i in 0..TIMES {
83 ImageRef::from_renderer(&mut window)
84 .roi_mut(&Rect::new(50, 50, 750, 550))
85 .blit(&data3_roi.roi(&Rect::new(0, 0, 750, 550)));
86 }
87 });
88
89 time!("image_roi_mut_blit_over", {
90 let data4_roi = ImageRef::from_data(800, 400, &mut data4[..]);
91 for _i in 0..TIMES {
92 ImageRef::from_renderer(&mut window)
93 .roi_mut(&Rect::new(0, 120, 800, 400))
95 .blit(&data4_roi.roi(&Rect::new(0, 0, 800, 400)));
96 }
97 });
98
99 println!("------------------------------------------------");
100
101 window.sync();
102
103 'events: loop {
104 for event in window.events() {
105 match event.to_option() {
106 EventOption::Quit(_quit_event) => break 'events,
107 EventOption::Mouse(evt) => println!(
108 "At position {:?} pixel color is : {:?}",
109 (evt.x, evt.y),
110 window.getpixel(evt.x, evt.y)
111 ),
112 event_option => println!("{:?}", event_option),
113 }
114 }
115 }
116}