pub struct Rect { /* private fields */ }Expand description
Utilies for working with Image type. As with other APIs, the (0, 0) position is left top of the screen, then it grows downward and rightward.
Implementations§
Source§impl Rect
impl Rect
Sourcepub fn new(x: i32, y: i32, w: u32, h: u32) -> Rect
pub fn new(x: i32, y: i32, w: u32, h: u32) -> Rect
Examples found in repository?
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, 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 //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_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(10, 120, 790, 400)) // to test blit_over does not trigger
94 .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}Sourcepub fn area(&self) -> usize
pub fn area(&self) -> usize
Return (width * height) as usize, which you can use it as a buffer length in an array.
pub fn left(&self) -> i32
pub fn right(&self) -> i32
pub fn top(&self) -> i32
pub fn bottom(&self) -> i32
pub fn width(&self) -> u32
pub fn height(&self) -> u32
Sourcepub fn iwidth(&self) -> i32
pub fn iwidth(&self) -> i32
A convenient type for width() with i32 for rect calculations. Consider to use other utilities functions if it suitable.
Sourcepub fn iheight(&self) -> i32
pub fn iheight(&self) -> i32
A convenient type for height() with i32 for rect calculations. Consider to use other utilities functions if it suitable.
Sourcepub fn container(&self, other: &Rect) -> Rect
pub fn container(&self, other: &Rect) -> Rect
Create a union between two Rectangles. If the two does not overlap, the returned Rect will be larger than two rectangle areas.
Sourcepub fn contains(&self, x: i32, y: i32) -> bool
pub fn contains(&self, x: i32, y: i32) -> bool
Check if a point is in or at the edge of Rectangle.
To check if two Rectangle overlaps, use !a.intersection(b).is_empty().
Sourcepub fn intersection(&self, other: &Rect) -> Rect
pub fn intersection(&self, other: &Rect) -> Rect
Create an intersection between two Rectangles. If the two does not overlap, the returned Rect will be empty.