subimage_example/
subimage_example.rs

1fn main() {
2    use spottedcat::{Bounds, Context, DrawOption, Image, Pt, Spot, WindowConfig, run};
3
4    struct SubImageExample {
5        tree: Image,
6        tree_sub: Image,
7    }
8
9    impl Spot for SubImageExample {
10        fn initialize(_context: &mut Context) -> Self {
11            const TREE_PNG: &[u8] = include_bytes!("../assets/happy-tree.png");
12            let decoded = image::load_from_memory(TREE_PNG).expect("failed to decode happy-tree.png");
13            let rgba = decoded.to_rgba8();
14            let (w, h) = (rgba.width(), rgba.height());
15            let tree = Image::new_from_rgba8(Pt::from(w), Pt::from(h), rgba.as_raw())
16                .expect("failed to create happy-tree image");
17
18            let crop_w = (w / 2).max(1);
19            let crop_h = (h / 2).max(1);
20            let tree_sub = Image::sub_image(
21                tree,
22                Bounds::new(Pt::from(0.0), Pt::from(0.0), Pt::from(crop_w), Pt::from(crop_h)),
23            )
24                .expect("failed to create sub image");
25
26            Self { tree, tree_sub }
27        }
28
29        fn draw(&mut self, context: &mut Context) {
30            let mut opts = DrawOption::default();
31            opts.position = [Pt::from(20.0), Pt::from(20.0)];
32            opts.scale = [1.0, 1.0];
33            self.tree.draw(context, opts);
34
35            let mut opts = DrawOption::default();
36            opts.position = [Pt::from(420.0), Pt::from(20.0)];
37            opts.scale = [1.0, 1.0];
38            self.tree_sub.draw(context, opts);
39        }
40
41        fn update(&mut self, _context: &mut Context, _dt: std::time::Duration) {}
42
43        fn remove(&self) {}
44    }
45
46    let mut cfg = WindowConfig::default();
47    cfg.title = "subimage example".to_string();
48    run::<SubImageExample>(cfg);
49}